HDU 1540
来源:互联网 发布:淘宝哪里有信用卡 编辑:程序博客网 时间:2024/05/21 11:39
void pushup(int pos){ node[pos].lx = node[lson].lx; node[pos].rx = node[rson].rx; if(node[pos].lx == node[lson].len()) node[pos].lx += node[rson].lx; if(node[pos].rx == node[rson].len()) node[pos].rx += node[lson].rx; node[pos].mx = node[lson].rx + node[rson].lx; node[pos].mx = max(node[pos].mx,max(node[lson].mx,node[rson].mx));}
/*Tunnel Warfare经典题:区间合并求某点所在区间的最大长度*/#include<cstdio>#include<cmath>#include<cstring>#include<vector>#include<stack>#include<algorithm>using namespace std;typedef long long LL;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int n,m,x;char op[10];struct Node{ int l,r,lx,rx,mx; int mid(){ return (l + r) >> 1; } int len(){ return (r - l + 1); }}node[maxn << 2];void pushup(int pos){ node[pos].lx = node[lson].lx; node[pos].rx = node[rson].rx; if(node[pos].lx == node[lson].len()) node[pos].lx += node[rson].lx; if(node[pos].rx == node[rson].len()) node[pos].rx += node[lson].rx; node[pos].mx = node[lson].rx + node[rson].lx; node[pos].mx = max(node[pos].mx,max(node[lson].mx,node[rson].mx));}void build(int l,int r,int pos){ node[pos].l = l; node[pos].r = r; if(l == r){ node[pos].lx = node[pos].rx = node[pos].mx = 1; return; } int mid = node[pos].mid(); build(l,mid,lson); build(mid + 1,r,rson); pushup(pos);}void update(int pos,int m,int d){ if(node[pos].l == node[pos].r){ node[pos].lx = node[pos].rx = node[pos].mx = d; return; } int mid = node[pos].mid(); if(m <= mid) update(lson,m,d); else update(rson,m,d); pushup(pos);}int query(int pos,int m){ if(node[pos].l == node[pos].r || node[pos].mx == 0 || node[pos].mx == node[pos].len()){ return node[pos].mx; } int mid = node[pos].mid(); if(m <= mid){ int v = mid - node[lson].rx + 1; if(m >= v) return query(lson,m) + query(rson,mid + 1); else return query(lson,m); } if(m > mid){ int v = mid + node[rson].lx; if(m <= v) return query(rson,m) + query(lson,mid); else return query(rson,m); }}void debug(int pos){ printf("[%d %d] lx:%d rx:%d mx:%d\n",node[pos].l,node[pos].r,node[pos].lx,node[pos].rx,node[pos].mx); if(node[pos].l == node[pos].r) return; debug(lson); debug(rson);}int main(){ while(scanf("%d%d",&n,&m) != EOF){ build(1,n,1); stack<int>st; for(int i = 0; i < m; i++){ scanf("%s",op); if(op[0] == 'D'){ scanf("%d",&x); st.push(x); update(1,x,0); } else if(op[0] == 'R'){ if(st.empty()) continue; x = st.top(); st.pop(); update(1,x,1); } else if(op[0] == 'Q'){ scanf("%d",&x); printf("%d\n",query(1,x)); } } } return 0;}
0 0
- hdu 1540
- HDU 1540
- HDU 1540
- hdu 1540
- Hdu 1540
- hdu 1540 Tunnel Warfare
- hdu 1540 线段树
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 线段树
- hdu 1540 区间合并
- hdu 1540 && poj 2892
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- 部署自己的tomcat,让tomcat和IIS共同享用服务器的80端口
- Android ORM-GreenDao学习二之进阶篇
- Spring Security身份认证之UserDetailsService
- CocoaPods安装和使用教程
- AOP之基于AspectJ注解总结与案例
- HDU 1540
- NuGet 质量博客链接
- Gradle入门系列(3):依赖管理
- 最简单的jQuery图片轮播插件
- Gradle入门系列(4):创建二进制发布版本
- 【转自Oracle ACE-盖国强】Oracle 11g全表扫描以Direct Path Read方式执行
- Activity基本用法
- [LeetCode] Search for a Range
- “坚持不一定成功,放弃一定会失败”--------王大爷