求助站外题
查看原帖
求助站外题
363006
wangyibo201026楼主2022/5/7 18:22

题目:POJ 2481。

代码:

#include<bits/stdc++.h>

#define end '\n'

using namespace std;

inline int read() {
	int x = 0, f = 1;
  char ch = getchar();
	while(ch < '0' || ch > '9'){
    if(ch == '-'){
      f = -1;
    }
    ch = getchar();
  }
	while(ch >= '0' && ch <= '9'){
    x = x * 10 + ch - 48;
    ch = getchar();
  }
	return x * f;
}

const int N = 1e5 + 5;

int n;

struct Node{
	int s, e, id, ans;
}a[N];

bool cmp(Node x, Node y){
	return x.s < y.s;
}

bool cmp2(Node x, Node y){
	return x.id < y.id;
}

int tree[N];

int lowbit(int x){
	return x & -x;
}

void update(int x, int k){
	while(x <= 100000){
		tree[x] += k;
		x += lowbit(x);
	}
}

int query(int x){
	int ans = 0;
	while(x){
		ans += tree[x];
		x -= lowbit(x);
	}
	return ans;
}

void Solve(){
	while(n = read()){
		if(!n){
			return ;
		}
		for(int i = 1; i <= n; i++){
			a[i].s = read(), a[i].e = read();
			a[i].id = i;
		}
		sort(a + 1, a + 1 + n, cmp);
		for(int i = 1; i <= n; i++){
			a[i].ans = query(100000) - query(a[i].e - 1);
			update(a[i].e, 1);
		}
		sort(a + 1, a + 1 + n, cmp2);
		for(int i = 1; i < n; i++){
			printf("%lld ", a[i].ans);
		}
		printf("%lld\n", a[n].ans);
	}
}

int main(){
  Solve();
  return 0;
}
2022/5/7 18:22
加载中...