Codeforces 669E cdq分治
题意:你需要维护一个multiset,支持以下操作:
1:在某个时间点向multiset插入一个数。
2:在某个时间点在multiset中删除一个数。
3:在某个时间点查询multiset的某个数的个数。
思路:该题相当于要构建一个在任意位置插入,并查询前缀操作的某个值的多少。乍一看比较棘手,搞不好还要数据结构的嵌套。但是转化一下思路,用cdq分治这题非常简单。分治过程中按时间排序即可,用map维护前半部分产生的影响,后面如果有查询在map中查询即可。
代码:
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 100010;
- struct query{
- int flag, t, val, id;
- };
- query q[maxn], tmp[maxn];
- int ans[maxn], tot;
- map<int, int> mp;
- void cdq(int l, int r) {
- if(l == r) return;
- int mid = (l + r) >> 1;
- cdq(l, mid);
- cdq(mid + 1, r);
- int l1 = l, l2 = mid + 1, pos = l;
- mp.clear();
- while(l1 <= mid && l2 <= r) {
- if(q[l1].t < q[l2].t) {
- if(q[l1].flag == 1) mp[q[l1].val]++;
- else if(q[l1].flag == 2) mp[q[l1].val]--;
- tmp[pos++] = q[l1++];
- } else {
- if(q[l2].flag == 3) {
- ans[q[l2].id] += mp[q[l2].val];
- }
- tmp[pos++] = q[l2++];
- }
- }
- while(l1 <= mid) tmp[pos++] = q[l1++];
- while(l2 <= r) {
- if(q[l2].flag == 3) {
- ans[q[l2].id] += mp[q[l2].val];
- }
- tmp[pos++] = q[l2++];
- }
- for (int i = l; i <= r; i++)
- q[i] = tmp[i];
- }
- int main() {
- int n;
- scanf("%d", &n);
- for (int i = 1; i <= n; i++) {
- scanf("%d%d%d", &q[i].flag, &q[i].t, &q[i].val);
- if(q[i].flag == 3) q[i].id = ++tot;
- }
- cdq(1, n);
- for (int i = 1; i <= tot; i++) {
- printf("%d\n", ans[i]);
- }
- }
Codeforces 669E cdq分治的更多相关文章
- 【题解】Radio stations Codeforces 762E CDQ分治
虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...
- Radio stations CodeForces - 762E (cdq分治)
大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...
- Tufurama CodeForces - 961E (cdq分治)
题面 One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series " ...
- AI robots CodeForces - 1045G (cdq分治)
大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...
- Codeforces 1093E Intersection of Permutations [CDQ分治]
洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...
- Codeforces 1045G AI robots [CDQ分治]
洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- Codeforces 848C (cdq分治)
Codeforces 848C Goodbye Souvenir Problem : 给一个长度为n的序列,有q个询问.一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点 ...
随机推荐
- maven clean后 编译报错
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <config ...
- javascript 设置cookie和取得cookie
代吗实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- HTML5 绘制阴影
代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...
- spring boot与ElasticSearch的集成
本文主要介绍Spring boot与ElasticSearch的集成,因为Spring boot的教程以及ElasticSearch的学习其他博客可能更优秀,所以建议再看这篇文章前先学习学习一下Spr ...
- BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2213 https://loj.ac/problem/2161 题解 做一道简单题来放松一下. ...
- 01.java8入门
函数式编程的不变模式 import java.util.Arrays; /** * 函数式编程的不变模式 */ public class ArrStream { public static void ...
- vue cli3以上的项目中如何使用axois请求本地json文件
首先明确一点,在vue cli3以上的版本中,存放静态资源的文件是public 我刚开始以为是和vue cli2一样需要放在static文件夹下,但是项目中没有这个文件夹,我就自己创建了一个,结果请求 ...
- MyEclipse使用过程中的问题及对应设置的总结
1.关闭 Javaweb项目中的updating index : Window => Preferences => Myeclipse Enterprise Workbench => ...
- git查看切换分支
Git一般有很多分支,我们clone到本地的时候一般都是master分支,那么如何切换到其他分支呢?主要命令如下: 1. 查看远程分支 $ git branch -a 我在mxnet根目录下运行以上命 ...
- Linux随笔 - linux 多个会话同时执行命令后history记录不全的解决方案【转载】
基本认识linux默认配置是当打开一个shell终端后,执行的所有命令均不会写入到~/.bash_history文件中,只有当前用户退出后才会写入,这期间发生的所有命令其它终端是感知不到的. 问题场景 ...