Black Box
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14319 Accepted: 5848
Description
Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions:
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending.
Let us examine a possible sequence of 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer
(elements are arranged by non-descending)
1 ADD(3) 0 3
2 GET 1 3 3
3 ADD(1) 1 1, 3
4 GET 2 1, 3 3
5 ADD(-4) 2 -4, 1, 3
6 ADD(2) 2 -4, 1, 2, 3
7 ADD(8) 2 -4, 1, 2, 3, 8
8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8
9 GET 3 -1000, -4, 1, 2, 3, 8 1
10 GET 4 -1000, -4, 1, 2, 3, 8 2
11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8
It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type.
Let us describe the sequence of transactions by two integer arrays:
A(1), A(2), …, A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).
u(1), u(2), …, u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, … and N-transaction GET. For the Example we have u=(1, 2, 6, 6).
The Black Box algorithm supposes that natural number sequence u(1), u(2), …, u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), …, A(u(p)) sequence.
Input
Input contains (in given order): M, N, A(1), A(2), …, A(M), u(1), u(2), …, u(N). All numbers are divided by spaces and (or) carriage return characters.
Output
Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.
Sample Input
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Sample Output
3
3
1
2
题目:这里写链接内容
题意:给定一个n,m。
后两行为a[1]~a[n]和u[1]~u[m]。
a表示要按顺序插入的数据。u表示在第u[i]个数据插入后询问第K小的数据。
k初始为0,每次询问之前k+1.
思路:平衡树模板题,没有用到删除操作,所以用二叉排序堆应该也可以做(但是应该被卡了)。
代码:
#include<cstdio>
#include<algorithm>
#include <ctime>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn =30100;
int random() {//用自随机函数会被卡,使用rand()可以过,如果以时间为随机种子会快一点点
int inter = 823;
return inter = int(inter * 48271LL % 2147483647);
}
struct Treap {
struct node {
ll key, weight, cnt, size;
node *childs[2];
void init(ll x) {
key = x;
weight = rand();
cnt = 1;
size = 1;
childs[0] = childs[1] = NULL;
}
};
node *root;
void update(node *&x) {
if (x == NULL)
return;
int right = 0, left = 0;
if (x->childs[0] != NULL)right = x->childs[0]->size;
if (x->childs[1] != NULL)left = x->childs[1]->size;
x->size = right + left + x->cnt;
}
void rotate(node *&x, int t) {
node *y = x->childs[t];
x->childs[t] = y->childs[1 - t];
y->childs[1 - t] = x;
update(x);
update(y);
x = y;
}
void _insert(node *&x, ll k) {
if (x != NULL) {
if (x->key == k) {
x->cnt++;
} else {
int t = x->key < k;
_insert(x->childs[t], k);
if (x->childs[t]->weight < x->weight) {
rotate(x, t);
}
}
} else {
x = (node *) malloc(sizeof(node));
x->init(k);
}
update(x);
}
void _erase(node *&x, ll k) {
if (x->key == k) {
if (x->cnt > 1) {
x->cnt--;
} else {//如果被删除节点存在子节点,先将其旋转至底层再删除
if (x->childs[0] == NULL && x->childs[1] == NULL) {
x->cnt = 0;
return;
}
int t;
if (x->childs[0] == NULL)t = 1;
else if (x->childs[1] == NULL)t = 0;
else t = x->childs[0] > x->childs[1];
rotate(x, t);
_erase(x, k);
}
} else {
int t = x->key < k;
_erase(x->childs[t], k);
if (x->childs[t]->cnt == 0)free(x->childs[t]), x->childs[t] = NULL;//动态内存释放
}
update(x);
}
int _getkth(node *&x, ll k) {
int t = 0;
if (x->childs[0] != NULL)t = x->childs[0]->size;
if (k <= t)return _getkth(x->childs[0], k);
k -= t + x->cnt;
if (k <= 0)return x->key;
return _getkth(x->childs[1], k);
}
void insert(ll k) {
_insert(root, k);
}
void erase(ll k) {
_erase(root, k);
}
ll getkth(ll k) {
return _getkth(root, k);
}
};
Treap tree;
ll a[maxn],u;
int main() {
srand((unsigned int)time(NULL));//以时间为随机种子
int n,m,now=0,add=1;
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; ++i)scanf("%lld",&a[i]);
for(int i=1;i<=m;i++){
scanf("%lld",&u);
while (add!=u+1)tree.insert(a[add++]);
printf("%lld\n",tree.getkth(++now));
}
return 0;
}