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[提交][状态][讨论版] 题目描述 “三角形十分的美丽,相信大家小学就学过三角形具有稳定性, ...
随机推荐
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- JSP 使用 JDBC连接SQL Server
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- CSS中的那点事儿(一)--- CSS中的单位1
单位主要用在规定元素的数值性css属性的,这里主要讨论应用的比较多的是width height padding margin font-size,而单位中应用最广泛就是%.px.em 百分比 百分比 ...
- stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
1.StringBuffer类的概述 * A:StringBuffer类概述 * 通过JDK提供的API,查看StringBuffer类的说明 * 1.线程安全的可变字符序列. * 2.可将字符串缓冲 ...
- CentOS设置代理
假设我们要设置代理为 IP:PORT1.网页上网网页上网设置代理很简单,在firefox浏览器下 Edit-->>Preferences-->>Advanced--> ...
- STL Algorithms 之 unique
C++的文档中说,STL中的unique是类似于这样实现的: template <class ForwardIterator> ForwardIterator unique ( Forwa ...
- UIButton常见属性和方法
一.创建,两种方法: 1. 常规的 initWithFrame UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 8 ...
- 理解Objective-C Runtime (五)协议与分类
Objective-C中的分类允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以做到. Objective-C中的协议是普遍存在的 ...
- GCD的使用(1)使用GCD保护property
作为一个iOS开发者,必须要熟练使用GCD,本文是站在实际应用的角度总结GCD的用法之一: 使用barrier保护property.在多线程环境下,如果有多个线程要执行同一份代码,那么有时会出现问题, ...
- 状态空间搜索好题UVA10603
题目 分析:注意这里求的是最少流量, 二不是最少步数!!!所以我们用优先队列去维护一个最小流量,然后进行bfs即可,解释一下一个重要的数组ans[i],表示的是杯子中的水为i时的最小流量 #inclu ...