题意:定义在某颗星星左下方的星星的个数表示该星星的水平,求出水平分别为为0...n-1的星星个数。

首先题目是按照y坐标升序输入的,设第第1,2...n个星星的横坐标依次为x1,x2,...xn.显然星星i的level等于(x1,x3,...xi-1)中比xi小的数的个数,将它记做Li,普通求法扫描一遍,

复杂度为O(n^2),显然数据了大会超时。采用树状数组来解决,复杂度为O(nlogn),用a[x]表示横坐标为x的点的个数,随着输入,不断更新维护树状数组。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
using namespace std;
const int MAXN = 32100;
/*tree为树状数组,ans数组统计星星水平*/
int tree[MAXN],ans[MAXN];
int n;
int readSum(int k){ //求和a[1]+a[2]...+a[k]
int sum = 0;
while (k > 0){
sum += tree[k];
k -= (k&-k);
}
return sum;
}
void add(int pos, int diff){ //增量修改
while (pos <MAXN){
tree[pos] += diff;
pos += (pos&-pos);
}
}
int main(){
int i, x, y;
while (scanf("%d", &n) != EOF){
memset(ans, 0, sizeof(ans));
memset(tree, 0, sizeof(tree));
for (i = 0; i < n; i++){
scanf("%d%d", &x, &y);
x++; //避免0带来的不便
int level = readSum(x); //计算星星的水平
ans[level]++;
add(x, 1); //维护树状数组
}
for (i = 0; i < n; i++){
printf("%d\n", ans[i]);
}
}
return 0;
}

  

hdu 1541Stars的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. 【Linked List Cycle】cpp

    题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...

  2. leetcode 【 Maximum Subarray 】python 实现

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  3. IOS开发学习笔记042-UITableView总结2

    一.自定义非等高的cell         如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...

  4. ogre3D学习基础15 -- 创建BSP Scene Manager

    BSP(binary-space partitioning) Scene Manager(二叉空间分割)场景管理器比较适合用于室内场景. 第一,添加框架代码如下 #include "Exam ...

  5. uReplicator实现分析

    MirrorMakerWorker分析 是整个同步机制的主入口,主要组织的逻辑有: 配置数据的传入与处理,ConsumerConfig对象的构建 度量对象的准备,定时上报的度量数据收集线程的定义与启动 ...

  6. WordCount by Java

    WordCount by Java 软测第二周作业 该项目github地址如下: https://github.com/YuQiao0303/WordCount 一.概述 项目WordCount的需求 ...

  7. vlc无法播放.flv视频文件

    解决方法:https://videoconverter.wondershare.com/vlc/flv-not-displaying-video-vlc-media-player.html. 在pre ...

  8. leetcode NO.349 两个数组的交集 (python实现)

    来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...

  9. hadoop配置文件: hdfs-site.xml, mapred-site.xml

    dfs.name.dir Determines where on the local filesystem the DFS name node should store the name table( ...

  10. 【两种方式 Service References和 web References 】手把手教你引入webservice 服务

    1.对于一个webservie服务我们如何引入到自己的项目中去呢 第一种方法[Service References]:鼠标移到属性上 右键添加服务引用 然后在地址栏输入webservice 地址 点击 ...