Educational Codeforces Round 10 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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.