scau 1144 数星星 bit + 扫描线的思想
这题如果用二维树状数组,则会直接爆内存。
那么可以运用扫描线的思路。
就是,它同时被x和y限制了,那么可以在查询的时候,确保x先满足了,(把x按小到大排序)
然后就相当于是关于y的一个一维bit了,
注意同一个点它询问两次。
8
2 2
1 1
1 2
1 3
1 4
1 5
2 1
0 1
2
1 1
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + ;
struct node {
int x, y, id;
bool operator < (const struct node & rhs) const {
if (x != rhs.x) return x < rhs.x;
else return y < rhs.y;
}
}a[maxn];
int c[maxn];
int lowbit(int x) {
return x & (-x);
}
void upDate(int pos, int val) {
while (pos <= maxn - ) {
c[pos] += val;
pos += lowbit(pos);
}
}
int sum(int pos) {
int ans = ;
while (pos > ) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
int ser[maxn];
int ans[maxn];
int n;
vector<int>want;
void work() {
memset(c, , sizeof c);
// scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d%d", &a[i].x, &a[i].y);
a[i].x++;
a[i].y++;
a[i].id = i;
}
sort(a + , a + + n);
// for (int i = 1; i <= n; ++i) {
// printf("%d %d %d\n", a[i].x, a[i].y, a[i].id);
// }
int m;
scanf("%d", &m);
memset(ser, , sizeof ser);
for (int i = ; i <= m; ++i) {
int id;
scanf("%d", &id);
ser[id] = i;
want.push_back(id);
}
for (int i = ; i <= n; ++i) {
if (ser[a[i].id]) {
ans[a[i].id] = sum(a[i].y);
}
upDate(a[i].y, );
}
for (int i = ; i < want.size(); ++i) {
printf("%d\n", ans[want[i]]);
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d", &n) != EOF) work();
return ;
}
scau 1144 数星星 bit + 扫描线的思想的更多相关文章
- SCAU-1144 数星星-HDU-1166-树状数组的应用
本文借鉴代码提供:https://www.cnblogs.com/geek1116/p/5566709.html树状数组详解:https://www.cnblogs.com/xenny/p/97396 ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
- codev 2147 数星星
2147 数星星 http://codevs.cn/problem/2147/ 题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘宝上买下来了一个高级望远镜.他十分 ...
- 题解西电OJ (Problem 1008 - 数星星)
题目内容: Description “不要问我太阳有多高 我会告诉你我有多真 不要问我星星有几颗 我会告诉你很多很多” 一天Qinz和wudired在天上数星星,由于星星可以排列成一条直线,他们比赛看 ...
- 一本通1536数星星 Stars
1536:[例 2]数星星 Stars 时间限制: 256 ms 内存限制: 65536 KB [题目描述] 原题来自:Ural 1028 天空中有一些星星,这些星星都在不同的位置,每 ...
- codevs 2147 数星星
2147 数星星 http://codevs.cn/problem/2147/ 题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘宝上买下来了一个高级望远镜.他十分 ...
- codevs——2147 数星星
2147 数星星 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星 ...
- codevs2147数星星(哈希)
2147 数星星 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘 ...
- 大红数星星 图论 XD网络赛
问题 A: 大红数星星 时间限制: 3 Sec 内存限制: 128 MB提交: 1066 解决: 67[提交][状态][讨论版] 题目描述 “三角形十分的美丽,相信大家小学就学过三角形具有稳定性, ...
随机推荐
- hadoop报JAVA_HOME is not set暂时解决办法
直接在etc/hadoop/hadoop-env.sh中 export JAVA_HOME=XXX
- Ruby中任务构建工具rake的入门
不同的rake文件当中不要定义重名的方法,不然没法调用 参考:http://www.jb51.net/article/81476.htm Rake简介 Rake的意思是Ruby Make,一个用rub ...
- POJ 2017 Speed Limit (直叙式的简单模拟 编程题目 动态属性很少,难度小)
Sp ...
- maven实战(3)-- dependency <classifier>的使用
Maven 的classifier的作用 转自:http://blog.csdn.net/lovingprince/article/details/5894459 直接看一个例子,maven中要引入j ...
- BZOJ1040:骑士(基环树DP)
Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...
- [Selenium] 如何在老版本的Chrome 浏览器上使用selenium
由于Chrome Driver 只兼容Chrome 浏览器12.0.712.0 和之后的新版本,会因此如果要在老版本的Chrome 浏览器上使用Selenium, 则只能使用 SeleniumRC ...
- Android适合组件化开发的路由框架:Launch
1.概述 最近越来越不想写代码了,特别是一些重复性的代码,比如由于每次启动一个 Activity,我们都会很习惯的在 Activity 中写下: public static void launch(A ...
- CodeForces - 504A && CodeForces - 624C && CodeForces - 2B
Points 1. 关键要看到以度数为1的点作为突破口. 2. 关键是发现两者不同只能是a-c,而剩余的点必须为b 3. 注意0的情况.
- KDotAlert
一个iPhone X的适配让楼主受尽了自定义的苦,使用系统API多好,所以在楼主不懈的努力下,终于和组长达成一致:逐步用系统控件替换代码里面的自定义控件,第一个挨刀的就是 BlockAlertsAnd ...
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时26&&27
课时26 图像分割与注意力模型(上) 语义分割:我们有输入图像和固定的几个图像分类,任务是我们想要输入一个图像,然后我们要标记每个像素所属的标签为固定数据类中的一个 使用卷积神经,网络为每个小区块进行 ...