HDU-1540 Tunnel Warfare 【线段树+单点修改+区间更新】

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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
 

 

Sample Output
1
0
2
4
 

 
思路:
一道感觉并不是很难的线段树,菜鸡看着别人的题解按自己的习惯勉强写完的,真的菜啊。
lm[o], rm[o], sum[o],分别代表区间o左、右端点开始的最长连续区间,该区间内的最长连续区间。
 
Code:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define INF 0x3f3f3f3f
 4 #define M(a, b) memset(a, b, sizeof(a))
 5 #define lson o<<1
 6 #define rson o<<1|1
 7 const int maxn = 50000 + 10;
 8 int lm[maxn<<2], rm[maxn<<2], sum[maxn<<2], v, p;
 9 int S[maxn], top;
10 
11 void build(int o, int L, int R) {
12     lm[o] = rm[o] = sum[o] = R - L + 1;
13     if (L == R) return;
14     else {
15         int M = (L + R) >> 1;
16         build(lson, L, M);
17         build(rson, M+1, R);
18     }
19 }
20 
21 void update(int o, int L, int R) {
22     int M = (L + R) >> 1;
23     if (L == R) {
24         lm[o] = rm[o] = sum[o] = v;
25         return;
26     }
27     else {
28         if (p <= M) update(lson, L, M);
29         else update(rson, M+1, R);
30     }
31     lm[o] = lm[lson]; rm[o] = rm[rson];
32     sum[o] = max(rm[lson]+lm[rson], max(sum[lson], sum[rson]));
33     if (lm[lson] == M - L + 1) lm[o] += lm[rson];
34     if (rm[rson] == R - M) rm[o] += rm[lson];
35 }
36 
37 int query(int o, int L, int R, int x) {
38     if (L == R || sum[o] == 0 || sum[o] == R - L + 1) return sum[o];
39     else {
40         int M = (L + R) >> 1;
41         if (x <= M) {
42             if (x >= M - rm[lson] + 1) 
43                 return query(lson, L, M, x) + query(rson, M+1, R, M+1);
44             else return query(lson, L, M, x);
45         }
46         else {
47             if (x <= M + lm[rson]) 
48                 return query(rson, M+1, R, x) + query(lson, L, M, M);
49             else return query(rson, M+1, R, x);
50         }
51     }
52 }
53 
54 int main() {
55     int n, m;
56     while(~scanf("%d%d", &n, &m)) {
57         build(1, 1, n);
58         char op[3];
59         top = 0;
60         while(m--) {
61             scanf("%s", op);
62             if (op[0] == 'D') {
63                 scanf("%d", &p);
64                 S[top++] = p;
65                 v = 0;
66                 update(1, 1, n);
67             }
68             else if (op[0] == 'R') {
69                 if (top == 0) continue;
70                 p = S[--top];
71                 v = 1;
72                 update(1, 1, n);
73             }
74             else {
75                 scanf("%d", &p);
76                 printf("%d\n", query(1, 1, n, p));
77             }
78         }
79     }
80 
81     return 0;
82 }

转载于:https://www.cnblogs.com/robin1998/p/6505712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值