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
| #include <iostream> #include <algorithm> #include <stdio.h> #include <string> #include <queue> #include <cstring> using namespace std; int tree[400001]; int addmark[400001]; void pushdown(int node,int begin,int end) { if(addmark[node] == 0) return; int mid = (begin + end) / 2; addmark[node << 1] += addmark[node]; addmark[node << 1|1] += addmark[node]; tree[node << 1] += addmark[node] * (mid - begin + 1); tree[node << 1|1] += addmark[node] * (end - mid); addmark[node] = 0; } void update(int node,int begin ,int end,int l ,int r ,int k) { if(l > end || r < begin) return; if(l <= begin && r >= end) { tree[node] += k; addmark[node] += k; return; } pushdown(node,begin,end); int mid = (begin + end) / 2; update(node << 1, begin , mid, l, r, k); update(node << 1|1, mid + 1, end, l ,r ,k); tree[node] = tree[node << 1] + tree[node << 1|1]; } int query(int node,int begin,int end,int pos) { if(pos > end || pos < begin) return 0; if(pos == begin && begin == end) { return tree[node]; } pushdown(node,begin,end); int sum = 0; int mid = (begin + end) / 2; sum += query(node << 1,begin ,mid, pos); sum += query(node << 1|1,mid + 1,end,pos); return sum; } int main() { int n; while(cin >> n && n) { int l, r; memset(tree,0,sizeof(tree)); memset(addmark,0,sizeof(addmark)); for(int i = 1; i <= n; ++i) { scanf("%d %d",&l,&r); update(1,1,n,l,r,1); } for(int i = 1; i <= n; ++i) { printf("%d%c",query(1,1,n,i),i == n ? '\n' : ' '); } } }
|