题意

\(n\)个点,维护两个森林,这里\(A,B\)两个森林对应的点都是一样的,相当于对两个森林操作都会影响这\(n\)个点

开始森林里的树都是一个点,\(A,B\)支持合并(但树结构互不影响),\(A\)支持树赋\(0\),\(B\)支持树加值

做法

\(A,B\)分开做,每次合并新建一个虚点,这样相当于把\(n\)个点拍扁了,就比较好维护点集的操作

求出对每次查询最近的一次赋\(0\)操作,然后再在这个区间里算\(B\)修改对该点的贡献

将操作离线下来放在树上就可以做了

有个小\(trick\)就是用树状数组倍增一下求最近的那次赋\(0\)操作

Code(jly)

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 500000;
struct Tree {
int n;
vector<int> parent, lc, rc, id;
vector<vector<int>> mod;
Tree(int n) {
parent.assign(2 * n - 1, -1);
lc.assign(2 * n - 1, -1);
rc.assign(2 * n - 1, -1);
id.resize(n);
iota(id.begin(), id.end(), 0);
mod.resize(2 * n - 1);
}
};
long long fen[N + 1];
void add(int x, int y) {
for (int i = x + 1; i <= N; i += i & -i)
fen[i] += y;
}
long long sum(int x) {
long long res = 0;
for (int i = x; i > 0; i -= i & -i)
res += fen[i];
return res;
}
long long sum(int l, int r) {return sum(r) - sum(l);}
int getLeft(int x) {
int s = sum(x);
x = 0;
for (int i = 1 << 18; i > 0; i /= 2) {
if (x + i <= N && fen[x + i] < s) {
x += i;
s -= fen[x];
}
}
return x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
Tree t1(n), t2(n);
vector<vector<int>> query(n);
vector<int> lb(m), sz(2 * n - 1, 1);
vector<long long> ans(m, -1);
int c1 = 0, c2 = 0;
for (int i = 0; i < m; ++i) {
char op;
cin >> op;
if (op == 'U') {
int a, b;
cin >> a >> b;
--a;
--b;
int p = n + (c1++);
t1.parent[t1.id[a]] = t1.parent[t1.id[b]] = p;
t1.lc[p] = t1.id[a];
t1.rc[p] = t1.id[b];
t1.id[a] = p;
sz[p] = sz[t1.lc[p]] + sz[t1.rc[p]];
} else if (op == 'M') {
int a, b;
cin >> a >> b;
--a;
--b;
int p = n + (c2++);
t2.parent[t2.id[a]] = t2.parent[t2.id[b]] = p;
t2.lc[p] = t2.id[a];
t2.rc[p] = t2.id[b];
t2.id[a] = p;
} else if (op == 'A') {
int x;
cin >> x;
--x;
t1.mod[t1.id[x]].push_back(i);
} else if (op == 'Z') {
int x;
cin >> x;
--x;
t2.mod[t2.id[x]].push_back(i);
} else {
int x;
cin >> x;
--x;
query[x].push_back(i);
}
}
function<void(int)> dfs1 = [&](int u) {
for (int i : t2.mod[u])
add(i, 1);
if (u < n) {
for (int i : query[u])
lb[i] = getLeft(i);
} else {
dfs1(t2.lc[u]);
dfs1(t2.rc[u]);
}
for (int i : t2.mod[u])
add(i, -1);
};
function<void(int)> dfs2 = [&](int u) {
for (int i : t1.mod[u])
add(i, sz[u]);
if (u < n) {
for (int i : query[u])
ans[i] = sum(lb[i], i);
} else {
dfs2(t1.lc[u]);
dfs2(t1.rc[u]);
}
for (int i : t1.mod[u])
add(i, -sz[u]);
};
for (int i = 0; i < n + c2; ++i)
if (t2.parent[i] == -1)
dfs1(i);
for (int i = 0; i < n + c1; ++i)
if (t1.parent[i] == -1)
dfs2(i);
for (int i = 0; i < m; ++i)
if (ans[i] != -1)
cout << ans[i] << "\n";
return 0;
}

CF571D Campus(19-1)的更多相关文章

  1. 【刷题-PAT】A1095 Cars on Campus (30 分)

    1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...

  2. Linux运维必会的实战编程笔试题(19题)

    以下Linux运维笔试面试编程题,汇总整理自老男孩.马哥等培训机构,由运维派根据实战需求,略有调整: 企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理 ...

  3. 《TCP/IP具体解释》读书笔记(19章)-TCP的交互数据流

    在TCP进行传输数据时.能够分为成块数据流和交互数据流两种.假设按字节计算.成块数据与交互数据的比例约为90%和10%,TCP须要同一时候处理这两类数据,且处理的算法不同. 书籍本章中以Rlogin应 ...

  4. 复旦高等代数I(19级)每周一题

    本学期的高等代数每周一题活动计划从第2教学周开始,到第15教学周结束,每周的周末公布一道思考题(共14道,思考题一般与下周授课内容密切相关),供大家思考和解答.每周一题将通过“高等代数官方博客”(以博 ...

  5. GO语言编译环境采用gosublime(19新版)

    环境:博主已下载好,并安装在E:/SDK,使用过sublime text3,有git 0x00 环境变量 默认已经有GOPATH(代码存放在这),PATH(安装go的bin目录即E:\SDK\bin) ...

  6. PAT A1095 Cars on Campus (30 分)——排序,时序,从头遍历会超时

    Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...

  7. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  8. CTSC2016&&APIO2016滚粗记&&酱油记&&游记<del>(持续更新)</del>

    挖一波坑 #include <cstdio> using namespace std; int main(){ puts("转载请注明出处:http://www.cnblogs. ...

  9. Win95+IE3 – Win10+IE11全版本执行漏洞(含POC)

    微软本月安全更新修复了一个潜藏了18年的IE远程代码执行漏洞(CVE-2014-6332),可以说是给windows吃了一颗大补丸.缺陷出现在VBScript的代码中,自Windows 95首次发布( ...

随机推荐

  1. one-hot编码(pytorch实现)

    n = 5 #类别数 indices = torch.randint(0, n, size=(15,15)) #生成数组元素0~5的二维数组(15*15) one_hot = torch.nn.fun ...

  2. java架构之路-(微服务专题)feign的基本使用和nacos的配置中心

    上次回归: 上次我们说了ribbon的基本使用,包括里面的内部算法,算法的细粒度配置,还有我们自己如何实现我们自己的算法,主要还是一些基本使用的知识,还不会使用ribbon的小伙伴可以回去看一下上一篇 ...

  3. 阿里云服务器ECS Ubuntu18.04 安装mysql

    ubuntu系统好了,这下我应该安装MySQL数据库了.在安装过程中,遇到好多坑,下面是我的安装过程. 1.在阿里云控制台,用vnc登录到服务器. 用新的用户登录到Ubuntu用户系统. 打开终端: ...

  4. 开源堡垒机jumpserver的配置和使用

    开源跳板机jumpserver配置和使用 http://docs.jumpserver.org/zh/docs/quick_start.html#id9 系统设置 基本设置 # 修改url 的&quo ...

  5. 小记centos7.5下yum安装cobbler遇到的问题

    问题1:执行cobbler sync同步命令报错,提示dhcpd服务错误和Python源码错误 [root@server ~]# cobbler sync #<===执行cobbler同步的时候 ...

  6. 我国自主研发的先进辅助驾驶系统(ADAS)控制器产品实现量产配套

    来源: http://www.most.gov.cn/kjbgz/201710/t20171023_135606.htm 感谢对我们ADAS团队的肯定!

  7. 《Java 8 in Action》Chapter 11:CompletableFuture:组合式异步编程

    某个网站的数据来自Facebook.Twitter和Google,这就需要网站与互联网上的多个Web服务通信.可是,你并不希望因为等待某些服务的响应,阻塞应用程序的运行,浪费数十亿宝贵的CPU时钟周期 ...

  8. JavaScript-装饰器模式

    装饰器模式 为对象添加新功能 不改变其原有的结构和功能 传统 UML 类图 javascript 中的装饰器 装饰类 @testDec clss Demo { } function testDec(t ...

  9. Maven 阿里云仓库地址

    https://maven.aliyun.com/mvn/view 一般使用聚合仓库(group),path是仓库地址.可点击右上角“使用指南”: 附   目前阿里云仓库的地址 https://mav ...

  10. MySql 部分字段去重

    select * from personal_question_answer where answer_id in ( select min(answer_id) from personal_ques ...