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
1:求变化的序列的连续值
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 510005
#define MAXM 164444
#define INF 100000000
#define eps 1e-7
#define L(X) X<<1
#define R(X) X<<1|1
using namespace std;
const int maxn = 50000 + 10;
struct Tree
{
int l,r;
int lm,rm,mx;
} tree[maxn * 4];
void build(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].lm = tree[rt].rm = tree[rt].mx = r-l+1;
int mid = (l+r)>>1;
if(l!=r)
{
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
}
void update(int rt,int x,int flag)
{
if(tree[rt].l==tree[rt].r)
{
if(flag==1)
tree[rt].lm = tree[rt].rm = tree[rt].mx = 0;
else tree[rt].lm = tree[rt].rm = tree[rt].mx = 1;
return;
}
int mid = (tree[rt].l + tree[rt].r)>>1;
if(mid>=x)
update(rt<<1,x,flag);
else update(rt<<1|1,x,flag);
tree[rt].lm = tree[rt<<1].lm;
tree[rt].rm = tree[rt<<1|1].rm;
tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx);
if(tree[rt].lm == tree[rt<<1].r - tree[rt<<1].l+1)
tree[rt].lm +=tree[rt<<1|1].lm;
if(tree[rt].rm == tree[rt<<1|1].r - tree[rt<<1|1].l+1)
tree[rt].rm += tree[rt<<1].rm;
tree[rt].mx = max(tree[rt].mx,tree[rt<<1].rm + tree[rt<<1|1].lm);
}
int query(int rt,int x)
{
if(tree[rt].l==tree[rt].r || tree[rt].r - tree[rt].l+1 == tree[rt].mx || tree[rt].mx==0)
return tree[rt].mx;
int mid = (tree[rt].l + tree[rt].r)>>1;
if(mid>=x)
{
if(x>= mid - tree[rt<<1].rm + 1)
return query(rt<<1,x) + query(rt<<1|1,mid+1);
else return query(rt<<1,x);
}else
{
if(x<= mid + tree[rt<<1|1].lm)
return query(rt<<1|1,x) + query(rt<<1,mid);
else return query(rt<<1|1,x);
}
}
int sta[52000];
int tot=0;
char st[5];
int a;
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,1,n);
tot=0;
while(m--)
{
scanf("%s",st);
if(st[0]=='D')
{
scanf("%d",&a);
update(1,a,1);
sta[tot++] = a;
}else if(st[0]=='Q')
{
scanf("%d",&a);
int ans = query(1,a);
printf("%d\n",ans);
}else if(st[0]=='R')
{
tot--;
update(1,sta[tot],0);
}
}
}
return 0;
}
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