4129: Haruna’s Breakfast

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 835  Solved: 409
[Submit][Status][Discuss]

Description

Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了一棵

树上,每个结点都有一样食材,Shimakaze要考验一下她。
每个食材都有一个美味度,Shimakaze会进行两种操作:
1、修改某个结点的食材的美味度。
2、对于某条链,询问这条链的美味度集合中,最小的未出现的自然数是多少。即mex值。
请你帮帮Haruna吧。
 

Input

第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数。

第二行包括n个整数a1...an,代表每个结点的食材初始的美味度。
接下来n-1行,每行包括两个整数u,v,代表树上的一条边。
接下来m 行,每行包括三个整数
0 u x 代表将结点u的食材的美味度修改为 x。
1 u v 代表询问以u,v 为端点的链的mex值。
 

Output

对于每次询问,输出该链的mex值。

 

Sample Input

10 10
1 0 1 0 2 4 4 0 1 0
1 2
2 3
2 4
2 5
1 6
6 7
2 8
3 9
9 10
0 7 14
1 6 6
0 4 9
1 2 2
1 1 8
1 8 3
0 10 9
1 3 5
0 10 0
0 7 7

Sample Output

0
1
2
2
3

HINT

1<=n<=5*10^4

1<=m<=5*10^4
0<=ai<=10^9

Source

析:树上莫队,还是带修改的,和普通的带修莫队差不多,也就是在树上,对树上分块,然后对于mex,进行分块,因为最多出现n-1个整数也就是0-n-1,其他的都可以忽略。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 5e4 + 10;
const int maxm = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int SIZE, b, u, v;
int pos[maxn], a[maxn], t[maxn];
struct Edge{
int to, next;
};
Edge edges[maxn<<1];
int head[maxn], cnt;
int p[20][maxn];
int dep[maxn], st[maxn], top; struct Query{
int id, u, v, tim;
bool operator < (const Query &q) const{
if(pos[u] != pos[q.u]) return pos[u] < pos[q.u];
if(pos[v] != pos[q.v]) return pos[v] < pos[q.v];
return tim < q.tim;
}
};
struct Update{
int pos, now, last;
};
Query q[maxn];
Update pp[maxn];
int cur; void addEdge(int u, int v){
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u, int fa, int d){
p[0][u] = fa; dep[u] = d;
int bot = top;
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(v == fa) continue;
dfs(v, u, d + 1);
if(top - bot >= SIZE){
++b;
while(top != bot) pos[st[top--]] = b;
}
}
st[++top] = u;
} void init(){
dfs(1, 0, 0);
for(int k = 0; k < 18; ++k)
for(int v = 1; v <= n; ++v)
p[k+1][v] = p[k][v] == 0 ? 0 : p[k][p[k][v]];
} inline int LCA(int u, int v){
if(dep[u] > dep[v]) swap(u, v);
for(int k = 0; k < 18; ++k)
if(dep[v] - dep[u] >> k & 1) v = p[k][v];
if(u == v) return u;
for(int k = 17; k >= 0; --k)
if(p[k][u] != p[k][v]){
u = p[k][u];
v = p[k][v];
}
return p[0][u];
} struct Block{
int n, pos[maxn], SIZE;
int cnt[maxn], sum[maxn/225+10];
void init(){
SIZE = 225;
for(int i = 0; i <= n; ++i) pos[i] = i / SIZE;
ms(sum, 0); ms(cnt, 0);
}
inline void add(int x){ if(x < n) sum[pos[x]] += ++cnt[x] == 1; }
inline void del(int x){ if(x < n) sum[pos[x]] -= --cnt[x] == 0; }
inline int mex(){
for(int i = 0; ; ++i) if(sum[i] != SIZE){
for(int j = i*SIZE; ; ++j) if(!cnt[j]) return j;
}
}
};
Block B;
bool vis[maxn];
int ans[maxn]; inline void update(int pos, int now){
if(vis[pos]) B.del(a[pos]), B.add(now);
a[pos] = now;
} inline void Xor(int u){
if(vis[u]) B.del(a[u]), vis[u] = 0;
else B.add(a[u]), vis[u] = 1;
} inline void mov(int u, int v){
if(dep[u] > dep[v]) swap(u, v);
while(dep[u] < dep[v]) Xor(v), v = p[0][v];
while(u != v) Xor(u), Xor(v), u = p[0][u], v = p[0][v];
} int main(){
scanf("%d %d", &n, &m); ms(head, -1);
for(int i = 1 ; i <= n; ++i){
scanf("%d", a + i);
t[i] = a[i];
}
SIZE = pow(n, 0.45);
for(int i = 1; i < n; ++i){
int u, v; scanf("%d %d", &u, &v);
addEdge(u, v); addEdge(v, u);
}
init();
while(top) pos[st[top--]] = b;
int qcnt = 0, time = 0;
for(int i = 0; i < m; ++i){
int op, l, r; scanf("%d %d %d", &op, &l, &r);
if(op) q[++qcnt] = (Query){qcnt, l, r, time};
else pp[++time] = (Update){l, r, t[l]}, t[l] = r;
}
sort(q + 1, q + qcnt + 1);
B.n = n; B.init();
u = v = 1;
for(int i = 1; i <= qcnt; ++i){
while(cur < q[i].tim) ++cur, update(pp[cur].pos, pp[cur].now);
while(cur > q[i].tim) update(pp[cur].pos, pp[cur].last), --cur;
while(u != q[i].u) mov(u, q[i].u), u = q[i].u;
while(v != q[i].v) mov(v, q[i].v), v = q[i].v;
int lca = LCA(u, v);
Xor(lca); ans[q[i].id] = B.mex(); Xor(lca);
}
for(int i = 1; i <= qcnt; ++i) printf("%d\n", ans[i]);
return 0;
}

  

BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)的更多相关文章

  1. BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )

    题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...

  2. bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...

  3. 【BZOJ-2453&2120】维护队列&数颜色 分块 + 带修莫队算法

    2453: 维护队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 653  Solved: 283[Submit][Status][Discuss] ...

  4. [BZOJ4129]Haruna’s Breakfast(树上带修改莫队)

    BZOJ3585,BZOJ2120,BZOJ3757三合一. 对于树上路径问题,树链剖分难以处理的时候,就用树上带修改莫队. 这里的MEX问题,使用BZOJ3585的分块方法,平衡了时间复杂度. 剩下 ...

  5. BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)

    题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...

  6. 【BZOJ】4129: Haruna’s Breakfast 树分块+带修改莫队算法

    [题意]给定n个节点的树,每个节点有一个数字ai,m次操作:修改一个节点的数字,或询问一条树链的数字集合的mex值.n,m<=5*10^4,0<=ai<=10^9. [算法]树分块+ ...

  7. BZOJ 2120 数颜色 (带修莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6367  Solved: 2537[Submit][Status][Discuss] ...

  8. 【带修莫队】【权值分块】bzoj3196 Tyvj 1730 二逼平衡树

    这题用了三种算法写: 分块+二分:O(n*sqrt(n*log(n)) 函数式权值分块:O(n*sqrt(n)) 带修莫队+权值分块:O(n5/3) 结果……复杂度越高的实际上跑得越快……最后这个竟然 ...

  9. 牛客挑战赛48E-速度即转发【带修莫队,分块】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/11161/E 题目大意 给出\(n\)个数字的一个序列,\(m\)个操作. 给出\(l,r,k\),求一个最大 ...

随机推荐

  1. eclipse Mars4.5.2安装fatjar

    试了在eclipse下添加plugins的方法,但是并没有生效 最后看了一篇博客@参考博客 原文转载: 首先声明,eclipse luna 和mars 楼主亲测可用. .安装Eclipse2.0版本的 ...

  2. 20165315 预备作业3 Linux安装及学习

    20165315 预备作业3 Linux安装及学习 一.在自己笔记本上安装Linux操作系统 因为对操作电脑的不熟悉,我在第一项任务上就花费了一定的时间,在安装过程有如下问题: 我的电脑是苹果公司的M ...

  3. log4j日志配置(按天/按日)

    项目中尽管对log4j有基本的配置,例如按天生成日志文件以作区分,但如果系统日志文件过大,则就需要考虑以更小的单位切分或者其他切分方式.下面就总结一下log4j常用的配置参数以及切分日志的不同方式. ...

  4. 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String

    json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...

  5. 7-OKHttp使用详解,步骤挺详细的,适合初学者使用!

    OKHttp使用详解,步骤挺详细的,适合初学者使用! 一,OKHttp介绍 okhttp是一个第三方类库,用于android中请求网络. 这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Squ ...

  6. YII2中日志的配置与使用

    YII2中给我们提供了非常方便的日志组件,只需要简单配置一下就可以使用. 我们在config/web.php中配置如下: return [ //log必须在bootstrap期间就被加载,便于及时调度 ...

  7. php 通过stomp协议连接ActiveMQ

    一.安装php的stomp扩展 http://pecl.php.net/package/stomp 如:stomp-2.0.0.tgz > tar xf stomp-1.0.9.tgz > ...

  8. 比特币测试网络搭建以及RPC服务开启-配置注意事项

    .bitcoin QA Test环境 启动指定参数: "C:\Program Files (x86)\Bitcoin\bitcoin-qt.exe" -testnet -serve ...

  9. idea快捷键及快捷方法(待完善)

    一.快捷键 alt+insert      插入get.set.构造函数等 ctrl+shift+alt+g   生成注释文档 ctrl+shift+alt+z   移除注释文档 Ctrl + Alt ...

  10. java8 数据结构的改变(二) 对ConcurrentHashMap影响

    https://www.cnblogs.com/study-everyday/p/6430462.html http://www.importnew.com/22007.html