终末のcode

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;

const int N = 100007;

struct Tree{
int ch[2], fa, val, tot, siz;
}t[N];
int treeIndex, root;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + t[rt].tot;
}
inline int Ident(int x){
return x == t[t[x].fa].ch[1];
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
Splay(u, 0);
}
inline void Insert(int x){
int u = root, fa = 0;
while(u && t[u].val != x){
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u){
++t[u].tot;
}
else{
u = ++treeIndex;
t[u].fa = fa;
t[u].val = x;
t[u].siz = t[u].tot = 1;
t[u].ch[0] = t[u].ch[1] = 0;
if(fa) t[fa].ch[x > t[fa].val] = u;
}
Splay(u, 0);
}
inline int Kth(int x){
int u = root;
if(t[u].siz < x) return 0;
while(1){
int v = t[u].ch[0];
if(t[v].siz + t[u].tot < x){
x -= t[v].siz + t[u].tot;
u = t[u].ch[1];
}
else if(t[v].siz >= x){
u = v;
}
else
return t[u].val;
}
}
inline int Next(int x, int type){
Find(x);
int u = root;
if(t[u].val > x && type) return u;
if(t[u].val < x && !type) return u;
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
}
inline void Delete(int x){
int pre = Next(x, 0), nxt = Next(x, 1);
Splay(pre, 0), Splay(nxt, pre);
int u = t[nxt].ch[0];
if(t[u].tot > 1){
--t[u].tot;
Splay(u, 0);
}
else{
t[nxt].ch[0] = 0;
}
}
int main(){
int m;
io >> m;
Insert(1e9);
Insert(-1e9);
while(m--){
int opt, x;
io >> opt >> x;
switch(opt){
case 1:{
Insert(x);
break;
}
case 2:{
Delete(x);
break;
}
case 3:{
Find(x);
printf("%d\n", t[t[root].ch[0]].siz);
break;
}
case 4:{
printf("%d\n", Kth(x + 1));
break;
}
case 5:{
printf("%d\n", t[Next(x, 0)].val);
break;
}
case 6:{
printf("%d\n", t[Next(x, 1)].val);
break;
}
}
}
return 0;
}
50
1 577793
1 408221
1 880861
2 408221
1 460353
1 223489
6 577713
4 2
5 889905
2 880861
1 100033
1 73956
1 22575
5 583761
6 571549
1 812645
4 3
1 643621
1 451623
6 14895
1 556691
4 1
1 225789
2 22575
1 632329
3 73956
1 316785
5 101413
4 11
5 639414
6 636353
1 272382
1 434049
2 643621
1 99617
2 577793
1 921581
1 894033
3 223489
1 767367
3 272382
1 642721
1 272033
3 632329
1 737721
1 864513
5 746457
1 877545
1 51097
1 484817

~
577793
460353
880861
577793
577793
100033
22575
22575
1
100033
643621
632329
643621
4
6
13
737721

*/


A mater' s BIT... I do not understand at all...
Just record it here anyway.

const int MAX = 1 << 25;
const int dealZero = 10000007;

int t[MAX+7];
inline void Updata(int x,int w){
for(x += dealZero; x <= MAX; x += x&-x) t[x] += w;
}
inline int Query(int x){
x+=dealZero;
int sum=0;
while(x) sum+=t[x],x&=x-1;
return sum;
}
inline int Kth(int k,int rt=MAX){
for(int i=rt;i>>=1;)
if(t[rt-i]>=k)rt-=i;
else k-=t[rt-i];
return rt-dealZero;
}
int n,opt,x;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&opt,&x);
if(opt==1)Updata(x,1);
if(opt==2)Updata(x,-1);
if(opt==3)printf("%d\n",Query(x-1)+1);
if(opt==4)printf("%d\n",Kth(x));
if(opt==5)printf("%d\n",Kth(Query(x-1)));
if(opt==6)printf("%d\n",Kth(Query(x)+1));
}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;

const int N=100007;

#include<vector>
vector<int> t;

int main(){
int m;
io >> m;
t.reserve(N);
while(m--){
int opt, x;
io >> opt >> x;
switch(opt){
case 1:{
t.insert(upper_bound(t.begin(), t.end(), x), x);
break;
}
case 2:{
t.erase(lower_bound(t.begin(), t.end(), x));
break;
}
case 3:{
printf("%lld\n", lower_bound(t.begin(), t.end(), x) - t.begin() + 1);
break;
}
case 4:{
printf("%d\n", t[x-1]);
break;
}
case 5:{
printf("%d\n", *(--lower_bound(t.begin(), t.end(), x)));
break;
}
case 6:{
printf("%d\n", *(upper_bound(t.begin(), t.end(), x)));
break;
}
}
}
}


posted @ 2019-07-15 13:24  邱涵的秘密基地  阅读(165)  评论(0编辑  收藏  举报