POJ2481-Cows
来源:互联网 发布:淘宝营类目怎么修改 编辑:程序博客网 时间:2024/05/21 11:36
首先将所有的区间按终点从大到小排序,如果终点相同则按起点从小到大排序,然后用树状数组维护起点总数即可。
由于已经按照终点排序,已经插入区间的终点一定大于当前的区间,因此只需查找起点比当前起点小的区间即为所要求的答案。
#include <cstdio>#include <cstring>#include <algorithm>const int maxn = 100000+1;#define lowbit(i) ((i) & -(i))struct itv { int id, begin, end;};itv cow[maxn+10];int bit[maxn+10];int cnt[maxn+10];bool comp(const itv &a, const itv &b) { if (a.end == b.end) { return a.begin < b.begin; } return a.end > b.end;}void add(int i, int delta) { for (int j = i; j <= maxn; j += lowbit(j)) { bit[j] += delta; }}int sum(int k) { int ans = 0; for (int i = k; i > 0; i -= lowbit(i)) { ans += bit[i]; } return ans;}int main(int argc, char const *argv[]) { int n; while (scanf("%d", &n) == 1 && n) { memset(bit, 0, sizeof(bit)); for (int i = 0; i < n; i++) { scanf("%d%d", &cow[i].begin, &cow[i].end); cow[i].id = i; cow[i].begin++; cow[i].end++; } std::sort(cow, cow + n, comp); for (int i = 0; i < n; i++) { if (i >= 1 && cow[i].begin == cow[i-1].begin && cow[i].end == cow[i-1].end) { cnt[cow[i].id] = cnt[cow[i-1].id]; } else { cnt[cow[i].id] = sum(cow[i].begin); } add(cow[i].begin, 1); } for (int i = 0; i < n; i++) { if (i) { putchar(' '); } printf("%d", cnt[i]); } putchar('\n'); } return 0;}
0 0
- poj2481 Cows
- POJ2481 Cows
- POJ2481 Cows
- POJ2481 Cows
- poj2481--Cows
- POJ2481 Cows
- poj2481 Cows
- poj2481-Cows
- poj2481 Cows
- poj2481 Cows
- poj2481 Cows
- POJ2481-Cows
- POJ2481 Cows
- POJ2481-Cows
- poj2481——Cows
- poj2481~Cows~(树状数组)
- POJ2481——Cows
- poj2481 Cows&&poj2352 Stars
- Java中几个定义的区别
- Android_UI:沉浸式 ImmersiveMode
- hdu1003
- 使用nginx搭建点播和直播流媒体服务器
- Vertx中的verticle详解
- POJ2481-Cows
- Smarty变量
- Linux中常用C/C++一些头文件的作用
- vs2013激活码,亲测可用
- CentOS7 从零搭建 Cassandra3.7集群
- bzoj1010玩具装箱toy
- Redis提供的API
- Smarty 配置文件
- 从零学React Native之01创建第一个程序