UESTC 1584 Washi与Sonochi的约定 树状数组+排序
来源:互联网 发布:java编程思想pdf超清版 编辑:程序博客网 时间:2024/06/05 19:26
Washi与Sonochi的约定
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others)Submit Status
Sonochi,明年再一起看烟花。——WashioSumi
为了实现和Sonochi的约定,Washi必须要打败眼前强大的怪物。
怪物分布在二维平面上,
某个怪物的rank被定义为x坐标不大于其x坐标,且y坐标不大于其y坐标的怪物的数量。(不含其自身)
Washi要你输出n行,每行一个整数,分别代表rank为0~n−1的怪物数量。
Input
输入第一行为一个正整数n,接下来n行,第ii行两个整数xi、yi,表示一个怪物的坐标。
保证输入的坐标两两不同。
Output
包含n行,每行一个整数,第ii行的数代表rank为i−1i−1的怪物数量。Sample input and output
Sample Input Sample Output5
1 1
5 1
7 1
3 3
5 5
1
2
1
1
0
Hint
1≤n≤1000001≤xi≤100000
1≤yi≤100000
Source
17暑假前集训-数据结构专题 By AutSky_JadeK
2017 UESTC Training for Data Structures
UESTC 1584 Washi与Sonochi的约定
My Solution
题意:在二维平面上,某个点的rank被定义为x坐标不大于其x坐标,且y坐标不大于其y坐标的怪物的数量。
(不含其自身),要求输出n行,每行一个整数,分别代表rank为0~n^1的怪物数量。
树状数组+排序
把所有的坐标读入之后,
按照x为第优先级,y为第二优先级,都是从小到大排序,
只从从0~n-1扫一遍,
此时(i时)树状数组里的点的x值,都不比val[i].x大,//这题所有坐标都不同
所以get(val[i].y)即可得到,所有x坐标不大于vali,且y坐标小于vali的坐标(点)的个数,
然后把val[i].y插入到树状数组里。
扫一遍即可得到所有答案。
复杂度 O(nlogn)
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int MAXN = 1e5 + 8;struct p{ int x, y;}val[MAXN];inline bool cmp(const p &a, const p &b){ if(a.x != b.x) return a.x < b.x; else return a.y < b.y;}int _rank[MAXN];//int Tree[MAXN + 8];inline int lowbit(int x){ return (x & -x);}inline void add(int x, int value){ for(int i = x; i < MAXN; i += lowbit(i)){ Tree[i] += value; }}inline int get(int x){ int res = 0; for(int i = x; i; i -= lowbit(i)){ res += Tree[i]; } return res;}int main(){ #ifdef LOCAL freopen("d.txt", "r", stdin); //freopen("d.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL //ios::sync_with_stdio(false); cin.tie(0); int n, i; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%d%d", &val[i].x, &val[i].y); } //!害怕,"输入的坐标两两不同",为什么不去掉这句话呢,Y^_^Y sort(val, val + n, cmp); for(i = 0; i < n; i++){ _rank[get(val[i].y)]++; add(val[i].y, 1); } for(i = 0; i < n; i++){ printf("%d\n", _rank[i]); } #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
阅读全文
0 0
- UESTC 1584 Washi与Sonochi的约定 树状数组+排序
- POJ 2352 Stars & UESTC 1584 Washi与Sonochi的约定 排序+树状数组
- UESTC OJ1217(树状数组)
- 2016 UESTC Training for Data Structures E - 卿学姐与城堡的墙 树状数组求逆序对、离散化
- 2016 UESTC Training for Data Structures E - 卿学姐与城堡的墙 CDOJ 1341 树状数组 逆序对 离散化
- UESTC 841 休生伤杜景死惊开(树状数组)
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 树状数组+离线操作
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 CDOJ 1342 离线树状数组
- 2016 UESTC Training for Data Structures O - 卿学姐种美丽的花 树状数组+等差数列
- UESTC 1583 曜酱的心意 树状数组求逆序数
- 树状数组------冒泡排序的交换次数
- UESTC 1073 秋实大哥与线段树 线段树&&改值与区间和 or 树状数组
- poj1990 树状数组+排序
- UESTC -- 841 休生伤杜景死惊开(树状数组 逆序数)
- UESTC 838 母仪天下(树状数组)
- UESTC 842 天下归晋(树状数组)
- UESTC 1601 艾尔大停电2 二维树状数组+区间更新
- CDOJ1583-树状数组 (2017 UESTC Training for Data Structures)
- [leetcode: Python]
- python ggplot使用文档(2)——gemo_abline,gemo_area,gemo_bar
- 四大组件浅析(二)——Service服务的回顾
- 使用 Selenium 将滚动条移动到目标元素位置的方法
- 文件选择
- UESTC 1584 Washi与Sonochi的约定 树状数组+排序
- mips处理器linux内核pci初始化和设备枚举详解
- ionic3/2 使用modal自定义弹出框
- 卡尔曼滤波 -- 从推导到应用
- Activiti快速入门
- Java中的取余运算和取模运算
- 数据结构-线性表
- 利用jquery写的AJAX小例子
- mysql中的case,when.........