HDU 1540 Tunnel Warfare 线段树区间合并
来源:互联网 发布:sqlserver恢复表数据 编辑:程序博客网 时间:2024/06/08 04:26
http://acm.hdu.edu.cn/showproblem.php?pid=1540
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem 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!
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:
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.
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
思路:线段树区间合并(学了一上午才会)
AC代码:
#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<stack>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod2 = 1e9+7;const int INF = 1e8;const int inf = 0x3f3f3f3f;const int maxx = 50010;struct node{ int l,r;//l表示在当前区间从左往右数有多少个好村庄相连 直到数到坏村庄停止,r同理。} t[maxx*4];void build(int k,int l,int r){ t[k].r=t[k].l=r-l+1; if(l==r) return ; int m=(l+r)>>1; build(k<<1,l,m); build(k<<1|1,m+1,r);}void up(int k,int p){ int k1=k<<1,k2=k<<1|1; t[k].l=t[k1].l; t[k].r=t[k2].r; if(t[k].l==p-(p>>1)) t[k].l+=t[k2].l; if(t[k].r==p>>1) t[k].r+=t[k1].r;}void Insert(int k,int l,int r,int v,int id){ if(l==r) { t[k].l=t[k].r=v; return ; } int m=(l+r)>>1; if(id<=m) Insert(k<<1,l,m,v,id); else Insert(k<<1|1,m+1,r,v,id); up(k,r-l+1);}int ans;void Query(int k,int l,int r,int id){ if(l==r) { ans=t[k].l; return ; } int m=(l+r)>>1; int k1=k<<1,k2=k<<1|1; if(id<=m) { Query(k1,l,m,id); if(m+1-t[k1].r<=id) ans+=t[k2].l; } else { Query(k2,m+1,r,id); if(m+t[k2].l>=id) ans+=t[k1].r; }}stack<int> s;int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { while(!s.empty()) s.pop(); build(1,1,n); char c[10]; int a; while(m--) { scanf("%s",c); if(c[0]=='D') { scanf("%d",&a); s.push(a); Insert(1,1,n,0,a); } else if(c[0]=='Q') { ans=0; scanf("%d",&a); Query(1,1,n,a); printf("%d\n",ans); } else { if(!s.empty()) { a=s.top(); s.pop(); Insert(1,1,n,1,a); } } } }}
阅读全文
0 0
- HDU-1540 Tunnel Warfare 线段树 区间合并
- hdu 1540 Tunnel Warfare(线段树区间合并)
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- 【Hdu】1540 Tunnel Warfare(线段树|区间合并)
- HDU 1540 Tunnel Warfare(线段树 区间合并 +单点更新)
- hdu 1540/POJ 2892 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 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- 整数排序 II
- Image Retrieval
- Java-Eclipse导入maven项目时,Pom.xml文件报错处理方法
- ARM cortex-A8 学习笔记(3) —— ARM 汇编(上)
- 将博客搬至CSDN
- HDU 1540 Tunnel Warfare 线段树区间合并
- redis.conf配置详细解析
- KMP 精髓
- 关于Maven项目pom.xml文件不报错却有红叉的问题
- Windows及Linux系统下查看进程、端口、资源情况
- 多线程-匿名内部类的方式实现多线程程序
- 欢迎使用CSDN-markdown编辑器
- PHP之常用的函数总结
- Python练习题 10-13 验证用户