poj 2892 Tunnel Warfare(线段树)

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7499   Accepted: 3096

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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO

【题意】

       一排村庄,需要操作:D(x)破坏村庄x,R回复最后破化的村庄,Q(x)与x相连的最长村庄数。

【思路】

       线段树。

       01表示村庄是否存在。维护ls rs ms三个信息。

       Query:

    如果x位于左子,如果x位于左子的rs中则需要查询右子中与M+1相连的最长段,即query(u<<1,x)+query(u<<1|1,M+1);如果不处于rs中直接返回query(u<<1,x),X位于右子同理。

【代码】

 1 #include<cstdio>
 2 #include<iostream>
 3 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 4 using namespace std;
 5 
 6 const int N = 1e5+10;
 7 
 8 struct Trie{ int l,r,ls,rs,ms; }T[N<<1];
 9 
10 void maintain(int u) {
11     int lc=u<<1,rc=lc|1;
12     T[u].ls=T[lc].ls,T[u].rs=T[rc].rs;
13     if(T[lc].ls==T[lc].r-T[lc].l+1) T[u].ls+=T[rc].ls;
14     if(T[rc].rs==T[rc].r-T[rc].l+1) T[u].rs+=T[lc].rs;
15     T[u].ms=max(T[lc].ms,T[rc].ms);
16     T[u].ms=max(T[u].ms,T[lc].rs+T[rc].ls);
17 }
18 void build(int u,int L,int R) {
19     T[u].l=L,T[u].r=R;
20     if(L==R) {
21         T[u].ls=T[u].rs=T[u].ms=R-L+1;
22         return ;
23     }
24     int M=(L+R)>>1;
25     build(u<<1,L,M); build(u<<1|1,M+1,R);
26     maintain(u);
27 }
28 void update(int u,int r,int x) {
29     if(T[u].l==T[u].r)
30         T[u].ms=T[u].ls=T[u].rs=x;
31     else {
32         int M=(T[u].l+T[u].r)>>1;
33         if(r<=M) update(u<<1,r,x);
34         else update(u<<1|1,r,x);
35         maintain(u);
36     }
37 }
38 int query(int u,int x) {
39     if(T[u].l==T[u].r || T[u].ms==0 || T[u].ms==T[u].r-T[u].l+1) 
40         return T[u].ms;
41     int M=(T[u].l+T[u].r)>>1,lc=u<<1,rc=lc|1;
42     if(x<=M) {
43         if(x>=T[lc].r-T[lc].rs+1) 
44             return query(lc,x)+query(rc,M+1);
45         else return query(lc,x);
46     } else {
47         if(x<=T[rc].l+T[rc].ls-1) 
48             return query(rc,x)+query(lc,M);
49         else return query(rc,x);
50     }
51 }
52 
53 int n,m,s[N],top,f[N];
54 char op[5];
55 void read(int& x) {
56     char c=getchar(); int f=1; x=0;
57     while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();}
58     while(isdigit(c)) x=x*10+c-'0',c=getchar();
59     x*=f;
60 }
61 int main() {
62     read(n),read(m);
63     build(1,1,n);
64     int x;
65     while(m--) {
66         scanf("%s",op);
67         switch(op[0]) {
68             case 'D':
69                 read(x);update(1,x,0);s[++top]=x;f[x]=1;
70                 break;
71             case 'R':
72                 if(top)
73                     update(1,s[top],1),f[s[top]]=0,--top;
74                 break;
75             case 'Q':
76                 read(x);
77                 if(f[x]) puts("0");
78                 else printf("%d
",query(1,x));
79                 break;
80         }
81     }
82     return 0;
83 }
原文地址:https://www.cnblogs.com/lidaxin/p/5192513.html