luogu P2286 [HNOI2004]宠物收养场

传送门

看错题...以为只有宠物可以等主人...

所以还是要注意细节

剩下的就没啥 写个treap维护前驱后继和是宠物/主人就行

注意取模.....

Code:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<queue>
  6 #include<vector>
  7 #include<iostream>
  8 #include<iomanip>
  9 #define itn int
 10 #define ms(a,b) memset(a,b,sizeof a)
 11 #define rep(i,a,n) for(int i = a;i <= n;i++)
 12 #define per(i,n,a) for(int i = n;i >= a;i--)
 13 #define inf 2147483647
 14 using namespace std;
 15 typedef long long ll;
 16 ll read() {
 17     ll as = 0,fu = 1;
 18     char c = getchar();
 19     while(c < '0' || c > '9') {
 20         if(c == '-') fu = -1;
 21         c = getchar();
 22     }
 23     while(c >= '0' && c <= '9') {
 24         as = as * 10 + c - '0';
 25         c = getchar();
 26     }
 27     return as * fu;
 28 }
 29 //head
 30 const int N = 200005;
 31 #define int ll
 32 int root,idx;
 33 int val[N],sze[N],num[N];
 34 int pa[N],sn[2][N];
 35 int New(int v,int f) {
 36     val[++idx] = v,pa[idx] = f;
 37     sn[0][idx] = sn[1][idx] = 0;
 38     sze[idx] = num[idx] = 1;
 39     return idx;
 40 }
 41 void pup(int x) {sze[x] = sze[sn[0][x]] + sze[sn[1][x]] + num[x];}
 42 bool dir(int x) {return sn[1][pa[x]] == x;}
 43 void Rot(int x) {
 44     int y = pa[x],z = pa[y];
 45     bool d = dir(x);
 46     sn[dir(y)][z] = x,pa[x] = z;
 47     sn[d][y] = sn[d^1][x],pa[sn[d][y]] = y;
 48     sn[d^1][x] = y,pa[y] = x;
 49     pup(y),pup(x);
 50 }
 51 void splay(int x,int k) {
 52     while(pa[x] ^ k) {
 53         int y = pa[x],z = pa[y];
 54         if(z ^ k) (dir(x) == dir(y)) ? Rot(y) : Rot(x);
 55         Rot(x);
 56     }
 57     if(!k) root = x;
 58 }
 59 
 60 void Ins(int v) {
 61     int x = root;
 62     while(sn[v > val[x]][x] && v ^ val[x]) 
 63         x = sn[v > val[x]][x];
 64     if(v == val[x]) num[x]++,splay(x,0);
 65     else {
 66         sn[v > val[x]][x] = New(v,x);
 67         splay(idx,0);
 68     }
 69 }
 70 
 71 int Q1(int v) {
 72     int x = root;
 73     while(sn[v > val[x]][x] && v ^ val[x]) 
 74         x = sn[v > val[x]][x];
 75     splay(x,0);
 76     return val[x] == v;
 77 }
 78 
 79 int Q2(int rnk) {
 80     int x = root;
 81     while(x) {
 82         if(rnk <= sze[sn[0][x]]) x = sn[0][x];
 83         else if(rnk <= sze[sn[0][x]] + num[x]) return val[x];
 84         else rnk -= (sze[sn[0][x]] + num[x]),x = sn[1][x];
 85     }
 86     return 0;
 87 }
 88 
 89 int Q3(int v,bool d) {
 90     Q1(v);
 91     int x = root;
 92     if(val[x] > v && d) return x;
 93     if(val[x] < v && !d) return x;
 94     x = sn[d][root];
 95     while(sn[d^1][x]) x = sn[d^1][x];
 96     return x;
 97 }
 98 
 99 int Q4(int v,bool d) {
100     if(Q1(v)) return root;
101     else return Q3(v,d);
102 }
103 
104 void Del(int v) {
105     int L = Q3(v,0),R = Q3(v,1);
106     splay(L,0),splay(R,L);
107     int x = sn[0][R];
108     if(num[x] > 1) num[x]--,splay(x,0);
109     else sn[0][R] = 0;
110 }
111 #define mod 1000000ll
112 #define MP(x,y) (x) = ((x)+(y))%mod
113 signed main() {
114     Ins(inf),Ins(-inf);
115     int T = read(),ans = 0;
116     int cnt = 0; 
117     while(T--) {
118         int op = read(),x = read();
119         if(cnt == 0) Ins(x);
120         else if(cnt > 0) {
121             if(!op) Ins(x);
122             else {
123                 int L = val[Q4(x,0)],R = val[Q4(x,1)];
124                 if(R - x < x - L) MP(ans,R - x),Del(R);
125                 else MP(ans,x - L),Del(L); 
126             }
127         } else {
128             if(op) Ins(x);
129             else {
130                 int L = val[Q4(x,0)],R = val[Q4(x,1)];
131                 if(R - x < x - L) MP(ans,R - x),Del(R);
132                 else MP(ans,x - L),Del(L);
133             }
134         }
135         op ? cnt-- : cnt++;
136     }
137     printf("%lld\n",ans);
138     return 0;
139 }
posted @ 2018-12-11 13:09  白怀潇  阅读(120)  评论(0编辑  收藏  举报