题目描述

"But baby, I've been, I've been praying hard,
    Said, no more counting dollars
    We'll be counting stars"

Grandpa Shaw loves counting stars.
One evening he was sitting in his armchar, trying to figure out this problem.

Consider the sky as a rectangular coordinates, each star has a coordinate (Xi,Yi).
To make it simple, all the stars are in the first quadrant.
Now he want to know how many stars there are in the square formed by (0,0) and (x,y).
(including stars in edges, 4 vertices of the square is (0,0) (x,0) (x,y) (0,y))

There are n stars in the sky, and he raised m questions.
Because grandpa Shaw's eyesight is poor, he ask you for help.


in this image, yellow stars are in the square, blue ones are not.

输入

multiple test cases, please read until EOF.
for each test case:
first line contains two integers n m (0 <= n, m <= 10^5).
following n lines each line contains two integers Xi Yi (0 <= Xi, Yi <= 10^6).
following m lines each line contains two integers x y (0 <= x, y <= 10^6).

输出

for each test case:
first line "Case #t:" t is the number of test case.
following m lines, each line contains one integer, the number of stars in that square.

--正文
由于是离线的询问,并不需要二维
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int n,m;
int c[],ans[]; struct StarNode {
int x,y;
int num;
bool query;
};
struct StarNode star[]; bool cmp(struct StarNode s1,struct StarNode s2){
if (s1.x == s2.x) return s1.y < s2.y;
return s1.x < s2.x;
}
int lowbit(int i) {
return i&-i;
}
void Update(int i,int x){
while (i < ){
c[i] += x;
i += lowbit(i);
}
}
int Sum(int i){
int s = ;
while (i > ) {
s += c[i];
i -= lowbit(i);
}
return s;
} int main(){
int total = ;
while (scanf("%d %d",&n,&m) != EOF){
memset(c,,sizeof(c)); total ++;
int i,j;
for (i=;i<=n;i++){
int xi,yi;
scanf("%d %d",&star[i].x,&star[i].y);
star[i].query = false;
}
for (i=n+;i<=n+m;i++){
scanf("%d %d",&star[i].x,&star[i].y);
star[i].query = true; star[i].num = i - n;
}
sort(star+,star++n+m,cmp);
printf("Case #%d:\n",total);
for (i=;i<=n+m;i++){
if (star[i].query)
ans[star[i].num] = Sum(star[i].y+);
else
Update(star[i].y+,);
}
for (i=;i<=m;i++)
printf("%d\n",ans[i]);
}
return ;
}
 

XidianOJ 1177 Counting Stars的更多相关文章

  1. Counting Stars

    Counting Stars 题目链接:http://acm.xidian.edu.cn/problem.php?id=1177 离线+一维树状数组 一眼扫过去:平面区间求和,1e6的数据范围,这要h ...

  2. HDU 6184 Counting Stars

    Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!S ...

  3. 【刷题】HDU 6184 Counting Stars

    Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful! ...

  4. [hdu 6184 Counting Stars(三元环计数)

    hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...

  5. HDU 6184 Counting Stars 经典三元环计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6184 题意: n个点m条边的无向图,问有多少个A-structure 其中A-structure满足V ...

  6. HDU6184【Counting Stars】(三元环计数)

    题面 传送门 给出一张无向图,求 \(4\) 个点构成两个有公共边的三元环的方案数. 题解 orz余奶奶,orz zzk 首先,如果我们知道经过每条边的三元环个数\(cnt_i\),那么答案就是\(\ ...

  7. hdu6184 Counting Stars 【三元环计数】

    题目链接 hdu6184 题解 题意是让我们找出所有的这样的图形: 我们只需要求出每条边分别在多少个三元环中,记为\(x\),再然后以该点为中心的图形数就是\({x \choose 2}\) 所以我们 ...

  8. 全国高校网安联赛Web专场~WriteUp

    1.Sign 题目:Good Luck!flag{X-nuca@GoodLuck!} Flag直接写在题目上了,flag{X-nuca@GoodLuck!} 2.BaseCoding 提示:这是编码不 ...

  9. Python 新浪微博中提取最常见转载的微博转载了几次,username,内容

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-4 @author: guaguastd @name: fi ...

随机推荐

  1. 跨服务器之间的session共享

    跨服务器之间的Session共享方案需求变得迫切起来,最终催生了多种解决方案,下面列举4种较为可行的方案进行对比探讨: 1. 基于NFS的Session共享 NFS是Net FileSystem的简称 ...

  2. CentOS 基本设置

    CentOS 基本设置 1.更改163源 在使用yum的时候,可能yum被锁,可用如下命令解锁:rm -rf /var/run/yum.id 2.编译安装开源软件 安装自己编译的开源软件一般都会在/u ...

  3. oracle 跨数据库取数据

    思路:先从另一个数据库里把数据取出来, 然后,把这个数据集合解析,根据这个数据集合拆分组合成一个创建oralce临时表的方法及数据的插入.紧接着就可以写sql语句进行联合查询了. 下面是具体实例的方法 ...

  4. jq 文字上下不间断滚动实例

    <div class="ruzhuright">                     <div class="rzcontent"> ...

  5. oracle中的装换函数

    日期装换成字符的函数:TO_CHAR(date[,fmt[,params]]) 默认格式:DD-MON-RR 参数说明: date:将要装换的日期 fmt:装换的格式 params:日期的语言(可以不 ...

  6. Activity之间数据交流(onActivityResult的用法)

    http://www.blogjava.net/mixer-a/archive/2012/03/23/375014.html http://www.cnblogs.com/linjiqin/archi ...

  7. javascript中的事件委托

    这几天看到一个面试题,大概就是,让你给1000个li都添加一个click事件,应该怎么添加?大多数人第一开始的感觉可能就是,每个li上边都添加一个呗,那要是这样的话,估计面试的时候就会GG了,这里就是 ...

  8. NodeJs http.get 方法请求时间过长问题处理!

    NodeJs 获取网页内容 http.get 默认请求超时过长了,程序要卡死好一会. 根据网上资料现改造了一个带超时设置的获取网页内容方法. 主要方法使用 setTimeout 来超时, 代码如下: ...

  9. HackerRank "Kruskal (MST): Really Special Subtree"

    Kruskal Algorithm is based on Union-Find - quite intuitive. #include <vector> #include <ios ...

  10. python中使用xlrd、xlwt操作excel表格详解

    python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...