hdu1540-Tunnel Warfare (线段树区间合并)

题意:n个村庄,有三种操作,D x 破坏位置为x的村庄,R 修复上一次被破坏的村庄,Q x 输出含有x村庄的连续村庄的最大个数。线段树搞之,区间合并。

ls[maxn]为当前节点左面的连续区间,rs[maxn]为当前节点左面的连续区间,ms[maxn]当前节点的最大连续区间。

  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cctype>
  8 #include <cstdio>
  9 #include <string>
 10 #include <vector>
 11 #include <cstdlib>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 typedef unsigned long long ull;
 17 typedef long long ll;
 18 const int inf = 0x3f3f3f3f;
 19 const double eps = 1e-8;
 20 const int maxn = 5e4+10;
 21 int ls[maxn<<2],ms[maxn<<2],rs[maxn<<2];
 22 stack<int>S;
 23 void build(int l,int r,int pos)
 24 {
 25     ls[pos] = rs[pos] = ms[pos] = r - l + 1;
 26     if (l == r)
 27         return;
 28     int mid = (l + r) >> 1;
 29     build(l,mid,pos<<1);
 30     build(mid+1,r,pos<<1|1);
 31 }
 32 void update(int l,int r,int pos,int x,int val)
 33 {
 34     if (l == r)
 35     {
 36         ls[pos] = rs[pos] = ms[pos] = val;
 37         return;
 38     }
 39     int mid = (l + r) >> 1;
 40     if (x <= mid)
 41         update(l,mid,pos<<1,x,val);
 42     if (x > mid)
 43         update(mid+1,r,pos<<1|1,x,val);
 44     ms[pos] = max(rs[pos<<1]+ls[pos<<1|1],max(ms[pos<<1],ms[pos<<1|1]));
 45     ls[pos] = ls[pos<<1];
 46     rs[pos] = rs[pos<<1|1];
 47     if (mid-l+1==rs[pos<<1])
 48         ls[pos] += ls[pos<<1|1];
 49     if (r-mid==ls[pos<<1|1])
 50         rs[pos] += rs[pos<<1];
 51 }
 52 int query(int l,int r,int pos,int x)
 53 {
 54     if (ms[pos] == r - l + 1 || !ms[pos] || l == r)
 55     {
 56         return ms[pos];
 57     }
 58     int mid = (l + r) >> 1;
 59     if (x <= mid)
 60     {
 61         if (x >= mid - rs[pos<<1] + 1)
 62             return query(l,mid,pos<<1,x) + query(mid+1,r,pos<<1|1,mid+1);
 63         else
 64             return query(l,mid,pos<<1,x);
 65     }
 66     else
 67     {
 68         if (x <= mid+ls[pos<<1|1])
 69             return query(mid+1,r,pos<<1|1,x) + query(l,mid,pos<<1,mid);
 70         else
 71             return query(mid+1,r,pos<<1|1,x);
 72     }
 73 }
 74 int main(void)
 75 {
 76     #ifndef ONLINE_JUDGE
 77         freopen("in.txt","r",stdin);
 78     #endif
 79     int n,m;
 80     while (~scanf ("%d%d",&n,&m))
 81     {
 82         build(1,n,1);
 83         char op[10];
 84         for (int i = 0; i < m; i++)
 85         {
 86             int x;
 87             scanf ("%s",op);
 88             if (op[0] == ‘D‘)
 89             {
 90                 scanf ("%d",&x);
 91                 update(1,n,1,x,0);
 92                 S.push(x);
 93             }
 94             if (op[0] == ‘Q‘)
 95             {
 96                 scanf ("%d",&x);
 97                 printf("%d\n",query(1,n,1,x));
 98             }
 99             if (op[0] == ‘R‘)
100             {
101                 if (!S.empty())
102                 {
103                     int tmp = S.top();
104                     S.pop();
105                     update(1,n,1,tmp,1);
106                 }
107             }
108         }
109         while (!S.empty())
110             S.pop();
111     }
112     return 0;
113 }
时间: 2024-10-08 09:41:55

hdu1540-Tunnel Warfare (线段树区间合并)的相关文章

HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定 所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间. 注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间.反之同理. 查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询. 查询

HDU 1540 Tunnel Warfare(线段树 区间合并 最大连续区间)

题意  有n个连在一起的地道  接下来有m个操作  D x 炸掉x号地道  炸掉后x所在的区间就不连续了  Q x 查询输出包括x的最大连续区间长度   R修复最后一个被炸的地道  注意输入R时可能并没有需要修复的地道 线段树的区间合并问题  线段树要维护3个信息 len  对应区间的最大连续长度 ll  对应区间最左端的一段连续长度 lr  对应区间最右端的一段连续长度 那么双亲节点的这些信息和孩子节点有什么关系呢  容易发现 双亲的len 是 左孩子的len 右孩子的len  左孩子的右端长

Tunnel Warfare 线段树 区间合并|最大最小值

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 con

hdu 1540 Tunnel Warfare 线段树 区间合并

题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2.如果在坐儿子的右区间或右儿子的左区间,返回这两个区间长度和 3.继续递归 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std;

hdu1540 Tunnel Warfare 线段树/树状数组

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 con

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)

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 vill

hdu 1540 Tunnel Warfare(线段树)

题目链接:hdu 1540 Tunnel Warfare 题目大意:有连续的N个城镇,三种操作: D x:第x城镇被破坏 Q x:插叙第x城镇所在联通块有多少个城镇没有被破坏 R:修复最后一个被破坏的城镇 解题思路:线段树区间合并,每个城镇看成一个叶子节点,用一个vector记录破坏顺序.对于查询来说,每次只要判断是否在mid?R[lson(u)],mid+L[rson(u)]之间即可,否则即递归查询左右子树. #include <cstdio> #include <cstring>

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

HDU 3911 Black And White(线段树区间合并)

Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she wan