poj2481 树状数组
2015-10-08 15:39
260 查看
树状数组模板题,如果有牛的区域能全覆盖目前牛的区域,那么该牛比目前牛强壮
首先按e从大到小排,如果一样按s从小到大排
a数组记录的是ans,及到x为止的前缀和
注意两个区域相等时结果也相等而不算全覆盖
首先按e从大到小排,如果一样按s从小到大排
a数组记录的是ans,及到x为止的前缀和
注意两个区域相等时结果也相等而不算全覆盖
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #include <cmath> #include <map> #include <vector> using namespace std; #define FOR(i, l, r) for(int i = l; i <= r; i++) #define REP(i, r, l) for(int i = r; i >= l; i--) typedef long long ll; double eps = 1e-6; const int inf = 0x3f3f3f3f; const int mod = 1e9+7; struct coww { int s,e,id; friend bool operator <(const coww &nd1,const coww &nd2) { if(nd1.e == nd2.e) return nd1.s < nd2.s; //如果e相等,s从小到大排 return nd1.e > nd2.e; //e从大到小排 } }cow[100010]; int c[100010]; int a[100010]; int lowbit(int x) { return x & (-x); } void modify(int x) //x位置修改(+1)后c[x]的修改 { while(x <= 100010) { c[x] ++ ; x += lowbit(x); } } int sum(int x) //x的前缀和 { int ret=0; while(x > 0) { ret += c[x]; x-=lowbit(x); } return ret; } int main() { int n; while(~scanf("%d",&n)) { if(n== 0) break; memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); FOR(i,1,n) { scanf("%d%d", &cow[i].s, &cow[i].e); cow[i].s++ , cow[i].e++; cow[i].id=i; } sort( cow+1 , cow+n+1); FOR(i,1,n) { if(cow[i].s == cow[i-1].s &&cow[i].e == cow[i-1].e) a[cow[i].id]=a[cow[i-1].id]; else a[cow[i].id]=sum(cow[i].s); modify(cow[i].s); } FOR(i,1,n-1) printf("%d ",a[i]); printf("%d\n",a ); } return 0; }
相关文章推荐
- 在实现文本框只能输入数字和小数点的基础上实现了价格样式(保留两位小数)
- 韩顺平 java 第28讲 面试题
- 三星手机拍照,图片旋转问题的解决
- img及父元素(容器)实现类似css3中的background-size:contain / background-size:cover
- Oracle常用操作-----(一)
- 在windows上部署使用Redis(摘录)
- IOS打开pdf文件
- angularjs input上传图片前获取图片的Size
- Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
- 对Properties文件的增删改查操作工具类
- Opencv对XML和YAML文件实现I/O操作
- 任意多边形面积的计算
- 查找(顺序,有序)总结
- linux kernel docs
- 【笔记】利用CSS3特性实现圆角DIV
- iOS 二维码的制作(libqrencode)
- WIN8使用笔记
- java观察者模式(转)
- ccnp学习笔记
- 栈——PowerShell版