Nested Segments CodeForces
来源:互联网 发布:ubuntu 输入法安装 编辑:程序博客网 时间:2024/06/05 00:37
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.
41 82 34 75 6
3010
33 41 52 6
011
给定n条线段,问每条线段包含了几条线段,先根据左端点值从大到小排序,相等按照右端点值从小到大,这样排序后前面的区间相对与后面的区间左端点可以满足条件,只需求前面的线段里有几个右端点也满足的即可,这一步可以用树状数组维护,不过这个题的右端点值范围较大,所以要先将右端点值离散化处理,在用树状数组维护
#include<bits/stdc++.h>using namespace std;struct node{int l,r;int index;}num[200005];int f[200005],ans[200005],n;int sum(int x){int sum=0;while(x>0){sum+=f[x];x-=x&-x;}return sum;}int add(int t,int x){while(t<=n){f[t]+=x;t+=t&-t;}}int cmp1(node u,node v){if(u.l==v.l){return u.r<v.r;}return u.l>v.l;}int cmp(node u,node v){return u.r<v.r;}map<int,int>p;int main(){int i;cin>>n;for(i=0;i<n;i++){scanf("%d %d",&num[i].l,&num[i].r);num[i].index=i;}int cnt=1;sort(num,num+n,cmp);for(i=0;i<n;i++){if(!p.count(num[i].r)){p[num[i].r]=cnt++;}}sort(num,num+n,cmp1);for(i=0;i<n;i++){ans[num[i].index]=sum(p[num[i].r]);add(p[num[i].r],1);}for(i=0;i<n;i++){printf("%d\n",ans[i]);}return 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 树状数组 +离散化
- 图解Deep Learning
- 老薛主机优惠码
- EA&UML日拱一卒--序列图(Sequence Diagram)::交互概述图
- Android中的微信支付
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
- Nested Segments CodeForces
- oj 2357: 贺大和他的朋友们
- openGL ES进阶教程(四)用openGL ES+MediaPlayer 渲染播放视频+滤镜效果
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 从计算机底层考虑是如何实现‘’整数‘’的加减法
- 【Android学习笔记系列】BaseAdapter适配器的介绍、使用及优化(详细)
- 582. Kill Process
- CC3220学习笔记---中断
- JavaScript--2048游戏二()