[CF369E]Valera and Queries_离线_树状数组
Valera and Queries
题目链接:codeforces.com/problemset/problem/369/E
数据范围:略。
题解:
这种题,就单独考虑一次询问即可。
我们发现,包括了至少一个给定点的个数,等于总个数减掉一个给定点都不包括的线段数。
一个都不包括,就表示这个线段的在两个给定点中间,这个可以把线段抽象成二维平面上的点,然后离线+树状数组查询。
代码:
#include <bits/stdc++.h> #define N 1000010 using namespace std; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() {
int x = 0;
char c = nc();
while (c < 48) {
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x;
} int cnt = 0; struct Node {
int x, y1, y2, id, c, opt;
}a[N * 10]; inline bool cmp(const Node &a, const Node &b) {
return a.x == b.x ? a.opt > b.opt : a.x < b.x;
} int q[N]; int tree[N]; inline int lowbit(int x) {
return x & (-x);
} void update(int x) {
for (int i = x; i < N; i += lowbit(i)) {
tree[i] ++ ;
}
} int query(int x) {
int ans = 0;
for (int i = x; i; i -= lowbit(i)) {
ans += tree[i];
}
return ans;
} int ans[N]; int main() {
int n = rd(), m = rd();
// opt : 1 -> query, 2 -> update
for (int i = 1; i <= n; i ++ ) {
int x = rd(), y = rd();
a[ ++ cnt] = (Node) {x, y, 0, 0, 1, 2};
}
// cout << cnt << endl ;
for (int i = 1; i <= m; i ++ ) {
int num = rd();
for (int j = 1; j <= num; j ++ ) {
q[j] = rd();
}
q[0] = 0;
q[ ++ num] = N - 1;
// cout << num << endl ;
for (int j = 1; j <= num; j ++ ) {
if (q[j] - q[j - 1] >= 2) {
// printf("Fuck %d\n", j);
int x = q[j - 1] + 1, y = q[j] - 1;
a[ ++ cnt] = (Node) {x - 1, x, y, i, -1, 1};
a[ ++ cnt] = (Node) {y, x, y, i, 1, 1};
}
}
}
// cout << cnt << endl ;
sort(a + 1, a + cnt + 1, cmp);
// for (int i = 1; i <= cnt; i ++ ) {
// printf("%d %d %d %d %d %d\n", a[i].x, a[i].y1, a[i].y2, a[i].id, a[i].c, a[i].opt);
// }
for (int i = 1; i <= cnt; i ++ ) {
// printf("id :: %d\n", i);
if (a[i].opt == 2) {
update(a[i].y1);
}
else {
// cout << query(a[i].y2) << ' ' << query(a[i].y1) << ' ' << a[i].c << endl ;
// cout << (query(a[i].y2) - query(a[i].y1)) * a[i].c << endl ;
ans[a[i].id] += (query(a[i].y2) - query(a[i].y1)) * a[i].c;
}
} for (int i = 1; i <= m; i ++ ) {
printf("%d\n", n - ans[i]);
}
return 0;
}
[CF369E]Valera and Queries_离线_树状数组的更多相关文章
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- BZOJ_3653_谈笑风生_树状数组
BZOJ_3653_谈笑风生_树状数组 Description 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ...
- BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树
BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...
- BZOJ_2141_排队_树状数组+分块
BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...
- BZOJ_3132_上帝造题的七分钟_树状数组
BZOJ_3132_上帝造题的七分钟_树状数组 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b), ...
- 【loj6041】「雅礼集训 2017 Day7」事情的相似度 后缀自动机+STL-set+启发式合并+离线+扫描线+树状数组
题目描述 给你一个长度为 $n$ 的01串,$m$ 次询问,每次询问给出 $l$ .$r$ ,求从 $[l,r]$ 中选出两个不同的前缀的最长公共后缀长度的最大值. $n,m\le 10^5$ 题解 ...
- 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询
题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...
- BZOJ1878: [SDOI2009]HH的项链 (离线查询+树状数组)
1878: [SDOI2009]HH的项链 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 Description: HH有一串由 ...
- HDU 4746 莫比乌斯反演+离线查询+树状数组
题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...
随机推荐
- P2891 [USACO07OPEN]吃饭Dining
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...
- linux系列(十四):head命令
1.命令格式: head [参数] [文件] 2.命令功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行. 3.命令参数: -q 隐藏文件名 -v 显示文件名 ...
- mybatis-generator-gui如何打包成exe
快速阅读: 用wix和inno setup把mybatis-generator-gui 打包成exe和安装文件. 以后使用的时候方便,不用每次打开eclipse运行. 使用inno setup 5 ...
- 如何在虚拟机中安装kali linux
整理笔记,把以前印象笔记中记录的一些东西翻出来,想想发个随笔吧. 第一步在官网下载kali linux的镜像. 网址:https://www.kali.org/downloads/ (我的电脑是64位 ...
- clickhouse redash---olap
curl -XPUT '10.1.193.250:9200/ngx-logstash-2019-06/_settings' -d ' { "index.mapping.total_field ...
- Go 与 JSON
Go 中通过标准库encoding/json.encoding.xml.encoding/asn1和其他库对 JSON.XML.ASN.1 和其他类型的标准的编码和解码提供了良好的支持,这里对使用最多 ...
- How to transform the day time images to night time ? A series of paper review and some thinkings about this point.
How to transform the day time images to night time ? A series of paper review and some thinkings ab ...
- 如何准备算法工程师面试,斩获一线互联网公司机器学习岗offer?
原文:https://zhuanlan.zhihu.com/p/76827460?utm_source=wechat_session&utm_medium=social&utm_oi= ...
- API 设计 POSIX File API
小结: 1. https://mp.weixin.qq.com/s/qWrSyzJ54YEw8sLCxAEKlA API 设计最佳实践的思考 谷朴 阿里技术 昨天 阿里妹导读:API 是模块或者子 ...
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...