HDU1540 Turnal Warfare
线段树保存每个区间的左边最大连续长度和右边最大连续长度~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int maxn=1e6+;
struct node {
int l,r;
int ll,rl,ml;
//左边开始连续的最大长度和右边开始连续的最大长度
//以及这个区间的最大连续长度
}segTree[maxn*];
void build (int i,int l,int r) {
segTree[i].l=l;
segTree[i].r=r;
segTree[i].ll=segTree[i].rl=segTree[i].ml=r-l+;
if (l==r) return;
int mid=(l+r)>>;
build (i<<,l,mid);
build (i<<|,mid+,r);
}
void update (int i,int t,int val) {
if (segTree[i].l==segTree[i].r) {
if (val==) segTree[i].ll=segTree[i].rl=segTree[i].ml=;
else segTree[i].ll=segTree[i].rl=segTree[i].ml=;
return;
}
int mid=(segTree[i].l+segTree[i].r)>>;
if (t<=mid) update (i<<,t,val);
else update (i<<|,t,val);
segTree[i].ll=segTree[i<<].ll;
segTree[i].rl=segTree[i<<|].rl;
segTree[i].ml=max(segTree[i<<].ml,segTree[i<<|].ml);
segTree[i].ml=max(segTree[i].ml,segTree[i<<].rl+segTree[i<<|].ll);
if (segTree[i<<].ll==segTree[i<<].r-segTree[i<<].l+) segTree[i].ll+=segTree[i<<|].ll;
if (segTree[i<<|].rl==segTree[i<<|].r-segTree[i<<|].l+)
segTree[i].rl+=segTree[i<<].rl;
}
int query (int i,int t) {
if (segTree[i].l==segTree[i].r||segTree[i].ml==||segTree[i].ml==segTree[i].r-segTree[i].l+)
return segTree[i].ml;
int mid=(segTree[i].l+segTree[i].r)>>;
if (t<=mid) {
if (t>=segTree[i<<].r-segTree[i<<].rl+)
return query (i<<,t)+query (i<<|,mid+);
else return query (i<<,t);
}
else {
if (t<=segTree[i<<|].l+segTree[i<<|].ll-)
return query (i<<|,t)+query(i<<,mid);
else return query (i<<|,t);
}
}
int a[maxn],top,n,m,x;
string s;
int main () {
while (~scanf ("%d %d",&n,&m)) {
build (,,n);
top=;
while (m--) {
cin>>s;
if (s=="D") {
scanf ("%d",&x);
a[top++]=x;
update (,x,);
}
else if (s=="Q") {
scanf ("%d",&x);
printf ("%d\n",query(,x));
}
else {
if (x>) {
x=a[--top];
update (,x,);
}
}
}
}
return ;
}
HDU1540 Turnal Warfare的更多相关文章
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- hdu1540 Tunnel Warfare
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU--1540 Tunnel Warfare(线段树区间更新)
题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- hdu1540 Tunnel Warfare【线段树】
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- HDU-1540 Tunnel Warfare(区间连续点长度)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...
- HDU1540 Tunnel Warfare 水题
分析:不需要线段树,set可过,STL大法好 #include <iostream> #include <cstdio> #include <cstring> #i ...
随机推荐
- Solr与对应Jdk版本的关系
Solr各版本下载地址:http://archive.apache.org/dist/lucene/solr/ 下载的包里面的CHANGES.txt 有当前版本需要的说明.
- 二分题 D - Salary Changing codeforce
题意:给出n个人(n是奇数),s钱:s为总的可以付工钱的钱: 每一个工人有一个付工钱的区间,只要在这个区间范围内,随便一个数都可以当作给这个工人付了钱: 老板要付给每个工人钱,并且付钱的中位数要尽可能 ...
- 题解【BZOJ4145】「AMPPZ2014」The Prices
题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...
- NOIP做题练习(day4)
A - 同花顺 题面 题解 30分做法 爆搜即可. 60分做法 去重+贪心. 100分做法 去重+贪心后,我们要寻找一段符合条件的最长同花上升子序列 \(L\),\(n-L\) 即为所求的答案. 首先 ...
- apache 二级域名设置
1. 你要拥有一个有泛域名解析的顶级域名,例如:test.com 在dns服务上设置,域名服务商都提供此服务 www.test.com 指向服务器IPtest.com 指向 ...
- AWS ec2的ubuntu14.04上安装git服务
http://imerc.xyz/2015/11/13/Ubuntu-14-04%E4%B8%8AGit%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E6%90%AD%E5 ...
- [Note]后缀自动机
后缀自动机 代码 #include <cstdio> #include <algorithm> #include <cstring> const int M = 1 ...
- HTMLElement.offsetParent(offsetLeft和offsetTop参照offsetParent的内边距边界)
IE7以上(不是火狐): 父级没有定位: 本身没有定位: ==> offsetParent:body 本身定位为:absolute/relative: ==> offsetParent:b ...
- 关于JavaScript中0、空字符串、'0'是true还是false的总结
最近被问到关于js中空字符串是true还是false得问题,一时间没想起来,现在在chrome的console面板上输出代码测试一下. "" == false 结果是true ...
- Vue中引入静态JS文件(爬坑)
前言(背景介绍) 开发的项目需要与Threejs的3D项目结合在一起,需要静态引入,jquery.js,stats.js,three.js,ThreeBSP.js等静态文件.开发环境是iview-ad ...