HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
来源:互联网 发布:淘宝退货一般多久到账 编辑:程序博客网 时间:2024/06/03 22:05
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!
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.
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
这题自问我自己是做不出来。只能上网这了别人的代码看, 看了好久才明白其中的道理,讲解就交个大神的代码吧!
转载代码出处http://blog.csdn.net/libin56842/article/details/14105071
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stdlib.h> using namespace std; const int maxn = 50000+10; int n,m; int s[maxn],top;//s为模拟栈 struct node { int l,r; int ls,rs,ms;//ls,左端最大连续区间,rs右端最大连续区间,ms区间内最大连续区间 } a[maxn<<2]; void init(int l,int r,int i) { a[i].l = l; a[i].r = r; a[i].ls = a[i].rs = a[i].ms = r-l+1; if(l!=r) { int mid = (l+r)>>1; init(l,mid,i*2); init(mid+1,r,2*i+1); } } void insert(int i,int t,int x) { if(a[i].l == a[i].r) { if(x==1) a[i].ls = a[i].rs = a[i].ms = 1;//修复 else a[i].ls = a[i].rs = a[i].ms = 0;//破坏 return ; } int mid = (a[i].l+a[i].r)>>1; if(t<=mid) insert(2*i,t,x); else insert(2*i+1,t,x); a[i].ls = a[2*i].ls;//左区间 a[i].rs = a[2*i+1].rs;//右区间 a[i].ms = max(max(a[2*i].ms,a[2*i+1].ms),a[2*i].rs+a[2*i+1].ls);//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值 if(a[2*i].ls == a[2*i].r-a[2*i].l+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间 a[i].ls += a[2*i+1].ls; if(a[2*i+1].rs == a[2*i+1].r-a[2*i+1].l+1)//同理 a[i].rs += a[2*i].rs; } int query(int i,int t) { if(a[i].l == a[i].r || a[i].ms == 0 || a[i].ms == a[i].r-a[i].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了 return a[i].ms; int mid = (a[i].l+a[i].r)>>1; if(t<=mid) { if(t>=a[2*i].r-a[2*i].rs+1)//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回 return query(2*i,t)+query(2*i+1,mid+1); else return query(2*i,t);//如果不在左子树的右边界区间内,则只需要看左子树 } else { if(t<=a[2*i+1].l+a[2*i+1].ls-1)//同理 return query(2*i+1,t)+query(2*i,mid); else return query(2*i+1,t); } } int main() { int i,j,x; char ch[2]; while(~scanf("%d%d",&n,&m)) { top = 0; init(1,n,1); while(m--) { scanf("%s",ch); if(ch[0] == 'D') { scanf("%d",&x); s[top++] = x; insert(1,x,0); } else if(ch[0] == 'Q') { scanf("%d",&x); printf("%d\n",query(1,x)); } else { if(x>0) { x = s[--top]; insert(1,x,1); } } } } return 0; }
外加我的高仿版代码
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define N 50000using namespace std;struct no{ int ls; int rs; int ms;}st[N << 2];void build(int node, int l, int r);void updata(int node, int l, int r, int idx, int add);int query(int node, int l, int r, int idx);int main(){ int n, m; while(scanf("%d%d", &n, &m) == 2) { build(1, 1, n); char ch; int x; int a[N]; int top = -1; while(m--) { scanf(" %c", &ch); if(ch == 'D') { scanf("%d", &x); a[++top] = x; updata(1, 1, n, x, 0); } else if(ch == 'Q') { scanf("%d", &x); printf("%d\n", query(1, 1, n, x)); } else { updata(1, 1, n, a[top--], 1); } } } return 0;}void build(int node, int l, int r){ st[node].ls = st[node].rs = st[node].ms = r - l + 1; if(l != r) { int mid = (l + r) >> 1; build(node << 1, l, mid); build(node << 1 | 1, mid + 1, r); }}void updata(int node, int l, int r, int idx, int add){ if(l == r) { st[node].ls = st[node].rs = st[node].ms = add; return; } int mid = (l + r) >> 1; if(idx <= mid) { updata(node << 1, l, mid, idx, add); } else { updata(node << 1 |1, mid + 1, r, idx, add); } st[node].ls = st[node << 1].ls; st[node].rs = st[node << 1 | 1].rs; st[node].ms = max(max(st[node << 1].ms, st[node << 1 | 1].ms), st[node << 1].rs + st[node << 1 | 1].ls); if(st[node << 1].ls == mid - l + 1) { st[node].ls += st[node << 1 | 1].ls; } if(st[node << 1 | 1].rs == r - mid) { st[node].rs += st[node << 1].rs; }}int query(int node, int l, int r, int idx){ if(l == r || st[node].ms == 0 || st[node].ms == r - l + 1) { return st[node].ms; } int mid = (l + r) >> 1; if(idx <= mid) { if(idx >= mid - st[node << 1].rs + 1) { return query(node << 1, l, mid, idx) + query(node << 1 | 1, mid + 1, r, mid + 1); } else { return query(node << 1, l, mid , idx); } } else { if(idx <= mid + st[node << 1 | 1].ls) { return query(node << 1 | 1, mid + 1, r, idx) + query(node << 1, l, mid, mid); } else { return query(node << 1 | 1, mid + 1, r, idx); } }}
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- HDU1540:Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare 线段树区间合并
- HDU1540-Tunnel Warfare-线段树区间合并
- hdu1540 Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare (线段树区间合并)
- hdu1540-Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare(线段树,区间合并)
- HDU1540 Tunnel Warfare (线段树区间合并)
- HDU1540 Tunnel Warfare(线段树+区间合并)
- HDU1540 Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare(线段树区间合并详解)
- HDU1540-Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare(线段树)
- poj2892&&HDU1540 Tunnel Warfare(线段树)
- Tunnel Warfare(线段树区间合并问题)
- hdu1540 Tunnel Warfare 线段树中等难题
- poj2892 同hdu1540 Tunnel Warfare 线段树
- C/C++,用0x3f3f3f3f表示无穷大,0xc0c0c0c0表示无穷小
- (reading)Deep Visual-Semantic Alignments for Generating Image Descriptions
- java防止页面脚本注入 特殊字符过滤器
- CF692D2:The Wall (medium)(组合数)
- C/C++内存管理详解
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- react-native-open-project
- sqlmap之sql基础注入
- 图片传值+图片缩放拖拽
- UNIX环境高级编程——第六章—系统数据文件和信息
- Windows下Scrapy下载图片,指定图片名报错[WinError32]的解决办法
- hdu 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- jquery中的事件操作
- ES6 —(Class 的基本语法)