线段树版: #include <stdio.h> #include <string.h> #include <conio.h> #define N 15000 #define M 32000 struct t { int l; int r; int w; }tree[M * 3];
void maketree(int s, int t, int n) { tree[n].l = s; tree[n].r = t; tree[n].w = 0; if(t == s) return; int mid = (tree[n].l + tree[n].r) / 2; maketree(s, mid, n * 2); maketree(mid + 1, t, n * 2 + 1); } /*找在x坐标为t的星星的左下方有多少颗星星*/ int find(int s, int t, int n) { if(tree[n].l == s && tree[n].r == t) return tree[n].w; else { int sum = 0; int mid = (tree[n].l + tree[n].r ) >> 1; if(s > mid ) sum += find(s, t, n * 2 +1); else if(t <= mid) sum += find(s, t, n * 2); else sum += find(s, mid, n*2) + find(mid+1, t, n*2+1); return sum; } } /*更新线段树,一路上节点的w都增一*/ void insert(int x, int n) { tree[n].w++; if(tree[n].l == tree[n].r && tree[n].r == x) return; int mid = (tree[n].l + tree[n].r ) >> 1; if(x <= mid) insert(x, n * 2); else insert(x, n * 2 + 1); }
int main() { int i; int n, max; int a[N], b, ans[M];
freopen("in.txt", "r", stdin); scanf("%d", &n); max = 0; for(i=0; i<n; i++) { scanf("%d%d", &a[i], &b); max = max < a[i] ? a[i] : max; } maketree(0, max, 1); memset(ans, 0, sizeof(ans)); for(i=0; i<n; i++) { ans[find(0, a[i], 1)]++; insert(a[i], 1); } for(i=0; i<n; i++) { printf("%d\n", ans[i]); } getch(); return 0; } memory: 1108 time:172
|