At first thought, the total feeling is DP, but finally did not think of anything. And a violent hand.
Then start writing the line segment tree and the result is timed out. Feel a problem with the way your line tree is written. I'll write it sometime. Let's put the tree-like array in the first place.
~~~~~~~~~~~~~~~~~~~~~~~~
The tree array does not understand to see Rujia's Great White Book, that picture is very clear.
The main idea: the coordinates of the stars are given in the order of Y increments, and the points at the lower left of the dots represent the levels of this point, and how many 0~n-1 are asked? In fact, y is practically wooden.
Title Link: http://poj.org/problem?id=2352
http://acm.hdu.edu.cn/showproblem.php?pid=1541
The idea of a tree array is that every inquiry is the equivalent of asking how many points there are between 1~x (including x). Then we will read it and update it as well.
#include <cstdio> #include <cstring> #include <algorithm> #define N 32000+10using 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%d", &x,&y); x + +; The range of 1~x. ans[Sum (x)]++; Update (x,1); Update with A. } for (int i=0;i<n;i++) printf ("%d\n", Ans[i]); } return 0;}
POJ 2352 && HDU 1541 Stars (tree-like array)