POJ2352 Stars (静态二叉检索树)
https://vjudge.net/problem/POJ-2352
分析:
由于是按照y坐标的升序,y坐标向等的按x的升序的顺序给出星星。那么某个星星的等级数就是在他前面x坐标小于等于他的x坐标的星星的个数。
暴力的时间复杂度为n^2,超时
所以我们要记录前面所有x坐标出现的次数。然后要求出[0,xi]出现的和。
这就是静态二叉检索树 的标准问题了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int maxx=32010;
const int maxn=15010;
int c[maxx];
int level[maxn]; int sum(int x)
{
int res=0;
while(x)
{
res+=c[x];
x-=(x&-x);
}
return res;
}
void add(int x,int d)
{
while(x<=maxx)
{
c[x]+=d;
x+=(x&(-x));
}
}
int main()
{
int x,y,n;
scanf("%d",&n);
memset(c,0,sizeof(c));
memset(level,0,sizeof(level));
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
level[sum(x+1)]++;
add(x+1,1);
}
for(int i=0;i<n;i++)
printf("%d\n",level[i]);
return 0;
}
POJ2352 Stars (静态二叉检索树)的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- HDU 3791 二叉搜索树 题解
Problem Description 推断两序列是否为同一二叉搜索树序列 Input 開始一个数n,(1<=n<=20) 表示有n个须要推断,n= 0 的时候输入结束. 接下去一行是 ...
- 九度oj 题目1009:二叉搜索树
题目1009:二叉搜索树 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5733 解决:2538 题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n&l ...
- 九度OJ 1009:二叉搜索树 (二叉树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6321 解决:2767 题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需 ...
- 剑指offer:二叉搜索树的第k个结点(中序遍历)
1. 题目描述 /* 给定一棵二叉搜索树,请找出其中的第k小的结点. 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. */ 2. 思路 中序遍历二叉搜索树,第K个就 ...
- AC日记——二叉堆练习3 codevs 3110
3110 二叉堆练习3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给定N(N≤500,000)和N个整 ...
- codevs 3110 二叉堆练习3
3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- 关于Servlet技术
1.Servlet 技术 a)什么是 Servlet 1.Servlet 是 JavaEE 规范之一.规范就是接口 ...
- 关于:org.apache.catalina.connector.ClientAbortException及getOutputStream() has already been called for this response的异常处理
1.异常场景: 在进行将数据用word导出或者Excel导出的时候.抛出的异常getOutputStream() has already been called for this response 导 ...
- Switch User Command in Linux
Switch user command (su) has the following forms: su Switch to , without loading environment variabl ...
- SpringMVC学习08(拦截器)
8.拦截器 概述 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.开发者可以自己定义一些拦截器来实现特定的功能. 过滤器与拦截器的区别: ...
- 21JavaScript笔记(1)
JavaScript 基于对象和事件驱动 简单描述性语言 函数优先 解释型(即时编译型) 具有安全性的脚本语言 1.js组成 核心语法(ECMAScript):开放的.标准的脚本语言规范,主要包含了语 ...
- Python语言系列-01-入门
python的出生与应用 #!/usr/bin/env python3 # author:Alnk(李成果) """ 1,python的出生与应用 python的创始人为 ...
- SpringBoot开发二十三-统一记录日志
统一记录日志 AlphaAspect package com.nowcoder.community.aspect; import org.aspectj.lang.ProceedingJoinPoin ...
- Linux部署达梦数据库(完全版)
环境准备 数据库下载去http://www.dameng.com 1.下载好达梦数据库iso上传(通过xftp)到你自己存在的目录(/opt) 2.将你上传的iso文件挂载到其它的目录下面(mount ...
- Dom4j(解析property)
Dom4j(解析property) public class XpathTest { /** * XPath 使用路径表达式来选取 XML 文档中的节点或节点集 * * 经常使用到的路径表达式,如下 ...
- Ubuntu16.04 + OpenCV源码 + Qt5.10 安装、配置
在VMWare中配置安装Ubuntu16.04.没有什么特别的地方,正常安装即可. 安装最新版qt,此时5.10.按照普通QT教程安装(需要勾选gcc),无须sudo,此时不用管OpenCV.地址:h ...