Educational Codeforces Round 10 D.Nested Segments

2799 단어
D. Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
Examples
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라인(n<=2*10^5)을 드리겠습니다. 라인마다 [l,r]을 하나씩 주고 그의 좌우 구간을 대표합니다. 출력은 라인마다 얼마나 많은 라인을 그에게 완전히 덮어씌울 수 있습니까?
 
사고방식: r에 대해 이산화한 다음에 L에 따라 조회를 크고 작은 것으로 정렬하고 각 r에 대한 조회는 그의 r보다 몇 개 작다
(트리 배열 접두사 및)
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=200100;
int C[maxn],Hash[maxn*2],ans[maxn];
struct Point{
    int x,y,id;
}a[maxn];
int n;

bool cmp(Point u,Point v){
    if(u.x!=v.x)
        return u.x>v.x;
    return u.y>v.y;
}

void add(int x){
    while(x<=n){
        C[x]+=1;
        x+=(x&-x);
    }
}

int sum(int x){
    int ret=0;
    while(x>0){
        ret+=C[x];
        x-=(x&-x);
    }
    return ret;
}

int main(){
    int tot=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].id=i;
        Hash[++tot]=a[i].y;
    }
    sort(Hash+1,Hash+tot+1);
    int m=unique(Hash+1,Hash+tot+1)-Hash;
    for(int i=1;i<=n;i++)
        a[i].y=lower_bound(Hash+1,Hash+m,a[i].y)-Hash;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        ans[a[i].id]=sum(a[i].y);
        add(a[i].y);
    }
    for(int i=1;i<=n;i++)
        printf("%d
",ans[i]); return 0; }

좋은 웹페이지 즐겨찾기