HDU 1540 Tunnel Warfare
思路1:
树状数组+二分
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=5e4+;
int bit[N];
bool vis[N];
int n;
int add(int x,int a){
while(x<=n)bit[x]+=a,x+=x&-x;
}
int query(int x){
int ans=;
while(x)ans+=bit[x],x-=x&-x;
return ans;
}
bool check(int x,int m){
//cout<<query(m)<<' '<<query(x-1)<<endl;
if(query(m)-query(x-)<m-x+)return true;
else return false;
}
bool check1(int x,int m){
if(query(x)-query(m-)<x-m+)return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int m,x;
char c;
while(cin>>n>>m)
{
mem(bit,);
mem(vis,false);
for(int i=;i<=n;i++)add(i,);
stack<int>s;
for(int i=;i<=m;i++){
cin>>c;
if(c=='D'){
cin>>x;
if(!vis[x])add(x,-),vis[x]=true;
s.push(x);
}
else if(c=='Q'){
cin>>x;
int l=x,r=n,mid=(l+r)>>;
while(l<r){
//cout<<l<<' '<<r<<' '<<query(mid)<<' '<<query(x-1)<<endl;
if(check(x,mid))r=mid-;
else l=mid;
mid=(l+r+)>>; }
int R=mid;
l=,r=x,mid=(l+r)>>;
while(l<r){
if(check1(x,mid))l=mid+;
else r=mid;
mid=(l+r)>>;
}
int L=mid;
if(vis[x])cout<<<<endl;
else cout<<R-L+<<endl;
}
else{
if(vis[s.top()])add(s.top(),),vis[s.top()]=false;
s.pop();
}
}
}
return ;
}
思路2:
线段树区间合并
代码:
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=5e4+;
struct tree{
int ll,rr,len;//ll表示以当前区间左端点为左端点最长连续区间的长度,rr表示以当前区间右端点为右端点最长连续区间的长度,len表示当前区间的长度
}tree[N<<];
void push_up(int rt){
if(tree[rt<<].ll==tree[rt<<].len)tree[rt].ll=tree[rt<<].ll+tree[rt<<|].ll;
else tree[rt].ll=tree[rt<<].ll;
if(tree[rt<<|].rr==tree[rt<<|].len)tree[rt].rr=tree[rt<<|].rr+tree[rt<<].rr;
else tree[rt].rr=tree[rt<<|].rr;
}
void build(int rt,int l,int r){
tree[rt].ll=tree[rt].rr=tree[rt].len=r-l+;
if(l==r)return ;
int m=(l+r)>>;
build(ls);
build(rs);
}
void update(int p,int v,int rt,int l,int r){
if(l==r){
tree[rt].ll=tree[rt].rr=v;
return ;
}
int m=(l+r)>>;
if(p<=m)update(p,v,ls);
else update(p,v,rs);
push_up(rt);
}
int query(int p,int rt,int l,int r){
if(rt==){
if(l+tree[rt].ll->=p)return tree[rt].ll;
if(r-tree[rt].rr+<=p)return tree[rt].rr;
}
else{
if(l+tree[rt].ll->=p)return tree[rt].ll+tree[rt-].rr;
if(r-tree[rt].rr+<=p)return tree[rt].rr+tree[rt+].ll;
}
if(l==r)return ;
int m=(l+r)>>;
if(p<=m)return query(p,ls);
else return query(p,rs);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,m,x;
char c;
while(cin>>n>>m){
stack<int>s;
build(,,n);
while(m--){
cin>>c;
if(c=='D'){
cin>>x;
s.push(x);
update(x,,,,n);
}
else if(c=='Q'){
cin>>x;
cout<<query(x,,,n)<<endl;
}
else{
x=s.top();
s.pop();
update(x,,,,n);
}
}
}
return ;
}
HDU 1540 Tunnel Warfare的更多相关文章
- HDU 1540 Tunnel Warfare(最长连续区间 基础)
校赛,还有什么途径可以申请加入ACM校队? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1540 Tunnel Warfare (线段树 区间合并)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1540 Tunnel Warfare 线段数区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) P ...
- hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)
Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively i ...
随机推荐
- React组件,React和生命周期
笔记,具体可以看看这个博客: https://segmentfault.com/a/1190000004168886?utm_source=tag-newest react 的jsx document ...
- ios 透过上层视图点击相应下方视图的点击事件
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ UIView *hitView = [super hitTest:point ...
- 44 道 JavaScript 难题
JavaScript Puzzlers原文 1. ["1", "2", "3"].map(parseInt) 答案:[1, NaN, N ...
- 浅谈class私有变量
class 的前世今生 在 es6 之前,虽然 JS 和 Java 同样都是 OOP (面向对象)语言,但是在 JS 中,只有对象而没有类的概念. 在 JS 中,生成实例对象的传统方法是通过构造函数, ...
- cnats 使用
1. 准备 yum install cmakeyum install gcc gcc-c++yum install ncurses ncurses-develyum install openssl o ...
- HTML5 表单元素和属性
HTML5 表单元素和属性学习 版权声明:未经博主授权,内容严禁转载 ! 表单元素简介 无论实现提交功能还是展示页面功能,表单在HTML中的作用都十分重要. 在其他版本的HTML中,表单能够包含的元素 ...
- 20145317《网络对抗》Exp4 恶意代码分析
20145317<网络对抗>Exp4 恶意代码分析 一.基础问题回答 (1)总结一下监控一个系统通常需要监控什么.用什么来监控. 通常监控以下几项信息: 注册表信息的增删添改 系统上各类程 ...
- 探索Java8:(二)Function接口的使用
Java8 添加了一个新的特性Function,顾名思义这一定是一个函数式的操作.我们知道Java8的最大特性就是函数式接口.所有标注了@FunctionalInterface注解的接口都是函数式接口 ...
- VC++ 文件和应用程序关联,默认图标不显示问题
- @Transactional引起的NullPointerException
https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException ...