poj 2892
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 7725 | Accepted: 3188 |
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 nextm 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.
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<ctime>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int n,m,root=0,d[50100],st,ed,sz;
struct Treap{int rnd,v,w,l,r;}tr[80100];
inline void lturn(int &k)
{
int t=tr[k].r;
tr[k].r=tr[t].l;
tr[t].l=k;
k=t;
}
inline void rturn(int &k)
{
int t=tr[k].l;
tr[k].l=tr[t].r;
tr[t].r=k;
k=t;
}
inline void insert(int &w,int x)
{
if(w==0){
sz++;
w=sz;
tr[w].v=x;
tr[w].w=1;
tr[w].rnd=rand();
return;
}
if(tr[w].v==x){
tr[w].w++;
return;
}
if(tr[w].v>x){
insert(tr[w].l,x);
if(tr[tr[w].l].rnd<tr[w].rnd)rturn(w);
}
else{
insert(tr[w].r,x);
if(tr[tr[w].r].rnd<tr[w].rnd)lturn(w);
}
}
inline void del(int &w,int x)
{
if(tr[w].v==x){
if(tr[w].w>1){tr[w].w--;return;}
if(tr[w].l*tr[w].r==0)w=tr[w].l+tr[w].r;
else{
if(tr[tr[w].l].rnd<tr[tr[w].r].rnd){
rturn(w);
del(w,x);}
else{
lturn(w);
del(w,x);}
}
return;
}
else if(tr[w].v<x)del(tr[w].r,x);
else del(tr[w].l,x);
}
inline void find(int &w,int x)
{
if(w==0)return;
if(tr[w].v>=x&&tr[w].v<ed)ed=tr[w].v;
if(tr[w].v<=x&&tr[w].v>st)st=tr[w].v;
if(tr[w].v<x)find(tr[w].r,x);
else find(tr[w].l,x);
}
int main()
{
srand(time(0));
scanf("%d%d",&n,&m);
char od;
int a;
for(int i=1,j=0;i<=m;i++){
cin>>od;
if(od=='D'){
scanf("%d",&a);
insert(root,a);
j++;
d[j]=a;
}
if(od=='R'){
del(root,d[j]);
j--;
}
if(od=='Q'){
scanf("%d",&a);
st=0,ed=n+1;
find(root,a);
if(st==a&&ed==a)puts("0");
else printf("%d\n",ed-st-1);
}
}
return 0;
}
poj 2892的更多相关文章
- POJ 2892 Tunnel Warfare
传送门 很神奇的一道题,可以用线段树搞,为了练习treap所以拿treap写了. 其实根据询问,删除那个标号就加入平衡树,然后找到最大的和最小的就好了. 一些很烦人的小细节. //POJ 2892 / ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- HDU 1540 / POJ 2892 Tunnel Warfare (单点更新,区间合并,求包含某点的最大连续个数)
题意:一条线上有n个点,D x是破坏这个点,Q x是表示查询x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 思路:这题的关键是查询. 将被毁的村庄看成空位,当查询某个点的时候,如果我们知道它左 ...
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
Tunnel Warfare Time Limit: 4000/2000 MS ...
- poj 2892 &&hdu 1540 Tunnel Warfare
http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...
- poj 2892 Tunnel Warfare(线段树)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7499 Accepted: 3096 D ...
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...
- POJ 2892 Tunnel Warfare (SBT + stack)
题意:给定了初始的状态:有n个村庄连成一条直线,现在有三种操作: 1.摧毁一个村庄 2.询问某个村庄,输出与该村庄相连的村庄数量(包括自己) 3.修复被摧毁的村庄,优先修复最近被摧毁的........ ...
- HDU 1540 POJ 2892 Tunnel Warfare
线段树 区间合并 单点修改 区间查询.又是1秒钟构思,差错查了好久... ... 发现一个int型的定义成了char型,打脸. #include <stdio.h> #include &l ...
随机推荐
- jsp声称的java文件位置
想找到JSP生成的字节码文件还是Java文件,这得看你加载Web应用是自己配置的Tomcat还是加载到Eclipse默认路径下:先说Eclipse默认路径下的吧,其路径为:你的eclipse存放工程的 ...
- Android 视频播放器,在线播放
1. Bilibili https://github.com/Bilibili/ijkplayer 1.测试的时候总是崩溃,不知道是我不会用还是怎么回事. 2016-04-15 2.AndroidVi ...
- zookeeper入门讲解事例
zookeeper使用和原理探究(一) zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<Th ...
- Xcode 8.1 : Unable to read from device
今天升级了Xcode 8.1,准备在iOS10.0.2的iPhone 6 Plus上调试,提示:Unable to read from device. 查看文件路径:"~/Library/D ...
- 【代码笔记】iOS-创建具有中划线的文字
一, 效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m - (void)viewDidLoad { [super viewDidL ...
- socket编程中客户端常用函数 以及简单实现
1 常用函数 1.1 connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...
- Swift开发第十二篇——protocol组合&static和class
本篇分为两部分: 一.Swift 中 protocol 组合的使用 二.Swfit 中 static和class 的使用 一.Swift 中 protocol 组合的使用 在 Swift 中我们可以使 ...
- 使用eclipse创建spring mvc web工程并部署
创建maven工程
- svn黄色叹号解决
客户端是TortoiseSVN的话,在该文件或文件夹上点右键,选择TortoiseSVN——revert
- json数组的序列化和反序列化json数组的序列化和反序列化
如题,我就不多说了,自己看代码的,很好理解 using System; using System.Collections.Generic; using System.Web; using System ...