Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10962 Accepted Submission(s): 4305
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!
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.
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
有很多细节的一个题,磨了足足三天,ac之后对基础线段树又加深了见解, 还是太菜了,继续加油吧
#include<stack>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define mms(x, y) memset(x, y, sizeof x)
#define MAX 50005
#define INF 0x3f3f3f3f
int mx[MAX << 2], mn[MAX << 2];
int n, m;
stack <int> s;
void PushUp(int rt)
{
mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]);
}
void BuildTree(int rt, int l, int r)
{
if(l == r)
{
mx[rt] = 0;
mn[rt] = INF;
return;
}
int mid = (l + r) >> 1;
BuildTree(lson);
BuildTree(rson);
PushUp(rt);
}
void Update(int rt, int l, int r, int i, int val)
{
if(l == r)
{
if(val == 0)
{
mx[rt] = 0;
mn[rt] = INF;
return;
}
else
{
mx[rt] = mn[rt] = val;
return;
}
}
int mid = (l + r) >> 1;
if(i <= mid)
Update(lson, i, val);
if(i > mid)
Update(rson, i, val);
PushUp(rt);
}
int QueryA(int rt, int l, int r, int x, int y)
{
if(l >= x && y >= r)
return mx[rt];
int mid = (l + r) >> 1, ans = 0;
if(mid >= x)
ans = max(QueryA(lson, x, y), ans);
if(y > mid)
ans = max(QueryA(rson, x, y), ans);
return ans;
}
int QueryI(int rt, int l, int r, int x, int y)
{
if(l >= x && y >= r)
return mn[rt];
int mid = (l + r) >> 1, ans = INF;
if(mid >= x)
ans = min(QueryI(lson, x, y), ans);
if(y > mid)
ans = min(QueryI(rson, x, y), ans);
return ans;
}
void init()
{
mms(mx, 0);
mms(mn, 0);
BuildTree(1, 1, n);
while(!s.empty())
s.pop();
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
char ss[5];
int a;
init();
while(m--)
{
scanf("%s", ss);
if(ss[0] == 'D')
{
scanf("%d", &a);
s.push(a);
Update(1, 1, n, a, a);
}
if(ss[0] == 'R')
{
a = s.top();
s.pop();
Update(1, 1, n, a, 0);
}
if(ss[0] == 'Q')
{
scanf("%d", &a);
if(QueryA(1, 1, n, 1, a) == a && QueryI(1, 1, n, a, n) == a)
printf("0\n");
else
{
int l = a - QueryA(1, 1, n, 1, a);
int r;
if(QueryI(1, 1, n, a, n) == INF)
r = n - a + 1;
else
r = QueryI(1, 1, n, a, n) - a;
printf("%d\n", l + r - 1);
}
}
}
}
return 0;
}