Tunnel Warfare HDU
来源:互联网 发布:剑灵男灵剑士捏脸数据 编辑:程序博客网 时间:2024/06/06 09:51
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!
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!
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.
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
1024
题意 n个村庄,m次操作,n个村庄相连 。D 代表破坏此村庄 Q 为询问 此村庄相连的村庄(包括本身)有几个 R为修复上一个被破坏的村庄。。
思路 : 遇到Q时,一定是从这个村庄开始往左到哪里,往右到哪里,计算这个长度。
可以用set 先把0与n+1让入set中,遇到D时,把这个村庄放入set中,R时把上一个村庄从set中删除。
Q时在set中查找到大于这个村庄的值l,找到前一个小于这个村庄的值r,l-r-1为此区间的值,若正好在set中找到此村庄,则证明这个村庄被破坏,输出0;
#include<iostream>#include<cstring>#include<algorithm>#include<stack>#include<set>using namespace std;int n,m;int main(){ char s; int l; int t; while(cin>>n>>m) { l=0; stack<int> ss; set<int> aa; aa.insert(0); aa.insert(n+1); for(int i=0;i<m;i++) { cin>>s; if(s=='D') { cin>>t; ss.push(t); aa.insert(t); } if(s=='R') { int a=ss.top(); ss.pop(); aa.erase(a); } if(s=='Q') { cin>>t; int f=0; int l,r; set<int>::iterator ite; for(ite=aa.begin();ite!=aa.end();ite++) { if(*ite==t) { f=1; cout<<"0"<<endl; break; } if(*ite>t) { l=*ite; ite--; r=*ite; break; } } if(f==0) { cout<<l-r-1<<endl; } } } } return 0;}
阅读全文
0 0
- poj hdu Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU - 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU.1540 Tunnel Warfare
- HDU.1540 Tunnel Warfare
- 收集Android方方面面的经典知识, 最新技术.
- android广告SDK原理详解(附源码)
- 【HDU 6127 Hard challenge】& 斜率
- 为什么越来越多的公司在使用Spark Streaming
- 菜鸟Python(3)
- Tunnel Warfare HDU
- 机器学习(一)简介
- CentOS 7安装Tomcat 7
- Spring同名bean覆盖问题
- java VisualVM远程连接Tomcat
- jquery validate
- 机器学习(二)基本概念
- idea消息框等界面中文乱码显示成小方块
- 【Hibernate】(三)持久化对象的生命周期