树状数组 || POJ 2352 Stars
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉
题意:给出一堆星星的坐标,求每个星星左侧和下侧空间内的所有星星数量
思路:因为发现输入里y是递增输入的,所以输入当前这个星星的时候,它前面的所有星星肯定都在它下面(或者和它一行)
就转化为求之前输入的那些星星有多少个x坐标比它小的,用树状数组来维护
树状数组。。还不怎么会用哇。。。
数组大小开的。。emm是一个迷
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
#define SZ 320200
int a[SZ], d[SZ], t[SZ], n;
void add(int i, int x)
{
for(; i <= ; i += (i & -i))
d[i] += x;
}
int get(int i)
{
int ans = ;
for(; i; i -= (i & -i))
ans += d[i];
return ans;
} int main()
{
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
x++;//x可能等于0,把它们都+1
int ans = get(x);//求x之前的所有的和(前缀和,就是当前点的等级
t[ans]++;
add(x, );
}
for(int i = ; i < n; i++) printf("%d\n", t[i]);
return ;
}
树状数组 || POJ 2352 Stars的更多相关文章
- [树状数组]数星星 Stars
数 星 星 S t a r s 数星星 Stars 数星星Stars 题目描述 天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标.如果一个星星的左下方(包含正左和正下)有 k k k 颗星星 ...
- 树状数组入门 hdu1541 Stars
树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次 ...
- 线段树/树状数组 POJ 2182 Lost Cows
题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...
- 树状数组 || POJ 3321 Apple Tree
一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...
- LCA+树状数组 POJ 2763 Housewife Wind
题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POJ 2352 Stars(树状数组)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30496 Accepted: 13316 Descripti ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- 【树状数组】POJ 2352 Stars
/** * @author johnsondu * @time 2015-8-22 * @type Binary Index Tree * ignore the coordinate of y and ...
随机推荐
- C++开发工程师面试题库 100~150道
101. 编写strcat函数(6分) 已知strcat函数的原型是char *strcat (char *strDest, const char *strSrc); 其中strDest 是目的字符串 ...
- pojcoin【未完待续】
题意: 给你一些数字的种类,然后拥有这个种类的各个数量,输出可以组成多少数字,数字范围在1-m: 思路: 卧槽好难-
- PJzhang:漏洞渗透测试框架“天使之剑(AngelSword)”
猫宁!!! 参考链接: www.phpinfo.cc/?post=42 https://www.freebuf.com/sectool/149883.html 同事介绍了一款渗透测试框架AngelSw ...
- 跟我一起玩Win32开发(9):绘图(B)
我们今天继续涂鸦,实践证明,涂鸦是人生一大乐趣. 首先,我们写一个程序骨架子,以便做实验. #include <Windows.h> LRESULT CALLBACK MainWinPro ...
- BZOJ1026(数位dp)
数位dp第二道-就当成搜索,套板子写了写.我写的dp[pos][pre][state0]记录的是当前pos位没有限制时.前面的数是pre时.前面是否都是0时的方案数. #include <cst ...
- Random Query CodeForces - 846F
题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...
- spring cloud微服务项目的发布与部署
普通的javaweb项目要发布的话,一般就三种方法: 1.把项目直接放在tomcat的webApps下启动tomcat即可. 2.把项目打包成war包放在webApps下,启动tomcat,自动解压w ...
- [已读]HTML5与CSS3设计模式
我想说,不要被书名骗了,其实并没有涉及丁点h5与css3的内容,但是确实称得上比较详细的一本关于css的书.看它的页数就知道了,481~~ 今年上半年看完的,现在想想,觉得自己还是蛮拼的.内容会比较枯 ...
- JDBC——入门知识【转】
1. 什么是JDBC:Java数据库连接性(JavaDatabase Connectivity) API,允许用户从Java应用程序中访问任何表格化数据源. 2. JDBC除了提供到更宽范围的SQ ...
- Suricata的规则解读(默认和自定义)
不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...