Code Forces 652D Nested Segments(离散化+树状数组)
来源:互联网 发布:淘宝少女前线初始号 编辑:程序博客网 时间:2024/05/18 20:32
Nested Segments
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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
41 82 34 75 6
output
3010
input
33 41 52 6
output
011
离散化+树状数组。
把所有区间按照右端点排序,然后统计左端点和右端点之间的已经包含的左端点个数,用树状数组求区间和会很快
#include <iostream>#include <string.h>#include <stdlib.h>#include <algorithm>#include <stdio.h>#include <math.h>using namespace std;#define MAX 2*100000struct Node{ int l,r; int pos;}a[MAX+5];int n;int num[2*MAX+5];int c[2*MAX+5];int ans[MAX+5];int s;int lowbit(int x){ return x&(-x);}void update(int x,int num){ while(x<=s) { c[x]+=num; x+=lowbit(x); }}int sum(int x){ int _sum=0; while(x>0) { _sum+=c[x]; x-=lowbit(x); } return _sum;}int cmp(Node a,Node b){ return a.r<b.r;}int main(){ scanf("%d",&n); memset(c,0,sizeof(c)); int cnt=0; for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].l,&a[i].r); a[i].pos=i; num[cnt++]=a[i].l; num[cnt++]=a[i].r; } sort(num,num+cnt); for(int i=1;i<=n;i++) { a[i].l=lower_bound(num,num+cnt,a[i].l)-num+1; a[i].r=lower_bound(num,num+cnt,a[i].r)-num+1; } sort(a+1,a+n+1,cmp); s=a[n].r; for(int i=1;i<=n;i++) { int num=sum(a[i].r)-sum(a[i].l-1); ans[a[i].pos]=num; update(a[i].l,1); } for(int i=1;i<=n;i++) { printf("%d\n",ans[i]); } return 0;}
0 0
- Code Forces 652D Nested Segments(离散化+树状数组)
- CodeForces 652D Nested Segments(树状数组+离散化)
- CodeForces 652D Nested Segments(离散化,树状数组)
- Codeforces 652D Nested Segments 【树状数组 + 离散化】
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- Codeforces 652D Nested Segments【离散化+思维+树状数组】
- Nested Segments codeforces 652D 树状数组 +离散化
- CodeForces 652D Nested Segments(树状数组 离散化)
- Nested Segments CodeForces 652D 树状数组+离散化
- CodeForces 652D Nested Segments 树状数组
- CodeForce 652D Nested Segments 树状数组
- CodeForces 652D Nested Segments (树状数组)
- Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- Codeforces 652D Nested Segments 树状数组离线处理
- codeforces_652D. Nested Segments(树状数组、二分)
- cf#ECR10-D. Nested Segments-树状数组+二分
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- Code Forces 755 D PolandBall and Polygon(思维+树状数组)
- Code Forces 652C Foe Pairs
- Bzoj4443:[Scoi2015]小凸玩矩阵:网络流
- 项目中代码健壮性感言
- oracle创建数据库sql脚本
- ubuntu下搭建nfs服务器
- Code Forces 652D Nested Segments(离散化+树状数组)
- PHP常用正则表达式汇总
- 干货分享:让你分分钟学会 javascript 闭包
- 闪回归档:
- 【计网】TCP的三次握手和四次挥手
- Android App 沉浸式状态栏解决方案
- http://www.codeceo.com/article/8-java-search-engine.html
- MFC工程上创建SOUI环境并生成一个窗口
- 苦逼程序猿的求职经历