BZOJ

LOJ

洛谷

惊了,18年了还有省选出模板题吗= =

做这题就是练模板的,我就知道我忘的差不多了


询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分。

注意求询问二的时候,为了方便就先算一下LCA的答案。


//83472kb	3316ms
#include <cstdio>
#include <cctype>
#include <algorithm>
#define BIT 29
#define gc() getchar()
#define MAXIN 300000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5; int Enum,H[N],nxt[N<<1],to[N<<1],A[N],root1[N],root2[N],dfn[N],R[N],sz[N],dep[N],fa[N],son[N],top[N];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Trie
{
#define S N*(BIT+2)//+2!
int tot,sz[S],son[S][2];
void Insert(int x,int y,int v)
{
for(int i=BIT; ~i; --i)
{
int c=v>>i&1;
son[x][c]=++tot, son[x][c^1]=son[y][c^1];
x=tot, y=son[y][c], sz[x]=sz[y]+1;
}
}
int Query1(int x,int y,int v)
{
int res=0;
for(int i=BIT; ~i; --i)
{
int c=(v>>i&1)^1;
if(sz[son[x][c]]-sz[son[y][c]]>0) res|=1<<i;
else c^=1;
x=son[x][c], y=son[y][c];
}
return res;
}
int Query2(int x,int y,int z,int v)
{
int res=0,tmp=A[z]^v; z=root2[z];
for(int i=BIT; ~i; --i)
{
int c=(v>>i&1)^1;
if(sz[son[x][c]]+sz[son[y][c]]-2*sz[son[z][c]]>0) res|=1<<i;
else c^=1;
x=son[x][c], y=son[y][c], z=son[z][c];
}
return std::max(res,tmp);
}
}T1,T2; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
inline void AE(int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum;
}
inline int LCA(int u,int v)
{
while(top[u]!=top[v]) dep[top[u]]>dep[top[v]]?u=fa[top[u]]:v=fa[top[v]];
return dep[u]>dep[v]?v:u;
}
void DFS1(int x)
{
int mx=0; sz[x]=1, T2.Insert(root2[x]=++T2.tot,root2[fa[x]],A[x]);
for(int i=H[x],v; i; i=nxt[i])
if((v=to[i])!=fa[x])
fa[v]=x, dep[v]=dep[x]+1, DFS1(v), sz[x]+=sz[v], sz[v]>mx&&(mx=sz[v],son[x]=v);
}
void DFS2(int x,int tp)
{
static int Index=0;
top[x]=tp, dfn[x]=++Index, T1.Insert(root1[Index]=++T1.tot,root1[Index-1],A[x]);
if(son[x])
{
DFS2(son[x],tp);
for(int i=H[x],v; i; i=nxt[i])
if((v=to[i])!=fa[x]&&v!=son[x]) DFS2(v,v);
}
R[x]=Index;
} int main()
{
const int n=read(),Q=read();
for(int i=1; i<=n; ++i) A[i]=read();
for(int i=1; i<n; ++i) AE(read(),read());
DFS1(1), DFS2(1,1);
for(int i=1,x,y; i<=Q; ++i)
switch(read())
{
case 1: x=read(),printf("%d\n",T1.Query1(root1[R[x]],root1[dfn[x]-1],read())); break;
case 2: x=read(),y=read(),printf("%d\n",T2.Query2(root2[x],root2[y],LCA(x,y),read())); break;
} return 0;
}

BZOJ.5338.[TJOI2018]xor(可持久化Trie)的更多相关文章

  1. BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序

    强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...

  2. bzoj 5338: [TJOI2018]xor (树链剖分+可持久化01Trie)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5338 题面: 5338: [TJOI2018]xor Time Limit: 30 Sec  ...

  3. [BZOJ5338][TJOI2018]xor(可持久化Trie)

    可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...

  4. BZOJ_5338_ [TJOI2018]xor_可持久化trie

    BZOJ_5338_ [TJOI2018]xor_可持久化trie Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...

  5. bzoj 4546: codechef XRQRS [可持久化Trie]

    4546: codechef XRQRS 可持久化Trie codechef上过了,bzoj上蜜汁re,看别人说要开5.2e5才行. #include <iostream> #includ ...

  6. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

  7. BZOJ 3166 HEOI2013 ALO 可持久化trie+st表

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3166(洛谷上也有) 题意概述: 给出一个序列,对于一个区间,其权值为区间中的次大值亦或区 ...

  8. bzoj 3166 [Heoi2013]Alo 可持久化Trie

    3166: [Heoi2013]Alo Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1227  Solved: 569[Submit][Status ...

  9. 洛谷 P4592: bzoj 5338: [TJOI2018]异或

    题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...

随机推荐

  1. bzoj 2721

    题解:首先推一发式子(见csdn https://blog.csdn.net/lleozhang/article/details/83415995) 因为x是整数,所以x的数量显然为能使取得整数的t的 ...

  2. Centos系统压力测试 ab 命令安装与使用

    Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 ab运行需要依赖apr-util包,安装命令为: y ...

  3. Linux下source命令详解

    source命令用法 source FileName source命令作用 在当前bash环境下读取并执行FileName中的命令. *注:该命令通常用命令“.”来替代. 使用范例: source f ...

  4. vue :基本语法格式

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Vu ...

  5. spring cloud 容错之zuul回退和Zuul过滤器

    一.容错:Zuul回退 如果微服务下线了,针对每个微服务,都需要回复一个中文提示,而不是报异常 1.新建ConsumerFallbackProvider.java package com.pupeiy ...

  6. 获取访问IP

    public static String GetIP() { String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ ...

  7. 2017-2018-2 20165328 实验三《敏捷开发与XP实践》实验报告

    一.实践-1: 要求:参考http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD安装alibaba插件,解决代码中的规范问题. 在ID ...

  8. ubuntu安装php+mysql+apche

    步骤一,安装apache2 ? sudo apt-get install apache2 安装完成. 运行如下命令重启下: ? sudo /etc/init.d/apache2 restart 在浏览 ...

  9. 搭建自己的docker仓库

    https://docs.docker.com/registry/deploying/#run-a-local-registry https://docs.docker.com/registry/in ...

  10. Python 私有属性

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-08-05 # 类 ...