博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2892 Tunnel Warfare
阅读量:6825 次
发布时间:2019-06-26

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

 

 

Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u

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:

  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:

OOOOOOO D 3   OOXOOOO D 6   OOXOOXO D 5   OOXOXXO R     OOXOOXO R     OOXOOOO

 

 

用treap树存储所有被摧毁的村子,查询村子x的时候,只要在树里查离x最近的被摧毁的村子,就知道联系范围了

 

1 /*by SilverN*/  2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 struct node{ 9 int l,r; 10 int num; 11 int rand; 12 }t[120000]; 13 int st[120000],top=0;//用栈存储已经摧毁的村子 14 int cnt=0; 15 int root=0; 16 int n,m; 17 int s,e;//查询左右边界 18 void lt(int &k){ 19 int now=t[k].r; 20 t[k].r=t[now].l; 21 t[now].l=k; 22 k=now; 23 return; 24 } 25 void rt(int &k){ 26 int now=t[k].l; 27 t[k].l=t[now].r; 28 t[now].r=k; 29 k=now; 30 return; 31 } 32 void insert(int &k,int x){ 33 if(!k){ 34 cnt++; 35 k=cnt; 36 t[k].num=x; 37 t[k].rand=rand(); 38 return; 39 } 40 if(x==t[k].num)return;//不会重复破坏 41 if(x
t[k].num){ 45 insert(t[k].r,x);//建成右子树 46 if(t[t[k].r].rand
t[t[k].r].rand){ 58 lt(k); 59 del(k,x); 60 } 61 return; 62 } 63 if(x
=x && t[k].num
s)s=t[k].num; 71 if(t[k].num
>ch; 83 if(ch=='D'){ //摧毁 84 scanf("%d",&x); 85 st[++top]=x; 86 insert(root,x); 87 } 88 if(ch=='R'){ //恢复 89 del(root,st[top--]); 90 } 91 if(ch=='Q'){ //查询 92 scanf("%d",&x); 93 s=0;e=n+1;//设置边界 94 find(root,x); 95 if(s==x && e==x)printf("0\n"); 96 else printf("%d\n",e-s-1); 97 } 98 } 99 return 0;100 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5561403.html

你可能感兴趣的文章
The run destination iPhone 5.0 Simulator is not valid for running the scheme 'MyApp'
查看>>
【C#】在父窗体菜单合并子窗体菜单
查看>>
反射(6)程序集加载上下文
查看>>
oracle触发器after update of |更改之后赋值|
查看>>
Oracle命令:授权-收回权限-角色-用户状态
查看>>
常用的Ubuntu APT命令参数
查看>>
成功加盟者的8个特点
查看>>
Java基础03 构造器与方法重载
查看>>
如何让你的服务屏蔽Shodan扫描
查看>>
SpringBoot+Elasticsearch
查看>>
Vim 操作符命令和动作命令
查看>>
动态代理
查看>>
C语言 格式化输出--%m.n
查看>>
gradle配置国内的镜像
查看>>
Gitlab安装与备份恢复
查看>>
Elasticsearch-sql 用SQL查询Elasticsearch
查看>>
(原創) 如何讓Nios II自動抓到自己寫的IP的HAL? (SOC) (Nios II) (SOPC Builder) (DE2-70)
查看>>
JFS技术详细介绍
查看>>
Linux VI command
查看>>
创建可重用的对象
查看>>