POJ_2481(树状数组)
来源:互联网 发布:怎样开淘宝网店需要多少钱 编辑:程序博客网 时间:2024/04/30 09:27
本题的关键在于要考虑到两个区间完全相同的情况,其他的不用考虑太多。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;const int maxn = 100000 + 300;int c[maxn];int ans[maxn];struct cow{int s;int e;int in;bool operator < (const cow& z) const{if(z.s == s) return z.e < e;else return z.s > s;}}co[maxn], pri;int lowbit(int x){return x & (-x);}int sum(int x){int t = 0;while(x > 0){t += c[x];x -= lowbit(x);}return t;} int update(int x){while(x < maxn){c[x]++;x += lowbit(x);}return 0;}int main(){int tot;while(scanf("%d", &tot) && tot){memset(c, 0, sizeof(c));memset(co, 0, sizeof(co));memset(ans, 0, sizeof(ans));for(int i = 0; i < tot; ++i){scanf("%d%d", &co[i].s, &co[i].e);co[i].s++; co[i].e++;co[i].in = i;}sort(co, co+tot);pri.s = co[0].s; pri.e = co[0].e; pri.in = co[0].in;co[0].e++;ans[co[0].in] = 0 - sum(co[0].e);update(co[0].e);for(int i = 1; i < tot; ++i){ int s = co[i].e;s++;if(pri.e == co[i].e && pri.s == co[i].s){ans[co[i].in] = ans[co[i-1].in];}else{ans[co[i].in] = i - sum(s-1);}pri.e = co[i].e; pri.s = co[i].s; pri.in = co[i].in;update(s);}int i;for(i = 0; i < tot - 1; ++i){printf("%d ", ans[i]);} printf("%d\n", ans[i]);}return 0;}
0 0
- POJ_2481(树状数组)
- poj_2481,Cows,树状数组
- poj_2481 Cows
- (转)树状数组
- 树状数组(转载)
- Stars(树状数组)
- 树状数组(interval)
- (转)树状数组
- 树状数组(2)
- 树状数组(3)
- 树状数组(4)
- 树状数组(5)
- 树状数组(6)
- poj2352Stars(树状数组)
- POJ2299(树状数组)
- hdu2492 (树状数组)
- 树状数组()
- LA4329(树状数组)
- Unity编辑器扩展之RequireComponent等详解
- HDOJ 1240 Asteroids! 【BFS+优先队列】
- 在.NET Framework 2.0即以下平台下操作UDT
- 五大算法之三--贪心算法
- quartus中引脚配置
- POJ_2481(树状数组)
- linux中用到的头文件标注
- DHCP option43之sub-option设置
- C# Code
- java抽象类
- 五大算法之四--回溯法
- Web Service读写SQL数据库
- Leetcode解题-树(5.0.0)基础类
- SqlServer 无法为可更新的订阅设置发布服务器登录名 sp_link_publication