思路:
线段树区间更新模板。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 100005; 4 int tree[N * 4], lazy[N * 4], a[N]; 5 int n; 6 7 void build() 8 { 9 memset(tree, 0, sizeof tree); 10 memset(lazy, 0, sizeof lazy); 11 } 12 13 void pushdown(int num) 14 { 15 if (!lazy[num]) return; 16 tree[num * 2] += lazy[num]; 17 tree[num * 2 + 1] += lazy[num]; 18 lazy[num * 2] += lazy[num]; 19 lazy[num * 2 + 1] += lazy[num]; 20 lazy[num] = 0; 21 } 22 23 void update(int num, int l, int r, int x, int y, int dx) 24 { 25 if (x <= l && y >= r) { tree[num] += dx; lazy[num] += dx; return; } 26 pushdown(num); 27 int m = l + r >> 1; 28 if (x <= m) update(num * 2, l, m, x, y, dx); 29 if (y >= m + 1) update(num * 2 + 1, m + 1, r, x, y, dx); 30 tree[num] = tree[num * 2] + tree[num * 2 + 1]; 31 } 32 33 int query(int num, int l, int r, int x, int y) 34 { 35 if (x <= l && y >= r) return tree[num]; 36 pushdown(num); 37 int m = l + r >> 1, ans = 0; 38 if (x <= m) ans += query(num * 2, l, m, x, y); 39 if (y >= m + 1) ans += query(num * 2 + 1, m + 1, r, x, y); 40 return ans; 41 } 42 43 int main() 44 { 45 ios::sync_with_stdio(false); 46 while (cin >> n, n) 47 { 48 build(); 49 int a, b; 50 for (int i = 0; i < n; i++) { cin >> a >> b; update(1, 1, n, a, b, 1); } 51 for (int i = 1; i <= n; i++) cout << query(1, 1, n, i, i) << " "; 52 cout << endl; 53 } 54 return 0; 55 }