HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)
Now you are given a queue sequence and asked to perform several operations:
1. insert p
First you should find the smallest positive number (e.g. i) that does not appear in the current queue sequence, then you are asked to insert the +i at position p (position starts from 0). For -i, insert it into the right most position that result in a valid queue sequence (i.e. when encountered with element -x, the front of the queue should be exactly x).
For example, (+1 -1 +3 +4 -3 -4) would become (+1 +2 -1 +3 +4 -2 -3 -4) after operation 'insert 1'.
2. remove i
Remove +i and -i from the sequence.
For example, (+1 +2 -1 +3 +4 -2 -3 -4) would become (+1 +2 -1 +4 -2 -4) after operation 'remove 3'.
3. query i
Output the sum of elements between +i and -i. For example, the result of query 1, query 2, query 4 in sequence (+1 +2 -1 +4 -2 -4) is 2, 3(obtained by -1 + 4), -2 correspond.
In each case, the sequence is empty initially.
The input is terminated by EOF.
After each operation, output the sum of elements between +i and -i.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = * ;
const int n = ;
int weight[MAXN], child[MAXN][], size[MAXN], neg[MAXN], val[MAXN], pre[MAXN];
LL sum[MAXN];
int pos[MAXN];
int stk[MAXN], top, node_cnt; int m, p, x, root;
char s[]; priority_queue<int> Q;
int int_cnt; void test(int x) {
if(child[x][]) test(child[x][]);
cout<<val[x]<<" "<<sum[x]<<" "<<x<<" "<<pre[x]<<" "<<(child[pre[x]][] == x)<<endl;
//cout<<val[x]<<endl;
if(child[x][]) test(child[x][]);
} void init() {
while(!Q.empty()) Q.pop();
int_cnt = top = node_cnt = ;
} int new_int() {
if(!Q.empty()) {
int ret = -Q.top(); Q.pop();
return ret;
}
return ++int_cnt;
} int new_node(int f, int v) {
int x = (top ? stk[top--] : ++node_cnt);
pre[x] = f;
sum[x] = val[x] = v;
if(v < ) pos[n - v] = x;
else pos[v] = x;
size[x] = ; neg[x] = (v < );
weight[x] = rand();
child[x][] = child[x][] = ;
return x;
} void update(int x) {
sum[x] = sum[child[x][]] + sum[child[x][]] + val[x];
size[x] = size[child[x][]] + size[child[x][]] + ;
neg[x] = neg[child[x][]] + neg[child[x][]] + (val[x] < );
} void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
pre[y] = pre[x]; pre[x] = y;
pre[child[x][t]] = x;
update(x); update(y);
x = y;
} void insert1(int f, int &x, int k, int v) {
if(x == ) x = new_node(f, v);
else {
int t = (size[child[x][]] + <= k);
insert1(x, child[x][t], k - t * (size[child[x][]] + ), v);
if(weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} int cnt_pos(int x, int t) {
if(!x) return ;
int ret = cnt_pos(pre[x], child[pre[x]][] == x);
if(t == ) ret += size[child[x][]] - neg[child[x][]] + (val[x] > );
return ret;
} void insert2(int f, int &x, int k, int v) {
if(x == ) x = new_node(f, v);
else {
int t = (neg[child[x][]] + (val[x] < ) <= k);
insert2(x, child[x][t], k - t * (neg[child[x][]] + (val[x] < )), v);
if(weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} void remove(int &x) {
if(child[x][] && child[x][]) {
int t = weight[child[x][]] < weight[child[x][]];
rotate(x, t);
remove(child[x][t ^ ]);
} else {
stk[++top] = x;
pre[child[x][]] = pre[child[x][]] = pre[x];
x = child[x][] + child[x][];
}
if(x > ) update(x);
} LL query1(int x, int t) {
if(!x) return ;
LL ret = query1(pre[x], child[pre[x]][] == x);
if(t == ) ret += sum[child[x][]] + val[x];
return ret;
} LL query2(int x, int t) {
if(!x) return ;
LL ret = query2(pre[x], child[pre[x]][] == x);
if(t == ) ret += sum[child[x][]] + val[x];
return ret;
} LL query(int x, int a, int b) {
LL ret = query1(pre[a], child[pre[a]][] == a) + sum[child[a][]];
ret += query2(pre[b], child[pre[b]][] == b) + sum[child[b][]];
return ret;
} void update_parent(int t) {
while(t) update(t), t = pre[t];
} int main() {
for(int t = ; ; ++t) {
if(scanf("%d", &m) == EOF) break;
init();
printf("Case #%d:\n", t);
root = ;
while(m--) {
scanf("%s%d", s, &x);
if(*s == 'i') {
int tmp = new_int();
insert1(, root, x, tmp);
int k = cnt_pos(pos[tmp], ) - ;
insert2(, root, k, -tmp);
}
if(*s == 'r') {
if(root == pos[x]) {
remove(root);
}
else {
int t = pos[x], p = pre[t];
remove(child[p][child[p][] == t]);
update_parent(p);
}
int y = x + n;
if(root == pos[y]) {
remove(root);
}
else {
int t = pos[y], p = pre[t];
remove(child[p][child[p][] == t]);
update_parent(p);
}
Q.push(-x);
}
if(*s == 'q') {
printf("%I64d\n", query(root, pos[x], pos[x + n]));
}
//test(root);
}
}
}
HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)的更多相关文章
- HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)
Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
- HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)
Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...
- HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)
Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...
- HDU 4441 Queue Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:对于一个序列,每次有三种操作 insert pos 表示在pos插入一个数,这个数是最小的正数 ...
- HDU 4441 Queue Sequence(splay)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并 ...
- HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...
随机推荐
- Linux中软件使用笔记
刚刚接触Linux的小白,难免会碰到各种小问题,不要灰心,总有办法的... 1.搜狗输入法崩溃,打不出中文?都是乱码?一招制敌! 在Terminal中输入下面命令后重启电脑即可重生- 还有,是Sogo ...
- some small knowledge
cookie 增查 <!--1.语义化标签的作用--> <!--1.1 从开发角度考虑是提高代码的可读性可维护性--> <!--1.2 网站的发布者:seo 搜索引擎优化 ...
- js中实现页面跳转(返回前一页、后一页)
一:JS 重载页面,本地刷新,返回上一页 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a ...
- CTRL+F5 和F5 两种刷新有什么区别
- 用户交互input
input() 函数 接收到的都是str,如果输入为数字,打印结果想进行运算,此时需要转义.语法:内容=input("提示信息")这里可以直接获取到用户输入的内容. a = inp ...
- sqlachemy详解
实习期老大让我学Python...学了很久了好吗,不过确实对其中的一些原理性的东西还不够深入. 比如今天要说的sqlachemy,结合网上做些总结吧 ORM 全称 Object Relational ...
- 【nginx下对服务器脚本php的支持】
安装php7 下载地址:https://secure.php.net/downloads.php这里下载的是:wget http://ar2.php.net/distributions/php ...
- 几种常用的git命令
1.合并代码出现冲突,用git status 查看冲突所在的文件 2. clone 指定分支分支的文件夹 git clone -b **** ***; 3.git merge 和 git rebase ...
- Hadoop(21)-数据清洗(ELT)简单版
有一个诸如这样的log日志 去除长度不合法,并且状态码不正确的记录 LogBean package com.nty.elt; /** * author nty * date time 2018-12- ...
- python-集合类型
集合具有唯一性(集合中的元素各不相同),无序性,确定性(集合中的元素是不可改变的,不能是列表,字典以及集合本身) 1.add(self, *args, **kwargs),union(self, *a ...