HDOJ 1540 && POJ 2892 —— 线段树
来源:互联网 发布:英语软件哪个比较好 编辑:程序博客网 时间:2024/06/06 16:51
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Hint
An illustration of the sample input:
OOOOOOOD 3 OOXOOOOD 6 OOXOOXOD 5 OOXOXXOR OOXOOXOR OOXOOOO
Source
比较基础的一道线段树。
题意是给你若干个顺序相连的村庄,你有3种命令:D x :把x村庄干掉,R:重建上一个被干掉的村庄,Q x:输出与x村庄直接或间接相连的村庄数。
显然我们用线段树去维护3个域即可,suml该节点向左的最大长度,sumr该节点向右的最大长度,sum该段的长度。
村庄有3种状态,用cover记录: 1:全部村庄存在;0:全部村庄不存在; -1:有的村庄存在,有的不存在。
区间合并时有两种情况:
1:mid - l <= 左儿子的sumr 2:l - mid + 1 <= 右儿子的suml
两种情况皆为左儿子的sumr + 右儿子的suml 。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid, r , rt << 1 | 1//#define mid ((l + r) >> 1)#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 50000 + 500;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;//#pragma comment(linker, "/STACK:102400000,102400000")struct t{ int l , r , suml , sumr , cover , sum;}t[MAXN << 2];stack <int>s;void init(){ clr(t , 0);}void build(int l , int r , int rt){ t[rt].l = l ,t[rt].r = r; t[rt].suml = t[rt].sumr = t[rt].sum = r - l; t[rt].cover = 1; if(l == r - 1)return ; int mid = (l + r) >> 1; build(lson) ,build(rson);}void PushUp(int rt){ if(t[rt << 1].cover == 0 && t[rt << 1 | 1].cover == 0)t[rt].cover = 0; else if(t[rt << 1].cover == 1 && t[rt << 1 | 1].cover == 1)t[rt].cover = 1; else { t[rt].cover = -1; } t[rt].suml = t[rt << 1].suml + (t[rt << 1].cover == 1 ? t[rt << 1 | 1].suml : 0); t[rt].sumr = t[rt << 1 | 1].sumr + (t[rt << 1 | 1].cover == 1 ? t[rt << 1].sumr : 0); t[rt].sum = max(t[rt].suml , max(t[rt].sumr , t[rt << 1].sumr + t[rt << 1 | 1].suml));}void update(int L , int R , int add , int rt){ if(t[rt].l >= L && t[rt].r <= R) { t[rt].cover = t[rt].sum = t[rt].suml = t[rt].sumr = add; return ; } if(t[rt].l == t[rt].r - 1)return ; int mid = (t[rt].l + t[rt].r) >> 1; if(L <= mid)update(L , R , add , rt << 1); if(mid < R)update(L , R , add , rt << 1 | 1); PushUp(rt);}int query(int rt , int l){ if(t[rt].cover == 1)return t[rt].sum; if(t[rt].cover == 0)return 0; int mid = (t[rt].l + t[rt].r) >> 1; if(l < mid) { if(mid - l <= t[rt << 1].sumr) { return t[rt << 1].sumr + t[rt << 1 | 1].suml; } return query(rt << 1 , l); } else { if(l - mid + 1 <= t[rt << 1 | 1].suml) { return t[rt << 1 | 1].suml + t[rt << 1].sumr; } return query(rt << 1 | 1 , l); }}char str[5];int n , m;int main(){ //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge while(~scanf("%d%d" , &n , &m)) { init();// outstars build(0 , n , 1);// outstars while(!s.empty())s.pop(); while(m--) { scanf("%s" ,str); int x; if(str[0] == 'D') { scanf("%d" , &x); update(x - 1 , x , 0 , 1); s.push(x); } else if(str[0] == 'R') { if(s.empty())break; x = s.top(); s.pop(); update(x - 1 , x , 1, 1); } else { scanf("%d" , &x); int ans = query(1 , x - 1); printf("%d\n" , ans); } } } return 0;}
- HDOJ 1540 && POJ 2892 —— 线段树
- POJ 2892 Tunnel Warfare && HDOJ 1540 (线段树)
- 【线段树+扫描线】 HDOJ 1828 && POJ 1177 Picture
- HDOJ 4302 —— 线段树 或 优先队列
- HDOJ 4614 —— 线段树区间更新&区间求和
- HDOJ 4399 —— 线段树单点更新&单点查询
- 线段树——HDOJ 1166 敌兵布阵 解题报告
- POJ 2155 树套树—线段树套线段树
- HDOJ 1540 Tunnel Warfare(线段树)
- 【线段树】 HDOJ 1540 Tunnel Warfare
- HDOJ-1540 Tunnel Warfare(线段树+二分)
- HDOJ 1166(线段树)
- HDOJ 1754 (线段树)
- HDOJ 1698 (线段树)
- hdoj 1698 线段树
- hdoj 1754 【线段树】
- hdoj 4267 - 线段树
- hdoj 4578 线段树
- C++ 11右值引用
- C++11标准库之Type Traits简介
- ContentProvider/ContentResolver和Uri详解
- 二叉树相关问题
- OpenCV:概述、结构和内容
- HDOJ 1540 && POJ 2892 —— 线段树
- hdu2873 Bomb Game
- Ubuntu下如何将普通用户提升到root权限
- 深信服笔试题
- 应用程序正常初始化(0xc0000135)失败
- C++虚基类(virtual base class)
- 关于重构手机APP页面的若干技术问题
- 设计模式背后的思想
- 桌面文件夹图标变黑了 & 解决办法!~~