标签:blog io for 2014 sp log html amp on
#include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int maxn = 100010; int pre[maxn], ch[maxn][2], sz[maxn]; int root, top1; int s[maxn], top2;//内存池 LL sum[maxn]; int val[maxn], add[maxn], a[maxn]; void pushdown(int x) { if(add[x]) { val[ch[x][0]] += add[x]; val[ch[x][1]] += add[x]; add[ch[x][0]] += add[x]; add[ch[x][1]] += add[x]; sum[ch[x][0]] += (LL)sz[ch[x][0]]*add[x]; sum[ch[x][1]] += (LL)sz[ch[x][1]]*add[x]; add[x] = 0; } } void pushup(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + val[x]; } void rotate(int x, int d) { int y = pre[x]; pushdown(y); pushdown(x); ch[y][d^1] = ch[x][d]; pre[ch[x][d]] = y; if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x; pre[x] = pre[y]; ch[x][d] = y; pre[y] = x; pushup(y); } void Splay(int x, int goal) { pushdown(x); while(pre[x] != goal) { if(pre[pre[x]] == goal) { rotate(x, ch[pre[x]][0] == x); } else { int y = pre[x], z = pre[y]; int d = (ch[z][0] == y); if(ch[y][d] == x) { rotate(x, d^1); rotate(x, d); } else { rotate(y, d); rotate(x, d); } } } pushup(x); if(goal == 0) root = x; } void erase(int x) { int y = pre[x]; } void NewNode(int &x, int f, int c) { if(top2) x = top2--; else x = ++top1; ch[x][0] = ch[x][1]; sz[x] = 1; pre[x] = f; val[x] = sum[x] = c; add[x] = 0; } void build(int &x, int l, int r, int f) { if(l > r) return; int m = (l + r) >> 1; NewNode(x, f, a[m]); build(ch[x][0], l, m-1, x); build(ch[x][1], m+1, r, x); pushup(x); } void init(int n) { ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; add[0] = sum[0] = val[0]; root = top1 = top2 = 0; NewNode(root, 0, -1); NewNode(ch[root][1], root, -1); sz[root] = 2; for(int i = 1; i <= n; i++) scanf("%d", &a[i]); build(ch[ch[root][1]][0], 1, n, ch[root][1]); pushup(ch[root][1]); pushup(root); //printf("**%d %d\n", val[root]); } int kth(int x, int k) { pushdown(x); int s = sz[ch[x][0]]; if(k == s+1) return x; if(k <= s) return kth(ch[x][0], k); return kth(ch[x][1], k-s-1); } void update(int l, int r, int c) { Splay(kth(root, l), 0); Splay(kth(root, r+2), root); val[ch[ch[root][1]][0]] += c; add[ch[ch[root][1]][0]] += c; sum[ch[ch[root][1]][0]] += (LL)c*sz[ch[ch[root][1]][0]]; pushup(ch[root][1]); pushup(root); } LL query(int l, int r) { Splay(kth(root, l), 0); Splay(kth(root, r+2), root); return sum[ch[ch[root][1]][0]]; }
标签:blog io for 2014 sp log html amp on
原文地址:http://blog.csdn.net/u011686226/article/details/39027441