博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2892 树状数组 + 二分
阅读量:4217 次
发布时间:2019-05-26

本文共 2764 字,大约阅读时间需要 9 分钟。

Tunnel Warfare
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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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 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/

你可能感兴趣的文章
STemWin学习笔记——TTF格式字体显示
查看>>
STemWIn学习笔记——汉字显示(外部存储器)
查看>>
Python学习笔记——Python提高-2
查看>>
Python学习笔记——多任务-进程
查看>>
Python学习笔记——多任务-线程
查看>>
Vim学习笔记——前言
查看>>
Vim学习笔记——小试牛刀
查看>>
Vim学习笔记——帮助
查看>>
Python学习笔记——网络通信过程
查看>>
Python学习笔记——正则表达式
查看>>
Python学习笔记——数据结构与算法
查看>>
Python学习笔记——顺序表
查看>>
Python学习笔记——链表
查看>>
MarkDown学习笔记——语法
查看>>
Python学习笔记——栈和队列
查看>>
Python学习笔记——排序与搜索
查看>>
Python学习笔记——爬虫之BeautifulSoup4数据提取
查看>>
Python学习笔记——爬虫的思路总结
查看>>
Python学习笔记——爬虫之动态HTML处理和机器图像识别
查看>>
Python学习笔记——爬虫之执行JavaScript语句与训练Tesseract
查看>>