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  ```#include<bits/stdc++.h>
using namespace std;
const int maxn = 50010;
#define lson l , m , num << 1
#define rson m + 1 , r , num << 1|1
int n , m , k , maxo[maxn << 2] , mino[maxn << 2];char s;
void up(int num){maxo[num] = max(maxo[num << 1] , maxo[num << 1|1]); mino[num] = min(mino[num << 1] ,mino[num << 1|1]);}
void built(int l , int r , int num){
if(l == r) {maxo[num] = 0 ; mino[num] = n + 1;return;}
int m = (l + r)>>1;
built(lson);built(rson);
up(num);
return;
}
void des(int k , int l , int r , int num){
if(l == r){maxo[num] = k; mino[num] = k;return;}
int m = (l + r)>>1; if(k <= m) des(k,lson); if(k > m)des(k,rson);up(num);
}
void red(int k , int l , int r , int num){
if(l == r){maxo[num] = 0; mino[num] = n + 1;return;}
int m = (l + r)>>1; if(k <= m) red(k,lson); if(k > m)red(k,rson);up(num);
}
int getl(int L , int R , int l , int r , int num){
if(L <= l && r <= R){return maxo[num];}
int ans = 0;int m = (l + r) >> 1; if(L <= m) ans = max(ans , getl(L , R , lson));
if(R > m) ans = max(ans , getl(L , R , rson));
return ans;
}
int getr(int L , int R , int l , int r , int num){
if(L <= l && r <= R){return mino[num];}
int ans = n + 1;int m = (l + r) >> 1; if(L <= m) ans = min(ans , getr(L , R , lson));
if(R > m) ans = min(ans , getr(L , R , rson));
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
built(1 , n , 1);
stack<int>q;
while(m--){
scanf("%s",s);
if(s == ‘D‘){
scanf("%d",&k); des(k , 1 , n , 1);
q.push(k);
}
if(s == ‘R‘){
if(q.empty()){
continue;
}
k = q.top(); q.pop();
red(k , 1 , n , 1);
}
if(s == ‘Q‘){
scanf("%d",&k);
int ll = getl(1 , k , 1 , n , 1);
int rr = getr(k , n , 1 , n , 1);
int ans = rr - ll - 1;
if(ans < 0) ans = 0;
printf("%d\n",ans);
}
}
}
}```

hdu 1540 Tunnel Warfare 线段树 单点更新，查询区间长度，区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene

HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定 所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间. 注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间.反之同理. 查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询. 查询

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

hdu 2795 Billboard（线段树单点更新）

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14337    Accepted Submission(s): 6148 Problem Description At the entrance to the university, there is a huge rectangular billboard of