POJ2481->树状数组
来源:互联网 发布:程序员创业项目 编辑:程序博客网 时间:2024/06/07 16:01
题意:给N个节点的S和E,求对于每个节点,其他节点和它的关系满足Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj 的个数
题解:由于E的关系递减,S的关系递增,因此可以先对E排序,然后利用树状数组更新并查询每个Sj在其之前的Si的个数就是问题的解
#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>using namespace std ;#define MAX 100005int T[MAX] ;int ans[MAX] ;int len ;int lowbit(int x){return (-x)&x ;}int Query(int x){int ret = 0 ;while(x > 0){ret += T [x] ;x -= lowbit(x) ;}return ret ;}void Update(int x , int d){while(x <= len){T[x] += d ;x += lowbit(x) ;}}struct Node{int E , S ;int number ;}cow[MAX];bool comp(const struct Node &x , const struct Node &y){if(x.E == y.E) return x.S < y.S ;return x.E > y.E ;}int main(){while(scanf("%d" , &len)!=EOF , len){for(int i = 1 ; i <=len ; i ++){scanf("%d%d" , &cow[i].S , &cow[i].E) ;cow[i].S ++ ;cow[i].E ++ ;cow[i].number = i ;}sort(cow + 1, cow + 1 + len ,comp) ;memset(T , 0 , sizeof(T)) ;for(int i = 1 ; i <= len ; i ++){if(i > 1 && cow[i].S == cow[i-1].S && cow[i].E == cow[i-1].E){ans[cow[i].number] = ans[cow[i-1].number] ;}elseans[cow[i].number] = Query(cow[i].S) ; Update(cow[i].S , 1) ;}for(int i = 1 ; i <= len ; i ++){if(i == 1) printf("%d", ans[i]);else printf(" %d", ans[i]);}printf("\n");}return 0 ;}
0 0
- poj2481-树状数组
- POJ2481树状数组
- poj2481 树状数组
- poj2481~Cows~(树状数组)
- POJ2481:Cows(树状数组)
- poj2481 树状数组
- poj2481 树状数组
- POJ2481 Cows 树状数组
- poj2481 Cows (树状数组)
- POJ2481->树状数组
- poj2481树状数组
- [树状数组]poj2481 Cows
- 树状数组poj2481
- [POJ2481] cows - 树状数组
- POJ2481 Cows 树状数组
- POJ2481 树状数组+sort
- poj2481-树状数组的应用
- poj2481之排序+树状数组
- Python学习3--返回函数 、匿名函数、装饰器、偏函数
- 屏幕录像工具之屏幕截图
- 前端小技巧
- 精通C#:Chapter5 封装-学习笔记
- android异常——NoClassDefFoundError
- POJ2481->树状数组
- Intent
- file_get_contents("php://input")的使用方法
- SVG学习笔记(1)
- MYSQL基础知识与常用方法
- Linux中的信号(1)
- (17)HTML标签详解之<a> <nav>
- File类
- iOS-应用生命周期