Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1556
Dyeing: dye [a, B] each time, and then query the number of dyeing times of all 1-N nodes...
I have done this before, but I have not understood the mysteries of reverse use of the makeup array... In fact, it is to reverse the regular query and update operations...
If [a, B] is updated, adding 1 to position a is equivalent to adding 1 to all the places next to this position, then, reducing the position B + 1 by 1 is equivalent to reducing the position after this position by 1. If the position X to be searched next time is behind this range, certainly it will not be added with 1. If X is in the middle of a and B, it must be added with 1...
Later, the speechless Daniel said that tree arrays could not be used... Sort the query first, and then add 1 minus 1 according to the above idea... Because there is no intermediate query for this question...
The two-dimensional pku2155, the same...
Code;
#include <stdlib.h>#include <conio.h>#include <malloc.h>#include <time.h>#include <string.h>#include <stdio.h>#include <memory.h>#include <vector>#include <string>#include <stack>#include <queue>#include <fstream>#include <cmath>#include <iomanip>#include <time.h>#include <stdio.h>#include <algorithm>#include <cmath>#include <iostream>using namespace std;const int N=100010;int n, m, ans;int a[N], sum[N];vector<int> xx[N], yy[N];int query(int i){int tmp = 0;for(; i>0; i-=i&(-i))tmp += sum[i];return tmp;}void update(int i, int v){for(; i<=n; i+=i&(-i))sum[i] += v;}int main(){int i, j, k, x, y, cas;while(scanf("%d", &n)!=EOF){if(n==0)break;for(i=1; i<=n; i++){sum[i] = 0;}for(i=1; i<=n; i++){scanf("%d%d", &x, &y);update(x, 1);update(y+1, -1);}for(i=1; i<n; i++)printf("%d ", query(i));printf("%d\n", query(i));}return 0;}