链接:
http://poj.org/problem?id=1442
题目:
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个询问,然后第
i
个寻问
思路:
先把n个数存进数组里,在询问为M的时候再将前M个数放入树堆,然后查询第i大就可以了。
实现:
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define read read()
#define edl putchar('\n')
#define clr(a, b) memset(a,b,sizeof a)
inline int read {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')c = getchar();
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
inline void write(int x) {
int y = 10, len = 1;
while (y <= x) {
y *= 10;
len++;
}
while (len--) {
y /= 10;
putchar(x / y + 48);
x %= y;
}
}
using namespace std;
template <class T, class Compare = std::less<T> >
class Treap {
private:
struct treap {
int size, fix;
T key;
Compare cmp;
treap *ch[2];
treap(T key) {
size = 1;
fix = rand();
this->key = key;
ch[0] = ch[1] = NULL;
}
int compare(T x) const {
if (x == key) return -1;
return cmp(x ,key) ? 0 : 1;
}
void Maintain() {
size = 1;
if (ch[0] != NULL) size += ch[0]->size;
if (ch[1] != NULL) size += ch[1]->size;
}
}*root;
Compare cmp;
void Rotate(treap *&t, int d) {
treap *k = t->ch[d ^ 1];
t->ch[d ^ 1] = k->ch[d];
k->ch[d] = t;
t->Maintain();
k->Maintain();
t = k;
}
void Insert(treap *&t, T x) {
if (t == NULL) t = new treap(x);
else {
//int d = t->compare(x);
int d = cmp(x ,t->key) ? 0 : 1;
Insert(t->ch[d], x);
if (t->ch[d]->fix > t->fix) Rotate(t, d ^ 1);
}
t->Maintain();
}
void Delete(treap *&t, int x) {
int d = t->compare(x);
if (d == -1) {
treap *tmp = t;
if (t->ch[0] == NULL) {
t = t->ch[1];
delete tmp;
tmp = NULL;
} else if (t->ch[1] == NULL) {
t = t->ch[0];
delete tmp;
tmp = NULL;
} else {
int k = t->ch[0]->fix > t->ch[1]->fix ? 1 : 0;
Rotate(t, k);
Delete(t->ch[k], x);
}
} else Delete(t->ch[d], x);
if (t != NULL) t->Maintain();
}
bool Find(treap *t, int x) {
while (t != NULL) {
int d = t->compare(x);
if (d == -1) return true;
t = t->ch[d];
}
return false;
}
T Kth(treap *t, int k) {
if (t == NULL || k <= 0 || k > t->size) return -1;
if (t->ch[0] == NULL) {
if (k == 1) return t->key;
return Kth(t->ch[1], k - 1);
}
if (t->ch[0]->size >= k) return Kth(t->ch[0], k);
if (t->ch[0]->size + 1 == k) return t->key;
return Kth(t->ch[1], k - 1 - t->ch[0]->size);
}
int Rank(treap *t, int x) {
int r;
if (t->ch[0] == NULL) r = 0;
else r = t->ch[0]->size;
if (x == t->key) return r + 1;
if (x < t->key) return Rank(t->ch[0], x);
return r + 1 + Rank(t->ch[1], x);
}
void Deletetreap(treap *&t) {
if (t == NULL) return;
if (t->ch[0] != NULL) Deletetreap(t->ch[0]);
if (t->ch[1] != NULL) Deletetreap(t->ch[1]);
delete t;
t = NULL;
}
void Print(treap *t) {
if (t == NULL) return;
Print(t->ch[0]);
cout << t->key << ' ';
Print(t->ch[1]);
}
public:
Treap() {
root = NULL;
}
~Treap() {
Deletetreap(root);
}
void insert(T x) {
Insert(root, x);
}
void clear() {
Deletetreap(root);
}
void print() {
Print(root);
}
T kth(int x) {
return Kth(root, x);
}
};
int val[int(1e6) + 7];
struct A {
int x;
A(int x):x(x) {}
A() {}
bool operator < (const A &tmp) const {
return x < tmp.x;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n, x, m;
Treap<A> a;
while (a.clear(), ~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]);
int index = 1;
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
for (int j = index; j <= x; j++)
a.insert(A{val[j]});
index = x + 1;
printf("%d\n", a.kth(i).x);
}
}
return 0;
}