【线段树】 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
原创粉丝点击