HDU 1556 Color the ball

来源:互联网 发布:战舰世界沙恩数据 编辑:程序博客网 时间:2024/06/08 08:09

Description

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。 
当N = 0,输入结束。
 

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 

Sample Input

31 12 23 331 11 21 30
 

Sample Output

1 1 13 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;}


0 0
原创粉丝点击