【线段树】 HDOJ 4967 Handling the Past
来源:互联网 发布:bootbox.js 中文 编辑:程序博客网 时间:2024/04/29 11:48
比较绕的题目,线段树维护栈中操作。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib>#include <cmath>#include <time.h>#define maxn 50005#define maxm 40005#define eps 1e-10#define mod 998244353#define INF 999999999#define lowbit(x) (x&(-x))#define mp mark_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R//#pragma comment (linker,"/STACK:102400000,102400000")typedef long long LL;//typedef int LL;using namespace std;LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}// headstruct opr{char k;int x, t;}op[maxn];int sum[maxn<<2], rmax[maxn<<2];int tt[maxn], tx[maxn];int tcnt, n, m, p, v, ql, qr, ans, limit;char ss[maxn];void scanf(char *s){char ch = getchar();while(ch != 'p') ch = getchar();ss[0] = ch, ss[1] = getchar(), getchar(), getchar();}int cmp(int a, int b){return a < b;}int search(int x){int top = tcnt, bot = 1, mid;while(top >= bot) {mid = (top+bot)>>1;if(tt[mid] == x) break;if(tt[mid] > x) top = mid-1;else bot = mid+1;}return mid;}void init(void){memset(tx, 0, sizeof tx);memset(sum, 0, sizeof sum);memset(rmax, 0, sizeof rmax);}void read(void){for(int i = 1; i <= n; i++) {//scanf("%s", ss);scanf(ss);op[i].k = ss[1];if(op[i].k == 'u') scanf(op[i].x), scanf(op[i].t);//scanf("%d%d", &op[i].x, &op[i].t);else scanf(op[i].t);//scanf("%d", &op[i].t);tt[i] = op[i].t;}sort(tt+1, tt+n+1, cmp);tcnt = n;for(int i = 1; i <= n; i++) op[i].t = search(op[i].t);}void pushup(int o){sum[o] = sum[ls] + sum[rs];rmax[o] = max(rmax[rs], sum[rs] + rmax[ls]);}void updata(int o, int L, int R){if(L == R) {sum[o] = rmax[o] = v;return;}int mid = (L+R)>>1;if(p <= mid) updata(lson);else updata(rson);pushup(o);}void query(int o, int L, int R){if(ans) return;int mid = (L+R)>>1;if(L == R) {limit += sum[o];if(limit > 0) ans = L;return;}if(ql <= L && qr >= R) {if(limit + rmax[o] > 0) {if(limit + rmax[rs] > 0) query(rson);else limit += sum[rs], query(lson);}else limit += sum[o];return;}if(qr > mid) query(rson);if(ql <= mid) query(lson);}void work(void){for(int i = 1; i <= n; i++) {if(op[i].k == 'u') p = op[i].t, v = 1, tx[op[i].t] = op[i].x, updata(1, 1, tcnt);if(op[i].k == 'o') p = op[i].t, v = -1, updata(1, 1, tcnt);if(op[i].k == 'e') {ans = limit = 0;ql = 1, qr = op[i].t-1;query(1, 1, tcnt);if(ans) printf("%d\n", tx[ans]);else printf("-1\n");}}}int main(void){int _ = 0;while(scanf("%d", &n), n != 0) {init();read();printf("Case #%d:\n", ++_);work();}return 0;}
0 0
- 【线段树】 HDOJ 4967 Handling the Past
- [HDOJ 4967] Handling the Past [线段树]
- 【HDU】4967 Handling the Past 线段树
- Hdu 4967 Handling the Past (线段树)
- HDU 4967 Handling the past(线段树)
- HDU 4967 Handling the Past 线段树 调用顺序
- HDU-4967 Handling the Past (线段树)
- hdu 4967 Handling the Past 线段树 2014 Multi-University Training Contest 9-1008
- hdu 4967 Handling the Past 线段树,最右正值区间
- HDU 4967(Handling the Past-线段树维护可持久化栈操作)
- HDU 4967 Handling the Past
- hdoj 1556 Color the ball 【线段树】
- HDOJ Color the ball (线段树方法)
- [HDOJ 4348] To the moon [线段树+可持久化]
- hdoj 1556 Color the ball(线段树||树状数组)
- 【线段树】 HDOJ 5381 The sum of gcd
- HDOJ 1556 Color the ball (线段树+区间更新)
- HDOJ 1556 Color the ball(树状数组 & 线段树)
- 中兴软件面试题2
- linux系统调用表
- C#中关闭第一个Form窗口,打开另一个新窗口方法
- 分布式缓存Memcached详解
- CentOS源码安装apache和php
- 【线段树】 HDOJ 4967 Handling the Past
- 指令流水线的计算
- 中兴面试题1
- (YII)AJAX CTreeView 实现动态加载无限级树
- HDU4968-Improving the GPA
- 常见的.NET开源项目整理
- 一个整数的二进制中1的个数
- 还是畅通工程 hdu acm 1233 c++
- linux下编译,链接,运行汇编程序