POJ 2352 (TLE版本 只提供大致思路)

/*
题目大致意思:给定每一个点都有坐标x,y
从原点(0,0)开始到点所形成的矩阵内有多少个其他的点 点之和就为等级

大致思路:也就是说这个点的左边中点的总和 想到了左子树 就想到了用treap来维护数组
----
核心:用每个点的x 作为key
      每个点的y 作为val
----
>=x的放在右子树中 否则放在左子树中
左旋右旋的关键:
左旋:右儿子的y>=父节点的y 
主要针对为何加上=解释:
因为在右儿子x首先是大于父节点的x,因为这道题目的输入规律来看
所以后面输入相同y的点 所形成的矩阵一定包含父节点 因此要左旋 将父节点作为该右儿子的左儿子


猜想:采取了递归的写法 又由于本题数据庞大 所以递归算法超时。。
*/
#include <cstdio>
#include <cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
const int N = 100000;
const int INF = 1e8;
using namespace std;
struct node
{
	int x, y;
	int val;//key即为横坐标 val即为纵坐标
	int l, r;
	int cnt, size;
}tr[N];
int root, idx;
void pushup(int p)
{
	tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt;
}
int get_node(int x, int y)
{
	tr[++idx].x = x;
	tr[idx].y = y;
	//tr[idx].val = rand();
	tr[idx].cnt = tr[idx].size = 1;
	return idx;
}
void zig(int &p)
{
	int q = tr[p].l;
	tr[p].l = tr[q].r;
	tr[q].r = p;
	p = q;
	pushup(tr[p].r);
	pushup(p);
}
void zag(int &p)
{
	int q = tr[p].r;
	tr[p].r = tr[q].l;
	tr[q].l = p;
	p = q;
	pushup(tr[p].l);
	pushup(p);
}
void insert(int &p, int x, int y)
{
	if (!p)p = get_node(x, y);
	else if (tr[p].x > x)//小于的都放在左子树中
	{
		insert(tr[p].l, x,y);
		if (tr[tr[p].l].y > tr[p].y)zig(p);
	}
	else if (tr[p].x <= x)
	{
		insert(tr[p].r, x,y);
		if (tr[tr[p].r].y >= tr[p].y)zag(p);
	}
	pushup(p);
}
/*
3
0 1
0 2
0 3
*/
int main()
{
     int n; 
	 while (scanf("%d", &n))
	 {
		 int ans[N];
		 memset(ans, 0, sizeof ans);
		 root = 0; idx = 0;
		 memset(tr, 0, sizeof tr);
		 for (int i = 0; i < n; i++)
		 {
			 int x, y;
			 scanf("%d%d", &x, &y);
			 insert(root, x, y);
			 //cout << tr[tr[root].l].size << endl;
			 ans[tr[tr[root].l].size]++;//关键性问题插入的x不是root 所以还要有一个查找函数
		 }
		 for (int i = 0; i <= n - 1; i++)
		 {
			 printf("%d\n", ans[i]);
		 }
		 int a = 01;
	 }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值