HDU 1540 Tunnel Warfare

Tunnel Warfare

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1540
64-bit integer IO format: %I64d      Java class name: Main
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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Source

 
解题:线段树的区间合并。。。
 
关键是合并的时候看怎么合并
 
如果在左儿子的右连续区间中,那么一定要加上右儿子的左连续区间
 
如果在右儿子的左连续区间中,那么一定要加上左儿子的右连续区间
 
 1 #include <bits/stdc++.h>
 2 using  namespace std;
 3 const int maxn = 50010;
 4 struct node {
 5     int lsum,rsum;
 6 } tree[maxn<<2];
 7 void build(int L,int R,int v) {
 8     tree[v].lsum = tree[v].rsum = R - L + 1;
 9     if(L == R) return;
10     int mid = (L + R)>>1;
11     build(L,mid,v<<1);
12     build(mid+1,R,v<<1|1);
13 }
14 void pushup(int v,int k) {
15     tree[v].lsum = tree[v<<1].lsum;
16     tree[v].rsum = tree[v<<1|1].rsum;
17     if(tree[v].lsum == k - (k>>1)) tree[v].lsum += tree[v<<1|1].lsum;
18     if(tree[v].rsum == (k>>1)) tree[v].rsum += tree[v<<1].rsum;
19 }
20 void update(int L,int R,int id,int val,int v) {
21     if(L == R) {
22         tree[v].lsum = tree[v].rsum = val;
23         return;
24     }
25     int mid = (L + R)>>1;
26     if(id <= mid) update(L,mid,id,val,v<<1);
27     if(id > mid) update(mid+1,R,id,val,v<<1|1);
28     pushup(v,R - L + 1);
29 }
30 int ans,tot,stk[maxn];
31 void query(int L,int R,int id,int v) {
32     if(L == R) {
33         ans = tree[v].lsum;
34         return;
35     }
36     int mid = (L + R)>>1;
37     if(id <= mid) {
38         query(L,mid,id,v<<1);
39         if(id >= mid - tree[v<<1].rsum + 1)
40             ans += tree[v<<1|1].lsum;
41     }
42     if(id > mid) {
43         query(mid+1,R,id,v<<1|1);
44         if(id <= mid + tree[v<<1|1].lsum)
45             ans += tree[v<<1].rsum;
46     }
47 }
48 int main() {
49     int n,m,x;
50     char op[10];
51     while(~scanf("%d %d",&n,&m)) {
52         build(1,n,1);
53         while(m--) {
54             scanf("%s",op);
55             if(op[0] == 'Q') {
56                 ans = 0;
57                 scanf("%d",&x);
58                 query(1,n,x,1);
59                 printf("%d\n",ans);
60             } else if(op[0] == 'D') {
61                 scanf("%d",&x);
62                 stk[tot++] = x;
63                 update(1,n,x,0,1);
64             } else if(op[0] == 'R' && tot)
65                 update(1,n,stk[--tot],1,1);
66         }
67     }
68     return 0;
69 }
View Code

 

posted @ 2015-04-25 22:36  狂徒归来  阅读(158)  评论(0编辑  收藏  举报