HDU1540 区间合并
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8115 Accepted Submission(s): 3142
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.
0
2
4
//线段树维护pre最大前缀,suf最大后缀,最后求某一点的答案就是其
//前缀加上后缀。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=;
int suf[maxn*],pre[maxn*];
void pushup(int i,int l,int r)
{
int m=(l+r)>>;
pre[i]=pre[i<<];
if(pre[i]==m-l+) pre[i]+=pre[i<<|];
suf[i]=suf[i<<|];
if(suf[i]==r-m) suf[i]+=suf[i<<];
}
void build(int i,int l,int r)
{
if(l==r){
suf[i]=pre[i]=;
return ;
}
int m=(l+r)>>;
build(i<<,l,m);
build(i<<|,m+,r);
pushup(i,l,r);
}
void update(int id,int c,int i,int l,int r)
{
if(l==r){
suf[i]=pre[i]=c;
return ;
}
int m=(l+r)>>;
if(id<=m) update(id,c,i<<,l,m);
else update(id,c,i<<|,m+,r);
pushup(i,l,r);
}
int query_pre(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&qr>=r) return pre[i];
int m=(l+r)>>,res;
if(qr<=m) return query_pre(ql,qr,i<<,l,m);
if(ql>m) return query_pre(ql,qr,i<<|,m+,r);
res=query_pre(ql,qr,i<<,l,m);
if(res==m-max(l,ql)+) res+=query_pre(ql,qr,i<<|,m+,r);
//注意记住写上max函数
return res;
}
int query_suf(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&qr>=r) return suf[i];
int m=(l+r)>>,res;
if(qr<=m) return query_suf(ql,qr,i<<,l,m);
if(ql>m) return query_suf(ql,qr,i<<|,m+,r);
res=query_suf(ql,qr,i<<|,m+,r);
if(res==min(r,qr)-m) res+=query_suf(ql,qr,i<<,l,m);
return res;
}
int main()
{
int n,m,x;
char ch[];
while(scanf("%d%d",&n,&m)==){
build(,,n);
stack<int>s;
while(m--){
scanf("%s",ch);
if(ch[]=='D'){
scanf("%d",&x);
update(x,,,,n);
s.push(x);
}
else if(ch[]=='R'){
if(!s.empty()){
int x=s.top();s.pop();
update(x,,,,n);
}
}
else if(ch[]=='Q'){
scanf("%d",&x);
int ans=query_suf(,x,,,n);
if(ans==) printf("%d\n",ans);
else{
ans+=query_pre(x,n,,,n)-;
printf("%d\n",ans);
}
}
}
}
return ;
}
HDU1540 区间合并的更多相关文章
- hdu1540 区间合并+询问某点的最大连续块
询问操作需要搞一下 今天被区间合并降智了 /* D a: 摧毁第a个点 Q a:询问a所在的点的块大小 R :修复最后被破坏的点 对于所有的点需要进行一次更新 更新比较容易,tag用来表示区间是否是完 ...
- Tunnel Warfare(HDU1540+线段树+区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...
- kb-09-线段树--区间合并比较繁
/* hdu-1540 题意:一个线段,长度为n,三种操作,Dx,挖掉某个点:R,恢复最近被挖掉的点:Qx查询该点所在的连续区间的长度: 树的节点维护三个变量,该节点左边界开始连续的个数ll,右边界开 ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
- HDU 3911 Black And White(线段树区间合并+lazy操作)
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
- POJ 2750 Potted Flower (线段树区间合并)
开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并... 给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
随机推荐
- Grid 网格布局
CSS 网格布局(Grid Layout) 是CSS中最强大的布局系统. 这是一个二维系统,这意味着它可以同时处理列和行,不像 flexbox 那样主要是一维系统. 你可以通过将CSS规则应用于父元素 ...
- leetcode个人题解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...
- react创建新项目并且修改配置文件
react创建项目 这是我在用react搭建项目时,用到的一些东西,顺序纯属自己定义, 一.创建项目 用react 创建一个项目,这也是官方给出的 1.npm install create-react ...
- Linearization of the kernel functions in SVM(多项式模拟)
Description SVM(Support Vector Machine)is an important classification tool, which has a wide range o ...
- 软件工程 speedsnail 第二次冲刺9
20150526 完成任务:划线的优化,速度和谐: 遇到问题: 问题1 速度仍然不满意 解决1 未解决 明日任务: 蜗牛碰到线后速度方向的调整:(做优化)
- 二叉搜索树(BST)---python实现
github:代码实现 本文算法均使用python3实现 1. 二叉搜索树定义 二叉搜索树(Binary Search Tree),又名二叉排序树(Binary Sort Tree). 二叉搜 ...
- iOS- UIScrollView、UIPageControl分页浏览图片
1.先介绍下UIScrollView的常见属性 @property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置 @property ...
- iOS-修改导航栏文字字体和颜色
//修改导航栏文字字体和颜色 nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[RGBColor co ...
- 敏捷冲刺Day1
前言: 之前的各种对教务系统的分析,再加上我们两三天的讨论和一个小时的站立会议,我们做出最终的决定.--我们决定换个项目主题,将原来的教务辅助系统换成现在的校园帮帮帮服务,并在之后会将完成后的计划书附 ...
- vue服务端渲染简单入门实例
想到要学习vue-ssr的同学,自不必多说,一定是熟悉了vue,并且多多少少做过几个项目.然后学习vue服务端渲染无非解决首屏渲染的白屏问题以及SEO友好. 话不多说,笔者也是研究多日才搞明白这个服务 ...