[洛谷P4688][Ynoi2016]掉进兔子洞
题目大意:给定一个$n(n\leqslant10^5)$序列,$m(m\leqslant10^5)$个询问,每个询问给出$l_1,r_1,l_2,r_2,l_3,r_3$。令$s$为该三个区间的交集的大小,则输出$|[l_1,r_1]|+|[l_2,r_2]|+|[l_3,r_3]|−3|s|$
题解:$|[l_1,r_1]|+|[l_2,r_2]|+|[l_3,r_3]|$这一部分比较好求,主要就是求$|s|$,$s$是这三个区间元素的并集,可以想到用$bitset$,但是$bitset$似乎只可以求有多少种相同元素,而不可以求有多少个相同元素,这时可以改一下离散化的方式,排序后不要去重,这时就可以用这个数和这个数现在已经出现的次数定下一个唯一确定位置。这样就可以完成求并集的过程了。
这里可以用莫队来求每个数出现次数以及那一个元素出现的集合。但是发现空间复杂度是$O(\dfrac{nm}{\omega})$,开不下。可以把询问分成$3$次进行处理,就可以了
卡点:把一个$maxm$打成了$maxn$,然后$RE$
C++ Code:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <iostream>
namespace __IO {
namespace R {
int x, ch;
inline int read() {
while (isspace(ch = getchar()));
for (x = ch & 15; isdigit(ch = getchar()); ) x = x * 10 + (ch & 15);
return x;
}
}
}
using __IO::R::read; #define maxn 100010
#define maxm 35000 int n, m;
int s[maxn], v[maxn];
std::bitset<maxn> ans[maxm + 10], res;
struct Query {
int l, r, id;
inline friend bool operator < (const Query &lhs, const Query &rhs) {
return lhs.l >> 8 == rhs.l >> 8 ? (lhs.l >> 8 & 1 ? lhs.r > rhs.r : lhs.r < rhs.r) : lhs.l < rhs.l;
}
} q[maxm * 3 + 10]; int tmpans[maxm + 10], cnt[maxn]; inline void add(int x) {res.set(x + cnt[x]); cnt[x]++;}
inline void del(int x) {cnt[x]--; res.reset(x + cnt[x]);} void solve() {
int tot = 0;
for (int i = 1; m && i < maxm; i++, m--) {
ans[i].set(); tmpans[i] = 0;
q[++tot].id = i, q[tot].l = read(), q[tot].r = read(), tmpans[i] += q[tot].r - q[tot].l + 1;
q[++tot].id = i, q[tot].l = read(), q[tot].r = read(), tmpans[i] += q[tot].r - q[tot].l + 1;
q[++tot].id = i, q[tot].l = read(), q[tot].r = read(), tmpans[i] += q[tot].r - q[tot].l + 1;
}
res.reset();
for (int i = 1; i <= n; i++) cnt[i] = 0;
int l = 1, r = 0;
std::sort(q + 1, q + tot + 1);
for (int i = 1; i <= tot; i++) {
while (r < q[i].r) add(s[++r]);
while (l > q[i].l) add(s[--l]);
while (r > q[i].r) del(s[r--]);
while (l < q[i].l) del(s[l++]);
ans[q[i].id] &= res;
}
const int M = tot / 3;
for (int i = 1; i <= M; i++) printf("%d\n", tmpans[i] - ans[i].count() * 3);
} int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) v[i] = s[i] = read();
std::sort(v + 1, v + n + 1);
for (int i = 1; i <= n; i++) s[i] = std::lower_bound(v + 1, v + n + 1, s[i]) - v;
while (m) solve();
return 0;
}
[洛谷P4688][Ynoi2016]掉进兔子洞的更多相关文章
- 洛谷P4135 Ynoi2016 掉进兔子洞 (带权bitset?/bitset优化莫队 模板) 题解
题面. 看到这道题,我第一反应就是莫队. 我甚至也猜出了把所有询问的三个区间压到一起处理然后分别计算对应询问答案. 但是,这么复杂的贡献用什么东西存?难道要开一个数组 query_appear_tim ...
- luogu P4688 [Ynoi2016]掉进兔子洞 bitset 莫队
题目链接 luogu P4688 [Ynoi2016]掉进兔子洞 题解 莫队维护bitset区间交个数 代码 // luogu-judger-enable-o2 #include<cmath&g ...
- p4688 [Ynoi2016]掉进兔子洞
传送门 分析 我们考虑先将所有数离散化 之后我们对于每个状态用一个bitset来记录 其中第i段表示颜色i的信息 对于每一段信息均是段首若干1,剩余若干0表示这种颜色有多少个 于是我们不难想到莫队 答 ...
- luogu P4688 [Ynoi2016]掉进兔子洞
luogu 我们要求的答案应该是三个区间长度\(-3*\)在三个区间中都出现过的数个数 先考虑数列中没有相同的数怎么做,那就是对三个区间求交,然后交集大小就是要求的那个个数.现在有相同的数,考虑给区间 ...
- [Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset)
[Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset) 题面 一个长为 n 的序列 a.有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间 ...
- BZOJ.4939.[Ynoi2016]掉进兔子洞(莫队 bitset 分组询问)
BZOJ 洛谷 删掉的数即三个区间数的并,想到bitset:查多个区间的数,想到莫队. 考虑bitset的每一位如何对应每个数的不同出现次数.只要离散化后不去重,每次记录time就可以了. 但是如果对 ...
- Luogu4688 [Ynoi2016]掉进兔子洞 【莫队,bitset】
题目链接:洛谷 我们知道要求的是\([l_1,r_1],[l_2,r_2],[l_3,r_3]\)的可重集取交的大小,肯定是要用bitset的,那怎么做可重集呢? 那就是要稍微动点手脚,首先在离散化的 ...
- 【洛谷 P4688】 [Ynoi2016]掉进兔子洞(bitset,莫队)
题目链接 第一道Ynoi 显然每次询问的答案为三个区间的长度和减去公共数字个数*3. 如果是公共数字种数的话就能用莫队+bitset存每个区间的状态,然后3个区间按位与就行了. 但现在是个数,bits ...
- BZOJ4939: [Ynoi2016]掉进兔子洞(莫队 bitset)
题意 题目链接 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于 ...
随机推荐
- Python-内置函数3
'''1.lambda 声明一个匿名函数,并且自动给你返回值2.map()3.float()4.globals()5.locals()6.input()7.print()8.int()9.int()1 ...
- apache和IIS共存,服务器对外统一使用80端口
apache和IIS共用80端口为了PHP与ASP各自的执行效率,要在服务器上安装iis与Apache,但是无法同时使用80端口,否则其中必定有一个启动不了.让它们共存的并且访问网站不需要加端口号,解 ...
- 限时购校验小工具&dubbo异步调用实现限
本文来自网易云社区 作者:张伟 背景 限时购是网易考拉目前比较常用的促销形式,但是前期创建一个限时购活动时需要各个BU按照指定的Excel格式进行选品提报,为了保证提报数据准确,运营需要人肉校验很多信 ...
- coolshell里的一些c++文章
c++数组不支持多态 https://coolshell.cn/articles/9543.htmlwhy gcc in c++ https://airs.com/ian/cxx-slides.pdf ...
- beauifulsoup模块的介绍
01 爬虫基础知识介绍 相关库:1.requests,re 2.BeautifulSoup 3.hackhttp 使用requests发起get,post请求,获取状态码,内容: 使用re匹 ...
- (转)CGMA - Organic World Building in UE4: week 6
原文:丢失,这篇是艺术家博客上发现的,小道整理笔记中,临时放于效果案例目录. In this week we focused on creating the grass and flora t ...
- HTMLTestRunner带饼图
# -*- coding: utf-8 -*- """ A TestRunner for use with the Python unit testing framewo ...
- ionic 获取input的值
1.参数传递法 例子:获取input框内容 这里有个独特的地方,直接在input处使用 #定义参数的name值,注意在ts中参数的类型 在html页面中 <ion-input type=&quo ...
- 干货来袭:Redis5.0支持的新功能说明
Redis5.0支持的新特性说明 本文内容来自华为云帮助中心 华为云DCS的Redis5.x版本继承了4.x版本的所有功能增强以及新的命令,同时还兼容开源Redis5.x版本的新增特性. Stream ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...