UOJ 58 (树上带修改的莫队)
UOJ 58 糖果公园
Problem :
给一棵n个点的树,每个点上有一种颜色,对于一条路径上的点,若 i 颜色第 j 次出现对该路径权值的贡献为 w[i] * c[j], 每次询问一条路径的权值,或者修改某个点的颜色。
Solution :
树上的带修改的莫队。
使用dfs序来对左右端点进行分块。
第一关键字分块排序左端点,第二关键字分块排序右端点,第三关键字排序询问顺序。
用S(v, u)代表 v到u的路径上的结点的集合。
用root来代表根结点,用lca(v, u)来代表v、u的最近公共祖先。
那么
S(v, u) = S(root, v) xor S(root, u) xor lca(v, u)
其中xor是集合的对称差。
简单来说就是节点出现两次消掉。
lca很讨厌,于是再定义
T(v, u) = S(root, v) xor S(root, u)
观察将curV移动到targetV前后T(curV, curU)变化:
T(curV, curU) = S(root, curV) xor S(root, curU)
T(targetV, curU) = S(root, targetV) xor S(root, curU)
取对称差:
T(curV, curU) xor T(targetV, curU)= (S(root, curV) xor S(root, curU)) xor (S(root, targetV) xor S(root, curU))
由于对称差的交换律、结合律:
T(curV, curU) xor T(targetV, curU)= S(root, curV) xor S(root, targetV)
两边同时xor T(curV, curU):
T(targetV, curU)= T(curV, curU) xor S(root, curV) xor S(root, targetV)
发现最后两项很爽……哇哈哈
T(targetV, curU)= T(curV, curU) xor T(curV, targetV)
(有公式恐惧症的不要走啊 T_T)
也就是说,更新的时候,xor T(curV, targetV)就行了。
即,对curV到targetV路径(除开lca(curV, targetV))上的结点,将它们的存在性取反即可。
引用自vfk
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int N = 1000008;
int n, m, q, block_size, block_num;
vector <int> vec[N];
int cv[N], cw[N], cnt[N], a[N], b[N], vis[N];
int belong[N], num, tot1, tot2;
int fa[N][20], dep[N];
stack <int> st;
long long ans[N], sum;
struct query1
{
int u, v, ub, vb, x, id;
query1(){}
query1(int u, int v, int x, int id) : u(u), v(v), x(x), id(id)
{
ub = belong[u];
vb = belong[v];
}
bool operator < (const query1 &b) const
{
if (ub != b.ub) return ub < b.ub;
if (vb != b.vb) return vb < b.vb;
return x < b.x;
}
}q1[N];
struct query2
{
int pos, x, y;
query2(){}
query2(int pos, int x, int y) : pos(pos), x(x), y(y){}
}q2[N];
void stpop(int &num)
{
++block_num;
for (int i = 1; i <= num; ++i)
{
int p = st.top(); st.pop();
belong[p] = block_num;
}
num = 0;
}
int dfs(int u)
{
int num = 1;
st.push(u);
for (auto v : vec[u])
{
if (v == fa[u][0]) continue;
fa[v][0] = u; dep[v] = dep[u] + 1;
num += dfs(v);
if (num >= block_size) stpop(num);
}
return num;
}
int get_lca(int u, int v)
{
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = 19; i >= 0; --i)
if (d & (1 << i))
u = fa[u][i];
if (u == v) return u;
for (int i = 19; i >= 0; --i)
if (fa[u][i] != fa[v][i])
{
u = fa[u][i];
v = fa[v][i];
}
return fa[u][0];
}
void init()
{
block_size = pow(n, 2.0 / 3); block_num = 0;
for (int i = 1; i <= m; ++i) cin >> cv[i];
for (int i = 1; i <= n; ++i) cin >> cw[i];
for (int i = 1; i <= n; ++i) vec[i].clear();
for (int i = 1; i < n; ++i)
{
int u, v; cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
fa[1][0] = 0; dep[1] = 1;
int num = dfs(1);
if (num != 0) stpop(num);
assert(st.empty());
for (int i = 1; i < 20; ++i)
for (int j = 1; j <= n; ++j)
fa[j][i] = fa[fa[j][i - 1]][i - 1];
for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = a[i];
tot1 = tot2 = 0;
for (int i = 1; i <= q; ++i)
{
int t, x, y; cin >> t >> x >> y;
if (t == 0)
{
q2[++tot2] = query2(x, b[x], y);
b[x] = y;
}
else
{
++tot1;
q1[tot1] = query1(x, y, tot2, tot1);
}
}
sort(q1 + 1, q1 + tot1 + 1);
for (int i = 1; i <= n; ++i) vis[i] = 0;
for (int i = 1; i <= m; ++i) cnt[i] = 0;
}
void update(int pos)
{
if (vis[pos])
{
sum -= (long long)cw[cnt[a[pos]]] * cv[a[pos]];
cnt[a[pos]]--;
}
else
{
cnt[a[pos]]++;
sum += (long long)cw[cnt[a[pos]]] * cv[a[pos]];
}
vis[pos] ^= 1;
}
void change(int pos, int x)
{
if (vis[pos])
{
update(pos);
a[pos] = x;
update(pos);
}
else a[pos] = x;
}
void work(int u, int v)
{
int lca = get_lca(u, v);
while (u != lca)
{
update(u);
u = fa[u][0];
}
while (v != lca)
{
update(v);
v = fa[v][0];
}
}
void solve()
{
sum = 0;
for (int i = 1; i <= q1[1].x; ++i) change(q2[i].pos, q2[i].y);
work(q1[1].u, q1[1].v);
update(get_lca(q1[1].u, q1[1].v));
ans[q1[1].id] = sum;
update(get_lca(q1[1].u, q1[1].v));
for (int i = 2, u = q1[1].u, v = q1[1].v, x = q1[1].x; i <= tot1; u = q1[i].u, v = q1[i].v, x = q1[i].x, ++i)
{
for (int j = x + 1; j <= q1[i].x; ++j) change(q2[j].pos, q2[j].y);
for (int j = x; j >= q1[i].x + 1; --j) change(q2[j].pos, q2[j].x);
work(u, q1[i].u);
work(v, q1[i].v);
update(get_lca(q1[i].u, q1[i].v));
ans[q1[i].id] = sum;
update(get_lca(q1[i].u, q1[i].v));
}
for (int i = 1; i <= tot1; ++i) cout << ans[i] << endl;
}
int main()
{
cin.sync_with_stdio(0);
while (cin >> n >> m >> q)
{
init();
solve();
}
}
UOJ 58 (树上带修改的莫队)的更多相关文章
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
- 【BZOJ】2120: 数颜色 带修改的莫队算法
[题意]给定n个数字,m次操作,每次询问区间不同数字的个数,或修改某个位置的数字.n,m<=10^4,ai<=10^6. [算法]带修改的莫队算法 [题解]对于询问(x,y,t),其中t是 ...
- P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队
\(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...
- UVA - 12345 带修改的莫队
题意显然:给出初始序列,单点修改,区间查询元素的种类. 由于时限过宽,暴力可过. 比较优秀的解法应该是莫队. 带修改的莫队题解可以看https://www.luogu.org/blog/user126 ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- Machine Learning CodeForces - 940F(带修改的莫队)
题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...
- BZOJ2120 数颜色(带修改的莫队算法)
Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜 ...
- BZOJ 2120 数颜色(带修改的莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 3478 Solved: 1342 [Submit][Status][Discus ...
- Luogu P1903 BZOJ 2120 数颜色 带修改的莫队
https://www.luogu.org/problemnew/show/P1903 之前切过这道题,复习莫队再切一遍,不过我之前写的是主席树和树状数组,也不知道我当时怎么想的…… 这个题卡常我没写 ...
随机推荐
- 014、BOM与DOM对象的应用
Screen屏幕对象 Width:屏幕的宽度 Height:屏幕的高度 availWidth:屏幕的有效宽度(不含任务栏) availHeight:屏幕的有效高度(不含任务栏) colorDepth: ...
- 使用colab运行深度学习gpu应用(Mask R-CNN)实践
1,目的 Google Colaboratory(https://colab.research.google.com)是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用, ...
- es5/6数组遍历以及常用的一些方法
数组的遍历方法 1...for(var i=0;i<arr.length;i++){ } ---------------------------------------------------- ...
- LibreOJ #119. 最短路 (堆优化dijkstra)
题目描述 给一个 n(1≤2500≤n) n(1 \leq 2500 \leq n)n(1≤2500≤n) 个点 m(1≤6200≤m) m(1 \leq 6200 \leq m)m(1≤6200≤m ...
- SQLite -创建表
SQLite -创建表 SQLite CREATE TABLE语句用于创建一个新表在任何给定的数据库.创建一个基本表包括表命名和定义其列,每列的数据类型 语法: CREATE TABLE语句的基本语法 ...
- SQLite运算符
SQLite运算符 SQLite的运算符是什么? 运算符是一个保留字或一个字符主要用于SQLite语句的WHERE子句来执行操作,如比较和算术运算. 操作符用于指定条件的SQLite语句和作为连词在一 ...
- java项目部署jar包
1. 先将打包成jar包 2. 查看所有的java进程 pgrep java 3. 杀死进程 kill -9 程序号 4.执行命令 nohup java -jar admin.jar > ...
- uva10735 Euler Circuit
题外话:很多混合图问题可以转化为有向图问题(将无向边拆为两条有向边) 本题不行,因为只能经过一次 这种问题能想到网络流.. 复习欧拉回路:入度==出度 和uva1380有点相似,要先给无向边定向.原图 ...
- PHP安全之 register_globals
一.register_globals = Off 和 register_globals = On的区别 register_globals是php.ini里的一个配置,这个配置影响到php如何接收传递过 ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146
问题介绍: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146 MySql语法错误, 但是错 ...