HDU

来源:互联网 发布:口袋助理软件 编辑:程序博客网 时间:2024/06/05 16:11

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540点击打开链接

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9539    Accepted Submission(s): 3727


Problem Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
 

Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
 

Output
Output the answer to each of the Army commanders’ request in order on a separate line.
 

Sample Input
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 

Sample Output
1024
 


区间合并的题 用三个变量分别记录从区间最左端的最长区间个数 最右端的最长区间个数 和 区间内最长区间个数

每次回缩判断即可

#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;#define maxn 60010struct xjy{    int left;    int right;    int lmax;    int rmax;    int mmax;};xjy tree[maxn<<2];int a[maxn];int ans=0;void build(int i,int left ,int right){    {        tree[i].left=left;        tree[i].right=right;        tree[i].lmax=tree[i].rmax=tree[i].mmax=right-left+1;    }    if(left!=right)    {        int mid=(left+right)>>1;        build(i<<1,left,mid);        build (i<<1|1,mid+1,right);    }}void updateregion(int i,int left,int right,int val){    if(tree[i].left==left&&tree[i].right==right)    {        tree[i].lmax=tree[i].rmax=tree[i].mmax=val;        return;    }    int mid=(tree[i].left+tree[i].right)>>1;    if(right<=mid)        updateregion(i<<1,left,right,val);    else if(left>mid)        updateregion(i<<1|1,left,right,val);    else    {        updateregion(i<<1,left,mid,val);        updateregion(i<<1|1,mid+1,right,val);    }    tree[i].lmax=tree[i<<1].lmax;    tree[i].rmax=tree[i<<1|1].rmax;    tree[i].mmax=max(max(tree[i<<1].mmax,tree[i<<1|1].mmax),tree[i<<1].rmax+tree[i<<1|1].lmax);    if(tree[i<<1].lmax==(tree[i<<1].right-tree[i<<1].left+1))        tree[i].lmax+=tree[i<<1|1].lmax;    if(tree[i<<1|1].rmax==(tree[i<<1|1].right-tree[i<<1|1].left+1))        tree[i].rmax+=tree[i<<1].rmax;}int query(int i,int t){    if(tree[i].left==tree[i].right||tree[i].mmax==(tree[i].right-tree[i].left+1)||!tree[i].mmax)    {        return tree[i].mmax;    }    int mid=(tree[i].left+tree[i].right)>>1;    if(t<=mid)    {        if(t>=tree[i<<1].right-tree[i<<1].rmax+1)            return query(i<<1,t)+query(i<<1|1,mid+1);        else            return query(i<<1,t);    }    else    {        if(t<=tree[i<<1|1].left+tree[i<<1|1].lmax-1)            return query(i<<1|1,t)+query(i<<1,mid);        else            return query(i<<1|1,t);    }}int main(){    int n,m;    int t;    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<maxn;i++)        {            tree[i].left=0;            tree[i].lmax=0;            tree[i].mmax=0;            tree[i].right=0;            tree[i].rmax=0;        }        build(1,1,n);        stack<int > ss;        for(int i=1;i<=m;i++)        {            char s;            scanf(" %c",&s);            if(s=='D')            {                int mid;                scanf("%d",&mid);                updateregion(1,mid,mid,0);                ss.push(mid);                //show(1,1,n);                //printf("\n");            }            else if(s=='R')            {                int mid;                mid=ss.top();                ss.pop();                updateregion(1,mid,mid,1);                //show(1,1,10);            }            else            {                int mid;                scanf("%d",&mid);                ans=0;                printf("%d\n",query(1,mid));            }        }    }}