闭关。
P2234 [HNOI2002]营业额统计
// Author: levil #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> pii; const int N = 2e5 + 5; const int M = 2e4 + 5; const double eps = 1e-10; const LL Mod = 1e9 + 7; #define pi acos(-1) #define INF 1e8 #define dbg(ax) cout << "now this num is " << ax << endl; inline int read() { int f = 1;int x = 0;char c = getchar(); while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } inline long long ADD(long long x,long long y) {return (x + y) % Mod;} inline long long DEC(long long x,long long y) {return (x - y + Mod) % Mod;} inline long long MUL(long long x,long long y) {return x * y % Mod;} int f[N],cnt[N],value[N],sons[N][2],sz[N],allsz,rt; inline bool get_which(int x) {return sons[f[x]][1] == x;} inline void update(int x) { if(x) { sz[x] = cnt[x]; if(sons[x][0]) sz[x] += sz[sons[x][0]]; if(sons[x][1]) sz[x] += sz[sons[x][1]]; } return ; } inline void Clear(int x) {sons[x][0] = sons[x][1] = f[x] = sz[x] = cnt[x] = value[x] = 0;} inline void rotate(int x) {//旋转 int fr = f[x],g_fr = f[fr],w_son = get_which(x); sons[fr][w_son] = sons[x][w_son ^ 1]; f[sons[fr][w_son]] = fr; sons[x][w_son ^ 1] = fr; f[fr] = x; f[x] = g_fr; if(g_fr) sons[g_fr][sons[g_fr][1] == fr] = x; update(fr); update(x); } inline void splay(int x) { for(int i;i = f[x];rotate(x)) if(f[i]) rotate((get_which(x) == get_which(i)) ? i : x); rt = x; } inline void insert(int x) { if(!rt) { rt = ++allsz; sons[allsz][0] = sons[allsz][1] = f[allsz] = 0; sz[allsz] = cnt[allsz]++; value[allsz] = x; return ; } int now = rt,fa = 0; while(1) { if(x == value[now]) { cnt[now]++; update(now); update(fa); splay(now); break; } fa = now; now = sons[now][value[now] < x]; if(!now) { f[++allsz] = fa; sons[allsz][0] = sons[allsz][1] = 0; sz[allsz] = cnt[allsz] = 1; sons[fa][value[fa] < x] = allsz; value[allsz] = x; update(fa); splay(allsz); break; } } } inline int find_num(int x) {//查询排名为x的数 int now = rt; while(1) { if(sons[now][0] && x <= sz[sons[now][0]]) now = sons[now][0]; else { int temp = (sons[now][0] ? sz[sons[now][0]] : 0) + cnt[now]; if(x <= temp) return value[now]; x -= temp; now = sons[now][1]; } } } inline int find_rank(int x) {//查询x的排名 int now = rt,ans = 0; while(1) { if(x < value[now]) now = sons[now][0]; else { ans += (sons[now][0] ? sz[sons[now][0]] : 0); if(x == value[now]) {splay(now);return ans + 1;} ans += cnt[now]; now = sons[now][1]; } } } inline int find_pre() { int now = sons[rt][0]; while(sons[now][1]) now = sons[now][1]; return now; } inline int find_suffix() { int now = sons[rt][1]; while(sons[now][0]) now = sons[now][0]; return now; } inline void Delete(int x) { int h = find_rank(x); if(cnt[rt] > 1) {cnt[rt]--;update(rt);return;} if(!sons[rt][0] && !sons[rt][1]) {Clear(rt);rt = 0;return;} if(!sons[rt][0]) { int old_rt = rt; rt = sons[rt][1]; f[rt] = 0; Clear(old_rt); return; } else if(!sons[rt][1]) { int old_rt = rt; rt = sons[rt][0]; f[rt] = 0; Clear(old_rt); return; } int lemx = find_pre(),old_rt = rt; splay(lemx); sons[rt][1] = sons[old_rt][1]; f[sons[old_rt][1]] = rt; Clear(old_rt); update(rt); } map<int,int> mp; void solve() { int n;n = read(); LL ans = 0; value[0] = INF; for(int i = 1;i <= n;++i) { int x;x = read(); insert(x); if(i == 1) ans += x; else { if(mp[x] != 0) continue; int pre = value[find_pre()]; int nxt = value[find_suffix()]; // printf("pre is %d nxt is %d\n",pre,nxt); int ma = min(abs(x - pre),abs(x - nxt)); ans += ma; } mp[x]++; } printf("%lld\n",ans); } int main() { solve(); system("pause"); return 0; }View Code