HDU

来源:互联网 发布:linux vi 批量删除 编辑:程序博客网 时间:2024/06/01 17:27

Tunnel Warfare

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


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


题意:有好几个村庄 D表示破坏村庄 R表示修复村庄 Q表示包括x的最大区间


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <vector>#define max_ 50010#define inf 0x3f3f3f3f#define ll long longusing namespace std;struct node{int l,r;int ls,rs,ms;//ls,左端最大连续区间,rs右端最大连续区间,ms区间内最大连续区间  };struct node tree[max_*4];int n,m;stack<int> s;void build(int i,int l,int r){tree[i].l=l;tree[i].r=r;tree[i].ls=tree[i].rs=tree[i].ms=r-l+1;if(l==r)return;int mid=(l+r)>>1;build(i<<1,l,mid);build(i<<1|1,mid+1,r);}void update(int i,int x,int v){if(tree[i].l==x&&tree[i].r==x){tree[i].ls=tree[i].rs=tree[i].ms=v;return;}int mid=(tree[i].l+tree[i].r)>>1;if(x<=mid)update(i<<1,x,v);elseupdate(i<<1|1,x,v);tree[i].ls=tree[i<<1].ls;//左区间tree[i].rs=tree[i<<1|1].rs;//右区间tree[i].ms=max(max(tree[i<<1].ms,tree[i<<1|1].ms),tree[i<<1].rs+tree[i<<1|1].ls);//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值if(tree[i<<1].ls==tree[i<<1].r-tree[i<<1].l+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间tree[i].ls+=tree[i<<1|1].ls;if(tree[i<<1|1].rs==tree[i<<1|1].r-tree[i<<1|1].l+1)tree[i].rs+=tree[i<<1].rs;}int query(int i,int x){if(tree[i].l==tree[i].r||tree[i].ms==0||tree[i].ms==tree[i].r-tree[i].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了return tree[i].ms;int mid=(tree[i].l+tree[i].r)>>1;if(x<=mid){if(x>=tree[i<<1].r-tree[i<<1].rs+1)//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值//如果t在左子树的右区间内,则要看右子树的左区间有多长并返回return query(i<<1,x)+query(i<<1|1,mid+1);elsereturn query(i<<1,x);//如果不在左子树的右边界区间内,则只需要看左子树  }else{if(x<=tree[i<<1|1].l+tree[i<<1|1].ls-1)return query(i<<1,mid)+query(i<<1|1,x);elsereturn query(i<<1|1,x);}}int main(int argc, char const *argv[]){while(scanf("%d%d",&n,&m)!=EOF){while(s.size())s.pop();memset(tree,0,sizeof(tree));build(1,1,n);while(m--){char c;int x;scanf(" %c",&c);if(c=='D'){scanf("%d",&x);update(1,x,0);s.push(x);}else if(c=='Q'){scanf("%d",&x);printf("%d\n",query(1,x));}else if(c=='R'){update(1,s.top(),1);s.pop();}}}return 0;}


原创粉丝点击