1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define Lson(x) (x<<1) #define Rson(x) (x<<1|1) #define Mid(x,y) ((x+y)>>1) #define MAXN 100005
struct Node{ int left; int right; int mark; int sum; }node[MAXN * 4];
void push_up(int pos) { node[pos].sum = node[Lson(pos)].sum + node[Rson(pos)].sum; }
void push_down(int pos) { if(node[pos].mark){ node[Lson(pos)].mark += node[pos].mark; node[Rson(pos)].mark += node[pos].mark; node[Lson(pos)].sum += node[pos].mark * (node[Lson(pos)].right - node[Lson(pos)].left + 1); node[Rson(pos)].sum += node[pos].mark * (node[Rson(pos)].right - node[Rson(pos)].left + 1); node[pos].mark = 0; } }
void build_tree(int left, int right, int pos) { node[pos].left = left; node[pos].right = right; node[pos].mark = 0; if(left == right){ node[pos].sum = 0; return ; } int mid = Mid(left, right); build_tree(left, mid, Lson(pos)); build_tree(mid + 1, right, Rson(pos)); push_up(pos); }
void update(int left, int right, int value, int pos) { if(left <= node[pos].left && node[pos].right <= right){ node[pos].mark += value; node[pos].sum += value * (node[pos].right - node[pos].left + 1); return ; } push_down(pos); int mid = Mid(node[pos].left, node[pos].right); if(right <= mid) update(left, right, value, Lson(pos)); else if(left > mid) update(left, right, value, Rson(pos)); else{ update(left, mid, value, Lson(pos)); update(mid + 1, right, value, Rson(pos)); } push_up(pos); }
int query(int left, int right, int pos) { if(node[pos].left == left && node[pos].right == right) return node[pos].sum; int mid = Mid(node[pos].left, node[pos].right); push_down(pos); if(right <= mid) return query(left, right, Lson(pos)); else if(left > mid) return query(left, right, Rson(pos)); else return query(left, mid, Lson(pos)) + query(mid + 1, right, Rson(pos)); }
int main() { int n, a, b; while(scanf("%d", &n) != EOF && n){ build_tree(1, n, 1); for(int i = 1; i <= n; i++){ scanf("%d%d", &a, &b); update(a, b, 1, 1); } for(int i = 1; i <= n; i++){ printf("%d", query(i, i, 1)); if(i != n) printf(" "); } printf("\n"); } return 0; }
|