At first, I thought it was DP, but I didn't think of anything at last. And a brute force attack.
Then, start writing the line segment tree and the result times out. I feel that there is a problem with writing the line segment tree. Write again in another day. Paste the tree array method first.
~~~~~~~~~~~~~~~~~~~~~~~~
If the tree array doesn't understand, go to Liu lujia's big white book. The picture is very clear.
The coordinates of the stars are given in the ascending order of Y. The points in the lower left of these points represent the series of the points ~ How many levels of N-1? In fact, Y is useful.
Question link: http://poj.org/problem? Id = 2352
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1541
The idea of the tree array is: Every query is equivalent to a query in 1 ~ Number of points between X (including X. So we can read and update while reading.
# Include <cstdio> # include <cstring> # include <algorithm> # define n 32000 + 10 using namespace STD; int A [n], ANS [n> 1]; int lowbit (int x) {return X & (-x);} int sum (int x) {int tot = 0; while (x> 0) {tot + = A [X]; X-= lowbit (x);} return tot;} void Update (int x, int v) {While (x <= N) {A [x] + = V; x + = lowbit (x) ;}} int main () {int N; while (scanf ("% d", & N )! = EOF) {int X, Y; memset (A, 0, sizeof (a); memset (ANS, 0, sizeof (ANS); For (INT I = 0; I <n; I ++) {scanf ("% d", & X, & Y); X ++; // 1 ~ The range of X. Ans [sum (x)] ++; Update (x, 1); // update with 1. } For (INT I = 0; I <n; I ++) printf ("% d \ n", ANS [I]);} return 0 ;}
Poj 2352 & HDU 1541 stars (tree array)