codeforces 652D Nested Segments 离散化+树状数组
2016-03-27 18:57
330 查看
题意:给你若干个区间,询问每个区间包含几个其它区间
分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和
注:(都是套路)
View Code
分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和
注:(都是套路)
#include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long LL; const int N=2e5+5; const int INF=0x3f3f3f3f; int a[N<<1],n,d; struct pair{ int l,r,id; bool operator<(const pair &e)const{ return r<e.r; } }p ; int c[N<<1]; void change(int x){ for(int i=x;i<=d;i+=i&(-i)) ++c[i]; } int query(int x){ int ans=0; for(int i=x;i>0;i-=i&(-i)) ans+=c[i]; return ans; } int res ; int main(){ scanf("%d",&n); int tot=0; for(int i=1;i<=n;++i){ scanf("%d%d",&p[i].l,&p[i].r),p[i].id=i; a[++tot]=p[i].l; a[++tot]=p[i].r; } sort(a+1,a+1+tot); d=1; for(int i=2;i<=tot;++i) if(a[i]!=a[i-1])a[++d]=a[i]; sort(p+1,p+1+n); for(int i=1;i<=n;++i){ int r=lower_bound(a+1,a+1+d,p[i].r)-a; int l=lower_bound(a+1,a+1+d,p[i].l)-a; res[p[i].id]=query(r)-query(l-1); change(l); } for(int i=1;i<=n;++i) printf("%d\n",res[i]); return 0; }
View Code
相关文章推荐
- 数组02
- 幸福其实很简单
- 第58课:使用Java和Scala在IDE中开发DataFrame实战学习笔记
- Lintcode 合并两个排序的链表
- CentOS7下JDK 安装错误 could not find libjava.so
- JVMGC——垃圾收集器的分类
- onWindowFoucusedChanged
- Android studio遇到在真机运行时出现adb not respond if you would like to retry问题解决方法
- Uva299——Train Swapping
- resin 4.0数据源的配置
- 环形数组最大子数组之和
- hdu5652
- cornerStone查看日志出错:Could not contact repository to read the latest log entries
- 数据库的基本操作
- JAVA基础总结(1)
- Swift2中的标准函数
- Android Xfermode 学习笔记
- BZOJ_P1176 [Balkan2007]Mokia(CDQ分治+树状数组)
- 你真的会写单例模式吗——Java实现
- PAT1051. Pop Sequence (25)