This question seems to be an original question of poj... first, we can determine that if a line segment is included by another line segment, the left endpoint of the line segment containing it must be smaller than or equal to this line segment. Therefore, we sort the left endpoint from small to large, and the left endpoint is the same as the right endpoint from large to small, in this way, all the line segments that contain the I-th line segment must be updated before.
Then we directly require that the right section of the preceding line segment be greater than the line segment. Because the tree array is used, we use a number to subtract the right endpoint, so the right endpoint is closer to the left, so we can make the tree-like array. When the attributes of the two line segments are the same, we need to assign the previous answer to the next line segment and then update it.
The Code is as follows:
#include <cstring>#include <cstdio>#include <algorithm>#include <cstdlib>#include <map>using namespace std;int N, c[400005], ret[200005], idx;map<int,int>mp;struct Node{ int l, r, NO; bool operator < (Node temp) const { if (l != temp.l) return l < temp.l; return r > temp.r; }}e[200005];struct High{ int h; bool operator < (High temp) const { return h < temp.h; } bool operator == (High temp) const { return h == temp.h; }}H[400005];int lowbit(int x){ return x & -x;}void add(int x, int val){ for (int i = x; i <= 400000; i += lowbit(i)) { c[i] += val; }}int sum(int x){ int ret = 0; for (int i = x; i > 0; i -= lowbit(i)) { ret += c[i]; } return ret;}int main(){ while (scanf("%d", &N) == 1) { memset(c, 0, sizeof (c)); memset(ret, 0, sizeof (ret)); mp.clear(); idx = -1; for (int i = 0; i < N; ++i) { scanf("%d %d", &e[i].l, &e[i].r); e[i].NO = i; H[++idx].h = e[i].l, H[++idx].h = e[i].r; } sort(H, H+idx+1); idx = unique(H, H+idx+1)-H; for (int i = 0; i < idx; ++i) { mp[H[i].h] = idx - i; } sort(e, e + N); for (int i = 0; i < N; ++i) { int pos = mp[e[i].r]; if (i > 0 && e[i].l == e[i-1].l) { if (e[i].r == e[i-1].r) { ret[e[i].NO] = ret[e[i-1].NO]; } else { ret[e[i].NO] = sum(pos); } } else if (i > 0 && e[i].l > e[i-1].l) { ret[e[i].NO] = sum(pos); } else { ret[e[i].NO] = 0; } add( pos, 1 ); } for (int i = 0; i < N; ++i) { if (i == 0) { printf("%d", ret[i]); } else { printf(" %d", ret[i]); } } puts(""); } return 0;}