codeforces_652D. Nested Segments(树状数组、二分)
来源:互联网 发布:会展设计软件 编辑:程序博客网 时间:2024/05/09 04:37
将点排序后,用二分找出线段两个端点在a数组的位置,将线段的末尾端点标1,用树状数组求出区间和,最后再按原先顺序输出。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define inf 0x3f3f3f3f#define maxn 200010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct node{ int l,r,ans,id;}p[2*maxn];int n,top=0;int c[2*maxn];int a[2*maxn];bool cmp1(node a,node b){ return a.l<b.l;}bool cmp2(node a,node b){ return a.id<b.id;}int update(int i,int x){ while(i<=2*n) { c[i]+=x; i+=(i&(-i)); }}int Sum(int pos){ int sum=0; while(pos>0) { sum+=c[pos]; pos-=(pos&(-pos)); } return sum;}int main(){ Si(n); mem(c,0); int i; for(i=1;i<=n;i++) { int l,r; Si(l);Si(r); a[top++]=l; a[top++]=r; p[i].l=l; p[i].r=r; p[i].id=i; } sort(a,a+top); for(i=1;i<=n;i++) { p[i].l=upper_bound(a,a+top,p[i].l)-a; p[i].r=upper_bound(a,a+top,p[i].r)-a; } sort(p+1,p+n+1,cmp1); for(i=1;i<=n;i++)update(p[i].r,1); for(i=1;i<=n;i++) { if(p[i].l+1>p[i].r-1)p[i].ans=0; else { p[i].ans=Sum(p[i].r-1)-Sum(p[i].l+1); } update(p[i].r,-1); } sort(p+1,p+n+1,cmp2); for(i=1;i<=n;i++) Pi(p[i].ans); return 0;}
0 0
- codeforces_652D. Nested Segments(树状数组、二分)
- cf#ECR10-D. Nested Segments-树状数组+二分
- CodeForces 652D Nested Segments (树状数组)
- CodeForces 652D Nested Segments(树状数组+离散化)
- CodeForces 652D Nested Segments(离散化,树状数组)
- CodeForces 652D Nested Segments 树状数组
- CodeForce 652D Nested Segments 树状数组
- Code Forces 652D Nested Segments(离散化+树状数组)
- Codeforces 652D Nested Segments 【树状数组 + 离散化】
- 初识树状数组 Educational Codeforces Round 10 D - 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 树状数组离线处理
- Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- 【Educational Codeforces Round 10D】【树状数组】Nested Segments 每条线段内部有多少条线段
- 数据结构之栈
- TODO
- android缓存框架ASimpleCache
- 删除win7的服务
- 抄点东西
- codeforces_652D. Nested Segments(树状数组、二分)
- OC - 正则表达式(身份证号详细验证和粗略验证两种表示,后期会持续添加)
- mysql常用命令
- Maya Render tutorial
- 如何使用iOS 开发证书 和 Profile 文件
- 使用TypedArray设置控件大小注意问题
- OpenStack笔记四 Neutron
- 手机安装apk出现INSTALL_FAILED_INSUFFICIENT_STORAGE
- 【BZOJ1426】收集邮票【期望DP】