Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 2814 | Accepted: 1044 |
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:
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
Source
用SET做,这个程序是另外大牛的,实践是422MS,用C++编译
#include
#include
using namespace std;
int stak[50000];
int main()
{
int n,m,pos,count;
char cmd;
set
scanf("%d%d",&n,&m);
mp.insert(0);
mp.insert(n+1);
count=0;
while(m--)
{
scanf(" %c",&cmd);
if(cmd=='D')
{
scanf("%d",&pos);
mp.insert(pos);
stak[count++]=pos;
}
if(cmd=='Q')
{
scanf("%d",&pos);
if(*mp.lower_bound(pos)==pos)
printf("0/n");
else
{
set
STI = mp.lower_bound(pos);
printf("%d/n",*STI-*(--STI)-1);
}
}
if(cmd=='R')
{
mp.erase(mp.find(stak[--count]));
}
}
return 0;
}
用线段树是200多MS,YEAH?!
#include
#include
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
int stack[50010],n,m;
struct Seg_Tree
{
int l,r;
int cval,lval,rval;
void doit(int st) { cval=lval=rval=( st==1? 0 : dis() ) ;}
int dis() { return (r-l+1) ;}
int mid() { return (l+r)/2 ;}
}tree[50010*3];
int max(int a,int b)
{
if(a>b) return a;
return b;
}
void build(int left,int right,int idx)
{
tree[idx].l=left;
tree[idx].r=right;
tree[idx].doit(0);
if(left==right) return;
int mid=tree[idx].mid();
build(left,mid,LL(idx));
build(mid+1,right,RR(idx));
}
void update(int x,int st,int idx)
{
if(tree[idx].l==tree[idx].r)
{
tree[idx].doit(st);
return ;
}
int mid=tree[idx].mid();
if(x<=mid) update(x,st,LL(idx));
else update(x,st,RR(idx));
tree[idx].cval=max(tree[LL(idx)].rval+tree[RR(idx)].lval,
max(tree[LL(idx)].cval,tree[RR(idx)].cval));
tree[idx].lval=tree[LL(idx)].lval;
tree[idx].rval=tree[RR(idx)].rval;
if(tree[LL(idx)].cval==tree[LL(idx)].dis()) tree[idx].lval+=tree[RR(idx)].lval;
if(tree[RR(idx)].cval==tree[RR(idx)].dis()) tree[idx].rval+=tree[LL(idx)].rval;
}
int query(int x,int idx)
{
if(tree[idx].l==tree[idx].r || tree[idx].cval==0 || tree[idx].cval==tree[idx].dis() )
{
return tree[idx].cval;
}
int mid=tree[idx].mid();
if(x<=mid)
{
if(x>tree[LL(idx)].r-tree[LL(idx)].rval)
return query(x,LL(idx))+query(tree[RR(idx)].l,RR(idx));
else
return query(x,LL(idx));
}
else
{
if(x return query(tree[LL(idx)].r,LL(idx))+query(x,RR(idx)); else return query(x,RR(idx)); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(tree,0,sizeof(tree)); memset(stack,0,sizeof(stack)); int num=0; build(1,n,1); for(int i=1;i<=m;i++) { char ch[2]; scanf("%s",&ch); if(ch[0]=='D') { int x; scanf("%d",&x); update(x,1,1); stack[num++]=x; } else if(ch[0]=='Q') { int x; scanf("%d",&x); printf("%d/n",query(x,1)); } else { if(num==0) continue; update(stack[--num],0,1); } } } return 0; }