Nested Segments
题目描述
You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.
输入
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.
输出
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
样例
Input
4
1 8
2 3
4 7
5 6
Output
3
0
1
0
Input
3
3 4
1 5
2 6
Output
0
1
1
题意
给定一条线上的 n 个分段。所有分段的端点均不重合。对于每个分段,找出它所包含的分段数目。
直接离散化右端点维护一个树状数组
AC代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define ber(i,s,e) for(int i=(s); i<=(e); i++)
#define rep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
const int mod = 1e9+7;
const double eps = 1e-8;
void fe() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt","w",stdout);
#endif
}
LL read()
{
LL 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<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
struct node
{
int l, r;
int id;
bool operator <(const node &x)const {
if(x.l == l)
return x.r > r;
return x.l < l;
}
}p[MAXN];
int n;
vector<int> v;
int ans[MAXN], sum[MAXN];
int lowbit(int x) {
return x&(-x);
}
void add(int x) {
while(x <= n) {
sum[x]++;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while(x) {
ans += sum[x];
x -= lowbit(x);
}
return ans;
}
int main(int argc, char const *argv[])
{
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i].l >> p[i].r;
p[i].id = i;
v.push_back(p[i].r);
}
sort(v.begin(), v.end());
auto e = unique(v.begin(), v.end());
for(int i = 1; i <= n; i++)
p[i].r = lower_bound(v.begin(),e,p[i].r)-v.begin()+1;
sort(p+1, p+1+n);
for(int i = 1; i <= n; i++) {
ans[p[i].id] = query(p[i].r);
add(p[i].r);
}
for(int i = 1; i <= n; i++)
cout << ans[i] << endl;
return 0;
}