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_离线_树状数组的更多相关文章

  1. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  2. BZOJ_3653_谈笑风生_树状数组

    BZOJ_3653_谈笑风生_树状数组 Description 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ...

  3. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

  4. BZOJ_2141_排队_树状数组+分块

    BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...

  5. BZOJ_3132_上帝造题的七分钟_树状数组

    BZOJ_3132_上帝造题的七分钟_树状数组 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b), ...

  6. 【loj6041】「雅礼集训 2017 Day7」事情的相似度 后缀自动机+STL-set+启发式合并+离线+扫描线+树状数组

    题目描述 给你一个长度为 $n$ 的01串,$m$ 次询问,每次询问给出 $l$ .$r$ ,求从 $[l,r]$ 中选出两个不同的前缀的最长公共后缀长度的最大值. $n,m\le 10^5$ 题解 ...

  7. 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询

    题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...

  8. BZOJ1878: [SDOI2009]HH的项链 (离线查询+树状数组)

    1878: [SDOI2009]HH的项链 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 Description: HH有一串由 ...

  9. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

随机推荐

  1. leetcode解题报告(18):Contains Duplicate

    描述 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  2. 国外最受欢迎的BT-磁力网站

    1.海盗湾 The Pirate Bay 2.KickAssTorrents 3.Torrentz 4.zooqle 5.SumoTorrent 6.TorrentDownloads 7.Rarbg ...

  3. Hadoop YARN 调度器(scheduler) —— 资源调度策略

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/hadoop_yarn_resource_scheduler 搜了 ...

  4. maven引入第三方jar包

    maven有两种文件解析和分配策略,也就是我们常说的artifacts(依赖). 第一种是本地仓库,这是你缓存在本地的依赖.默认在${user.home}/.m2/repository目录下;当mav ...

  5. 贝济埃曲线quadTo与传统的手势轨迹平滑度对比分析

    package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas ...

  6. springboot 之JPA

    1.添加pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. 丢失libiconv-2.dll解决办法以及无法定位输入点libiconv-2.dll到动态链接库

    摘自https://blog.csdn.net/mengxiangjia_linxi/article/details/78147348 丢失libiconv-2.dll解决办法以及无法定位输入点lib ...

  8. 123457123456---com.threeObj3.BabyShizi02--- 宝宝识字02

    com.threeObj3.BabyShizi02--- 宝宝识字02

  9. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  10. LeetCode_171. Excel Sheet Column Number

    171. Excel Sheet Column Number Easy Given a column title as appear in an Excel sheet, return its cor ...