//15000个坐标,对于每个坐标,其左下方向(含平 垂)坐标的个数就是它的level数
//输出各个level坐标个数
//之前做过线段树。。现在暴个treap
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cstdio>using namespace std;#define MX 15010int size, root;
struct Node
{int l, r, key, rand_fix;int countl, countr; //左右子树的节点数
}node[MX];int n, result[MX];
struct Point
{int x, y, p;
}point[MX];
bool _cmp(struct Point a, struct Point b)
{if(a.x != b.x) return a.x < b.x;return a.y < b.y;
}
//left rotate
void l_rot(int &index)
{int y = node[index].r;node[index].r = node[y].l;node[index].countr = node[y].countl;node[y].l = index;node[y].countl = node[index].countl + node[index].countr + 1;index = y;
}
//right rotate
void r_rot(int &index)
{int y = node[index].l;node[index].l = node[y].r;node[index].countl = node[y].countr;node[y].r = index;node[y].countr = node[index].countl + node[index].countr + 1;index = y;
}
void insert(int &index, int nkey)
{if(index == -1){index = ++size;node[index].l = node[index].r = -1;node[index].rand_fix = rand();node[index].key = nkey;node[index].countl = node[index].countr = 0;return ;}if(nkey < node[index].key){node[index].countl++;insert(node[index].l, nkey);if(node[node[index].l].rand_fix > node[index].rand_fix)r_rot(index);}else{node[index].countr++;insert(node[index].r, nkey);if(node[node[index].r].rand_fix > node[index].rand_fix)l_rot(index);}
}
bool remove(int &index, int nkey)
{if(index == -1) return false;if(nkey < node[index].key)return remove(node[index].l, nkey);else if(nkey > node[index].key)return remove(node[index].r, nkey);if(node[index].l == -1 && node[index].r == -1)index = -1;else if(node[index].l == -1)index = node[index].r;else if(node[index].r == -1)index = node[index].l;else{if(node[node[index].l].rand_fix < node[node[index].r].rand_fix){l_rot(index);remove(node[index].l, nkey);}else{r_rot(index);remove(node[index].r, nkey);}}return true;
}
int find(int index, int nkey)
{if(index == -1) return 0;if(node[index].key <= nkey){return node[index].countl+1 + find(node[index].r, nkey);}return find(node[index].l, nkey);
}void init()
{size = root = -1;srand(time(0));
}void print(int index)
{printf("%d ", node[index].key);if(node[index].l != -1) printf("left: %d ",node[node[index].l].key);if(node[index].r != -1) printf("right:%d ",node[node[index].r].key);printf("count: %d %d\n", node[index].countl, node[index].countr);if(node[index].l != -1) print(node[index].l); if(node[index].r != -1) print(node[index].r); }
int main()
{//freopen("1.txt", "r", stdin);init();scanf("%d", &n);for(int i = 0;i < n;i++){scanf("%d %d", &point[i].x, &point[i].y);point[i].p = i;}sort(point,point+n,_cmp);for(int i = 0;i < n;i++){insert(root, point[i].y);result[find(root,point[i].y)-1]++;}//print(root);for(int i = 0;i < n;i++) printf("%d\n", result[i]);
}
详细解决方案
poj 2352 treap
热度:7 发布时间:2023-12-21 00:03:56.0
相关解决方案
- POJ 2352 Stars .
- BZOJ3173[最长上升子序列] Treap+LIS
- CDQZ Challenge 8 Treap
- HDU-2352(verdis quo)
- treap 普通平衡树
- POJ-2352 线段树||树状数组 单点更新板子
- P3369 【模板】普通平衡树(Treap/SBT)
- 洛谷 P2286 [HNOI2004]宠物收养场 (treap 树)
- 非旋转treap模板(fhq treap)(洛谷3369,BZOJ3224)
- fhq——treap
- POJ 2352 Stars【树状数组】
- NEERC 17 G The Great Wall (treap)
- poj-2352-Stars-线段树
- POJ 2085 treap O(nlogn) 与 贪心 O(n)算法
- poj 2352 treap
- poj 1442 treap
- BZOJ2761 不重复的数字 【treap】
- 杭电ACM基础题(2352、2549、2564、2565、2567、2572、2609、2607、2707、2708、2719、2721、2723)
- Treap + FHQ Treap
- poj - 2352 - Stars(树状数组)
- 【算法竞赛学习笔记/数据结构】平衡树专题之FHQ Treap
- POJ 1785 treap 或 RMQ
- poj 2352 Stars(简单树状数组)此题也可以用线段树来做
- POJ 2352 树状数组
- POJ 2352 Stars(树状数组,查数)
- POJ 1442 Black Box(Treap,第k小)
- FHQ-Treap P3369 【模板】普通平衡树
- P1486 [NOI2004]郁闷的出纳员 fhq-Treap