题目描述

"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 ;
}
 

最新文章

  1. 【Learning Python】【第二章】Python基础类型和基础操作
  2. P1905生活大爆炸版 石头剪刀布
  3. 转: windows 10使用原生linux bash命令行
  4. Android Xutils 框架(转)
  5. 解决在iOS8环境下,当用户关闭定位服务总开关时,无法将APP定位子选项加入定位权限列表的问题
  6. paramiko-客户端和服务器认证工具
  7. 【video】m3u8
  8. c++中的名字查找
  9. springmvc的POST 请求转为 DELETE 或 put 请求配置HiddenHttpMethodFilter
  10. AD转换
  11. php中(包括织梦cms)set_time_limit(0)不起作用的解决方法
  12. Fragment与Fragment相互切换之间的生命周期方法
  13. Hive表生成函数explode讲解
  14. MFC界面分割以及挂载
  15. tomcat 内存溢出处理方案
  16. webpack基本配置
  17. 【Spring】SpringMVCの環境構築(簡)(Version3.1)
  18. 使用boost线程定时器作为后台线程来切换主循环程序状态方法2
  19. 各种学习Demo链接
  20. 文本框仅可接收decimal

热门文章

  1. VMD_EI_API=&gt;MAINTAIN_BAPI 去创建供应商主数据
  2. codeForce-589D Boulevard(判断线段是否相交)
  3. Apache服务器配置技巧
  4. [转]行者,一念一生,成功的背后!(给所有IT人)
  5. MySQL查询今天/昨天/本周、上周、本月、上个月份数据的sql代码
  6. 严格模式use strict
  7. ERROR 1130 (HY000):Host&#39;localhost&#39;解决方法
  8. python---dict字典
  9. ADF_Data Binding系列1_使用Bean Data Control
  10. error BK1506 : cannot open file &#39;.\Debug\????????.sbr&#39;: No such file or dire