本文共 2764 字,大约阅读时间需要 9 分钟。
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9156 | Accepted: 3782 |
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Hint
An illustration of the sample input:
OOOOOOOD 3 OOXOOOOD 6 OOXOOXOD 5 OOXOXXOR OOXOOXOR OOXOOOO
Source
//include#include #include using namespace std;const int N = 50000+5;typedef long long LL;int C[N+1];stack st;bool vis[N];int lowBit(int i){ return i&(-i);}LL getSum(int i){ LL ans = 0; while(i > 0){ ans += C[i]; i -= lowBit(i); } return ans;}void add(int i, int x){ while(i <= N){ C[i] += x; i += lowBit(i); }}int n,m;int main() { //freopen("C:/Users/zhangwei/Desktop/input.txt","r",stdin); scanf("%d%d",&n,&m); for(int i = 1; i <= n; ++i){ add(i,1); } char c[5]; int d; while(m--){ scanf("%s",c); if(c[0] == 'D'){ scanf("%d",&d); if(!vis[d]){ add(d,-1); st.push(d); vis[d] = true; } } else if(c[0] == 'R'){ if(!st.empty()){ int t = st.top(); st.pop(); add(t,1); vis[t] = false; } } else{ scanf("%d",&d); if(vis[d]){ printf("0\n"); continue; } int rs,re,l,r,t; l = d; r = n; while(l <= r){ int mid = (l+r)/2; if(getSum(mid) - getSum(d-1) == mid-d+1){ t = mid; l = mid+1; } else{ r = mid-1; } } re = t; l = 1; r = d; while(l <= r){ int mid = (l+r)/2; if(getSum(d) - getSum(mid-1) == d-mid+1){ r = mid-1; t = mid; } else{ l = mid+1; } } rs = t; printf("%d\n",re-rs+1); } } return 0; }
转载地址:http://peimi.baihongyu.com/