Description
Input
Output
Sample Input
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
Sample Output
1 1 1 3 2 1
简单的区间更新单点询问,拿来用splay练练延迟更新
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<functional> using namespace std; typedef unsigned long long ull; typedef long long LL; int n, m, l, r, c, root; char s[10]; struct Splays { const static int maxn = 1e5 + 10; const static int INF = 0x7FFFFFFF; int ch[maxn][2], F[maxn], U[maxn], C[maxn], A[maxn], sz, G[maxn]; LL S[maxn]; int Node(int f, int u, int c) { C[sz] = 1; S[sz] = A[sz] = c; G[sz] = ch[sz][0] = ch[sz][1] = 0; F[sz] = f; U[sz] = u; return sz++; } void clear(){ sz = 1; ch[0][0] = ch[0][1] = C[0] = A[0] = U[0] = F[0] = S[0] = G[0] = 0; } void Pushdown(int x) { G[ch[x][0]] += G[x]; G[ch[x][1]] += G[x]; S[ch[x][0]] += (LL)G[x] * C[ch[x][0]]; S[ch[x][1]] += (LL)G[x] * C[ch[x][1]]; A[x] += G[x]; G[x] = 0; } void rotate(int x, int k) { int y = F[x]; Pushdown(y); Pushdown(x); ch[y][!k] = ch[x][k]; F[ch[x][k]] = y; if (F[y]) ch[F[y]][y == ch[F[y]][1]] = x; F[x] = F[y]; F[y] = x; ch[x][k] = y; C[x] = C[y]; C[y] = C[ch[y][0]] + C[ch[y][1]] + 1; S[x] = S[y]; S[y] = S[ch[y][0]] + S[ch[y][1]] + A[y]; } void Splay(int x, int r) { for (int fa = F[r]; F[x] != fa;) { if (F[F[x]] == fa) { rotate(x, x == ch[F[x]][0]); return; } int y = x == ch[F[x]][0], z = F[x] == ch[F[F[x]]][0]; y^z ? (rotate(x, y), rotate(x, z)) : (rotate(F[x], z), rotate(x, y)); } } bool insert(int &x, int u, int v) { if (!x) { x = Node(0, u, v); return false; } int now = 0; for (int i = x; i&&!now; C[i]++, S[i] += v, i = ch[i][U[i] < u]) { //Pushdown(i); if (u == U[i]) { A[i] += v; Splay(i, x); x = i; return true; } if (!ch[i][U[i] < u]) now = ch[i][U[i] < u] = Node(i, u, v); } Splay(now, x); x = now; return false; } void add(int &x, int l, int r, int c) { insert(x, l - 1, 0); insert(ch[x][1], r + 1, 0); G[ch[ch[x][1]][0]] += c; S[ch[ch[x][1]][0]] += (LL)c*C[ch[ch[x][1]][0]]; S[ch[x][1]] += (LL)c*C[ch[ch[x][1]][0]]; S[x] += (LL)c*C[ch[ch[x][1]][0]]; } void find(int &x, int l, int r) { insert(x, l - 1, 0); insert(ch[x][1], r + 1, 0); printf("%d\n", S[ch[ch[x][1]][0]]); } void find(int &x, int l) { insert(x, l, 0); printf("%d", A[x]); } }solve; int main() { while (scanf("%d", &n) != EOF, n) { solve.clear(); root = 0; for (int i = 1; i <= n; i++) solve.insert(root, i, 0); solve.insert(root, 0, 0); solve.insert(root, n + 1, 0); for (int i = 1; i <= n; i++) { scanf("%d%d", &l, &r); solve.add(root, l, r, 1); } for (int i = 1; i <= n; i++) solve.find(root, i), printf("%s", i == n ? "\n" : " "); } return 0; }