标签:des style http color os io strong ar for
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 6972 | Accepted: 2864 |
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
#include<algorithm> #include<iostream> #include<stdio.h> #include<stack> #include<cstring> using namespace std; int n,m; int c[50005],d[50005]; stack <int> st; int lowbit(int x){ return x&-x; } int sum(int x){ int ret=0; while(x>=1){ ret+=c[x]; x-=lowbit(x); } return ret; } void add(int x,int val){ while(x<=n){ c[x]+=val; x+=lowbit(x); } } int sol(int x){ int left,right,mid; int ans=n+1; left=0; right=n+1; //注意这里的ans,left和right的初始值 while(left<=right){ mid=(left+right)>>1; if(sum(mid)>=x){ ans=mid; right=mid-1; } else left=mid+1; } return ans; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); while(!st.empty()) st.pop(); while(m--){ char s[2]; scanf("%s",s); if(s[0]=='D'){ int x; scanf("%d",&x); if(d[x]){ continue; } else{ st.push(x); d[x]=1; add(x,1); } } else if(s[0]=='Q'){ int x; scanf("%d",&x); if(d[x]){ printf("0\n"); } else{ int sm=sum(x); int left,right; left=sol(sm); right=sol(sm+1); //printf("%d %d\n",left,right); printf("%d\n",right-left-1); } } else{ if(st.empty()){ continue; } else{ d[st.top()]=0; add(st.top(),-1); st.pop(); } } } } return 0; }
标签:des style http color os io strong ar for
原文地址:http://blog.csdn.net/my_acm/article/details/38904551