http://poj.org/problem?id=2352
每读一个点(x,y),先查询区间[0,x]上有多少个点,然后把位置x上星星的数目+1
完整代码:
/*438ms,696KB*/ #include <cstdio> #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 #define root 0, 32000, 1 int sum[65536], ans[15005]; inline void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void update(int p, int addval, int l, int r, int rt) { if (l == r) { sum[rt] += addval; return; } int m = (l + r) >> 1; if (p <= m) update(p, addval, lson); else update(p, addval, rson); pushup(rt); } int query(int ql, int qr, int l, int r, int rt) { if (ql <= l && r <= qr) { return sum[rt]; } int sum = 0, m = (l + r) >> 1; if (ql <= m) sum += query(ql, qr, lson); if (m < qr) sum += query(ql, qr, rson); return sum; } int main() { int n, i, x; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d%*d", &x); ++ans[query(0, x, root)]; update(x, 1, root); } for (i = 0; i < n; ++i) printf("%d\n", ans[i]); return 0; }