SPOJ3273โโORDERSET - Order statistic set(Treap)
INSERT(S,x): if x is not in S, insert x into S DELETE(S,x): if x is in S, delete x from S and the two type of queries
K-TH(S) : return the k-th smallest element of S COUNT(S,x): return the number of elements of S smaller than x Input Line 1: Q (1 โค Q โค 200000), the number of operations In the next Q lines, the first token of each line is a character I, D, K or C meaning that the corresponding operation is INSERT, DELETE, K-TH or COUNT, respectively, following by a whitespace and an integer which is the parameter for that operation. If the parameter is a value x, it is guaranteed that 0 โค |x| โค 109. If the parameter is an index k, it is guaranteed that 1 โค k โค 109.
Output For each query, print the corresponding result in a single line. In particular, for the queries K-TH, if k is larger than the number of elements in S, print the word โinvalidโ.
Example Input 8 I -1 I -1 I 2 C 0 K 2 D -1 K 1 K 2
Output 122 invalid ์ ๋ชฉ: ์ฌ๊ธฐ ๋งํฌ ๋ด์ฉ ์ ์ ๋ชฉ ์ ์ ๋ ๋ค. ์งํฉ S ๋ฅผ ์ ์ง ํ๊ณ ์งํฉ ๋ฐ์ดํฐ ์ ๋ํ ์ฝ์ ์ ์คํ ํ ๋ฉฐ ์ญ์ ํ๊ณ K ๊ฐ ์ ์ผ ๋ฉฐ K ๋ณด๋ค ์์ ์์ ๊ฐ ์ ๋ฅผ ๊ตฌ ํฉ ๋ ๋ค.์ฌ๊ณ : ๋ฌธ์ ์ ์ฝ์ ๊ณผ ์ญ ์ ๋ ๋ชจ๋ ํฉ ๋ฒ ์ ์ธ ๊ฒ ์ด ์๋ ๋ผ ์ฝ ์ ๋ ๋ฐ์ดํฐ ๊ฐ ์ด๋ฏธ ์กด์ฌ ํ ์ ์ ์ง๋ง ๊ธฐ๋ก ํ ๋ ๋ฅผ ์ญ์ ํ ๋ ๊ฒ ์ ์งํฉ ์ ์กด์ฌ ํ์ง ์ ์ ์ ์๋ค ๋ ๋ป ์ด๋ค.K ๊ฐ ์ ์ผ ๋ฉด k ๋ ์งํฉ ํฌ๊ธฐ ๋ณด๋ค ํด ์ ์ ์ต ๋ ๋ค. ์ด๋ invalid ๋ฅผ ์ถ๋ ฅ ํฉ ๋ ๋ค.๋ถ๋ช ํ ๊ฒ ์ Treap ์ ํ ํ ๋ฆฟ ๋ฌธ ์ ๋ ๋ถํฉ๋ฆฌํ ์ฝ์ , ์ญ์ , K ์์ ๊ฒ ์ ์ฒ๋ฆฌ ํ ๋ ๊ฒ ์ ๋ ๋ค.๊ทธ๋ ์ง ์ ์ผ ๋ฉด RE ๊ฐ ์ฝ๋ค.์ฝ๋:
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn =30100;
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;
ll 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=1;
} 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==NULL)return;
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]->weight > x->childs[1]->weight;
rotate(x, t);
_erase(x, k);
}
} else {
int t = x->key < k;
if(x->childs[t]!=NULL)_erase(x->childs[t], k);
if(x->childs[t]!=NULL&&x->childs[t]->cnt == 0)free(x->childs[t]), x->childs[t] = NULL;//
}
update(x);
}
ll _getkth(node *&x, ll k) {
ll 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 _lower(node *&x,ll k,ll &ans){
if(x->key<=k){
ans=max(ans,x->key);
if(x->childs[1]!=NULL)_lower(x->childs[1],k,ans);
}else if(x->childs[0]!=NULL)_lower(x->childs[0],k,ans);
}
void _count(node *&x,ll k,ll &ans){
if(x==NULL)return;
if(x->key>=k){
ans-=x->cnt;
if(x->childs[1]!=NULL)ans-=x->childs[1]->size;
if(x->childs[0]!=NULL)_count(x->childs[0],k,ans);
}else if(x->childs[1]!=NULL)_count(x->childs[1],k,ans);
}
void insert(ll k) {
_insert(root, k);
}
void erase(ll k) {
_erase(root, k);
if(root!=NULL&&root->cnt == 0)free(root), root = NULL;
}
ll getkth(ll k) {
if(root==NULL||k>root->size)return -1;
return _getkth(root, k);
}
void lower(ll k,ll &ans){
_lower(root,k,ans);
}
void count(ll k,ll &ans){
_count(root,k,ans);
}
};
Treap tree;
int main() {
srand((ll)time(NULL));
int q;
scanf("%d",&q);
for(int i=0;ichar c;
ll a;
scanf(" %c%lld",&c,&a);
if(c=='I')tree.insert(a);
else if(c=='D')tree.erase(a);
else if(c=='K'){
ll ans=tree.getkth(a);
if(ans==-1)printf("invalid
");
else printf("%lld
",ans);
}else {
ll ans=0;
if(tree.root!=NULL)ans=tree.root->size;
tree.count(a,ans);
printf("%lld
",ans);
}
}
return 0;
}
์ด ๋ด์ฉ์ ํฅ๋ฏธ๊ฐ ์์ต๋๊น?
ํ์ฌ ๊ธฐ์ฌ๊ฐ ์ฌ๋ฌ๋ถ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ AI ์์ง์ ๋จธ์ ๋ฌ๋ ๋ถ์(์ค๋งํธ ๋ชจ๋ธ์ด ๋ฐฉ๊ธ ๋ง๋ค์ด์ ธ ๋ถ์ ํํ ๊ฒฝ์ฐ๊ฐ ์์ ์ ์์)์ ํตํด ๊ฐ์ฅ ์ ์ฌํ ๊ธฐ์ฌ๋ฅผ ์ถ์ฒํฉ๋๋ค:
Visual Studio์์ ํ์ผ ํด๋ ๊ตฌ๋ถ (ํฌํจ ๊ฒฝ๋ก ์ค์ )Visual Studio์์ c, cpp, h, hpp ํ์ผ์ ํด๋๋ก ๋๋๊ณ ์ถ์์ต๋๊น? ์ด์ฉ๋ฉด ๋๋ถ๋ถ์ ์ฌ๋๋ค์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ฒ์์ ํ์ผ์ด ๋ง๋ค์ด์ง๋ ์ฅ์๋ ํ๋ก์ ํธ ํ์ผ ๋ฑ๊ณผ ๊ฐ์ ์ฅ์์ ์๊ธฐ ๋๋ฌธ์ ํ์ผ...
ํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
CC BY-SA 2.5, CC BY-SA 3.0 ๋ฐ CC BY-SA 4.0์ ๋ฐ๋ผ ๋ผ์ด์ผ์ค๊ฐ ๋ถ์ฌ๋ฉ๋๋ค.
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค