Nested Segments CodeForces
来源:互联网 发布:手机淘宝付款方式 编辑:程序博客网 时间:2024/06/01 08:30
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.
Example
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
/* 树状数组,离散化 对于原始段,按照左端点从大到小排序,如果左端点相同,则按照右端点从小到大进行排序, 这样的话,对于段 i 来说,可能包括它的段就是 x ( x > i ) 段; 要求段 i 包含了多少段,那么可以将每个段的右端点赋值 1, 于是排好序后,对于每个段,统计下它右端点前的和,即为结果,然后更新 但是,右端点的值很大,所以用按从小到大对应一个小的值,也就是离散化; */#include<bits/stdc++.h>using namespace std;const int maxn=200007*4;int sum[maxn];struct node{ int x,y,id;};node a[maxn];int ans[maxn];int n;int low_bit(int x){ return x&(-x);}int update(int x){ while(x<=n) { sum[x]++; x+=low_bit(x); }}int query(int x){ int ans=0; while(x>0) { ans+=sum[x]; x-=low_bit(x); } return ans;}int cmp(node a,node b){ return a.y < b.y;}int cmp1(node a,node b){ if(a.x != b.x ) return a.x > b.x; else return a.y < b.y;}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].id=i; } sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) a[i].y=i; sort(a+1,a+1+n,cmp1); for(int i=1;i<=n;i++) { ans[a[i].id] = query(a[i].y); update(a[i].y); } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0;}
阅读全文
0 0
- Nested Segments CodeForces
- Nested Segments CodeForces
- codeforces 652D Nested Segments
- Educational Codeforces Round 10 D. Nested Segments
- Educational Codeforces Round 10 D Nested Segments
- Educational Codeforces Round 10 D.Nested Segments
- CodeForces 652D Nested Segments 树状数组
- Educational Codeforces Round 10-D. Nested Segments
- CodeForces D. Nested Segments【逆序对类型】
- Nested Segments
- Codeforces 652D Nested Segments 【树状数组 + 离散化】
- CodeForces 652D Nested Segments (树状数组)
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- CodeForces 652D Nested Segments(树状数组+离散化)
- CodeForces 652D Nested Segments(离散化,树状数组)
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- Codeforces 652D Nested Segments【离散化+思维+树状数组】
- Nested Segments codeforces 652D 树状数组 +离散化
- (个人)AR电子书系统创新实训第五周(1)
- web移动设备前端小结
- Libevent源码分析-----evthread_notify_base通知主线程
- @Autowired和@Resource 区别以及工作原理
- 11. Container With Most Water
- Nested Segments CodeForces
- html标签
- 基于TI的TCP/IP协议栈---NDK
- Libevent源码分析-----超时event的处理
- 解决EventBus中接收方法中无法更新UI的问题
- Spark-Streaming中累加
- Libevent源码分析-----Libevent时间管理
- hdoj1080 Human Gene Functions (DP,map)
- 冒泡排序