LINK:因懒无名



20分显然有\(n\cdot q\)的暴力。

还有20分 每次只询问一种颜色的直径不过带修改。

容易想到利用线段树维护直径就可以解决了。

当然也可以进行线段树分治 每种颜色存一下直径的端点即可。

考虑100分。

考虑到直径两个端点有区间可加性 所以直接外面套一个线段树维护区间端点即可。

修改采用上述做法两种均可。然后就做完了.

code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<queue>
#include<deque>
#include<stack>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 100001
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define get(x) x=read()
#define gt(x) scanf("%d",&x)
#define gi(x) scanf("%lf",&x)
#define put(x) printf("%d\n",x)
#define putl(x) printf("%lld\n",x)
#define rep(p,n,i) for(RE int i=p;i<=n;++i)
#define go(x) for(int i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define fep(n,p,i) for(RE int i=n;i>=p;--i)
#define vep(p,n,i) for(RE int i=p;i<n;++i)
#define pii pair<int,int>
#define mk make_pair
#define RE register
#define P 1000000007ll
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define EPS 1e-4
#define sq sqrt
#define S second
#define F first
#define mod 1000000007
#define l(p) t[p].l
#define r(p) t[p].r
#define L(p) t[p].L
#define R(p) t[p].R
#define mx(p) t[p].mx
using namespace std;
char *fs,*ft,buf[1<<15];
inline char gc()
{
return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
RE int x=0,f=1;RE char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f; }
const int MAXN=100010;
int n,m,len,Q,maxx,id,cnt,vv,ww,rt;
int c[MAXN],root[MAXN],d[MAXN];
int f[MAXN<<1][20],Log[MAXN<<1],dfn[MAXN],g[MAXN],pos[MAXN];
int lin[MAXN],nex[MAXN<<1],ver[MAXN<<1];
vector<int>w;
inline void add(int x,int y)
{
ver[++len]=y;
nex[len]=lin[x];
lin[x]=len;
}
inline void dfs(int x,int fa)
{
f[++cnt][0]=x;dfn[x]=cnt;d[x]=d[fa]+1;
//f为ST表元素 ->dfn->cnt
g[++id]=x;pos[id]=x;//g为dfs序->pos->id;
go(x)if(tn!=fa)
{
dfs(tn,x);
f[++cnt][0]=x;
}
}
inline int cmp(int x,int y){return d[x]>d[y]?y:x;}
inline int LCA(int x,int y)
{
x=dfn[x];y=dfn[y];
if(x>y)swap(x,y);
int z=Log[y-x+1];
return cmp(f[x][z],f[y-(1<<z)+1][z]);
}
inline int dist(int x,int y)
{
if(!x||!y)return 0;
int lca=LCA(x,y);
return d[x]+d[y]-2*d[lca];
}
struct wy
{
int l,r,L,R;int mx;
inline wy friend operator +(wy a,wy b)
{
if(!b.L&&!b.R)return a;
if(!a.L&&!a.R)return b;
wy c;
if(a.mx>b.mx)c=a;else c=b;
if((ww=dist(a.L,b.L))>c.mx)c.L=a.L,c.R=b.L,c.mx=ww;
if((ww=dist(a.L,b.R))>c.mx)c.L=a.L,c.R=b.R,c.mx=ww;
if((ww=dist(a.R,b.L))>c.mx)c.L=a.R,c.R=b.L,c.mx=ww;
if((ww=dist(a.R,b.R))>c.mx)c.L=a.R,c.R=b.R,c.mx=ww;
return c;
}
}t[MAXN*60];
inline void insert(int &p,int l,int r,int x,int w)
{
if(!p)p=++vv;
if(l==r)
{
L(p)=R(p)=w;
return;
}
int mid=(l+r)>>1;
if(x<=mid)insert(l(p),l,mid,x,w);
else insert(r(p),mid+1,r,x,w);
int wl=l(p),wr=r(p);
t[p]=t[l(p)]+t[r(p)];
t[p].l=wl;t[p].r=wr;
}
inline void build(int &p,int l,int r)
{
p=++vv;
if(l==r){t[p]=t[root[l]];return;}
int mid=(l+r)>>1;
build(l(p),l,mid);
build(r(p),mid+1,r);
int wl=l(p),wr=r(p);
t[p]=t[l(p)]+t[r(p)];
t[p].l=wl;t[p].r=wr;
}
inline void change(int p,int l,int r,int x)
{
if(l==r)
{
t[p]=t[root[x]];
return;
}
int mid=(l+r)>>1;
if(x<=mid)change(l(p),l,mid,x);
else change(r(p),mid+1,r,x);
int wl=l(p),wr=r(p);
t[p]=t[l(p)]+t[r(p)];
t[p].l=wl;t[p].r=wr;
}
inline wy ask(int p,int l,int r,int L,int R)
{
if(!p)return t[0];
if(L<=l&&R>=r)return t[p];
int mid=(l+r)>>1;
if(R<=mid)return ask(l(p),l,mid,L,R);
if(L>mid)return ask(r(p),mid+1,r,L,R);
return ask(l(p),l,mid,L,R)+ask(r(p),mid+1,r,L,R);
}
int main()
{
freopen("noname.in","r",stdin);
freopen("noname.out","w",stdout);
get(n);get(m);get(Q);
rep(1,n,i)get(c[i]);
rep(2,n,i)
{
int get(x),get(y);
add(x,y);add(y,x);
}
dfs(1,0);
rep(2,cnt,i)Log[i]=Log[i>>1]+1;
rep(1,Log[cnt],j)rep(1,cnt-(1<<j)+1,i)f[i][j]=cmp(f[i][j-1],f[i+(1<<j-1)][j-1]);
rep(1,n,i)insert(root[c[i]],1,n,g[i],i);
build(rt,1,m);
rep(1,Q,i)
{
int get(op),get(L),get(R);
if(op==1)
{
insert(root[c[L]],1,n,g[L],0);
change(rt,1,m,c[L]);
c[L]=R;
insert(root[c[L]],1,n,g[L],L);
change(rt,1,m,c[L]);
}
else put(ask(rt,1,m,L,R).mx);
}
return 0;
}
</details>

7.18 NOI模拟赛 因懒无名 线段树分治 线段树维护直径的更多相关文章

  1. 7.18 NOI模拟赛 树论 线段树 树链剖分 树的直径的中心 SG函数 换根

    LINK:树论 不愧是我认识的出题人 出的题就是牛掰 == 他好像不认识我 考试的时候 只会写42 还有两个subtask写挂了 拿了37 确实两个subtask合起来只有5分的好成绩 父亲能转移到自 ...

  2. 计蒜客模拟赛 #5 (B 题) 动态点分治+线段树

    虽然是裸的换根dp,但是为了在联赛前锻炼码力,强行上了点分树+线段树. 写完+调完总共花了不到 $50$ 分钟,感觉还行. code: #include <bits/stdc++.h> # ...

  3. NOI模拟赛 Day1

    [考完试不想说话系列] 他们都会做呢QAQ 我毛线也不会呢QAQ 悲伤ING 考试问题: 1.感觉不是很清醒,有点困╯﹏╰ 2.为啥总不按照计划来!!! 3.脑洞在哪里 4.把模拟赛当作真正的比赛,紧 ...

  4. 6.28 NOI模拟赛 好题 状压dp 随机化

    算是一道比较新颖的题目 尽管好像是两年前的省选模拟赛题目.. 对于20%的分数 可以进行爆搜,对于另外20%的数据 因为k很小所以考虑上状压dp. 观察最后答案是一个连通块 从而可以发现这个连通块必然 ...

  5. 【2018.12.10】NOI模拟赛3

    题目 WZJ题解 大概就是全场就我写不过 $FFT$ 系列吧……自闭 T1 奶一口,下次再写不出这种 $NTT$ 裸题题目我就艹了自己 -_-||| 而且这跟我口胡的自创模拟题 $set1$ 的 $T ...

  6. NOI 模拟赛 #2

    得分非常惨惨,半个小时写的纯暴力 70 分竟然拿了 rank 1... 如果 OYJason 和 wxjor 在可能会被爆踩吧 嘤 T1 欧拉子图 给一个无向图,如果一个边集的导出子图是一个欧拉回路, ...

  7. 【NOI P模拟赛】最短路(树形DP,树的直径)

    题面 给定一棵 n n n 个结点的无根树,每条边的边权均为 1 1 1 . 树上标记有 m m m 个互不相同的关键点,小 A \tt A A 会在这 m m m 个点中等概率随机地选择 k k k ...

  8. NOI模拟赛Day5

    T1 有and,xor,or三种操作,每个人手中一个数,求和左边进行某一种运算的最大值,当t==2时,还需要求最大值的个数. test1 20% n<=1000 O(n^2)暴力 test2 2 ...

  9. NOI模拟赛Day4

    看到成绩的时候我的内心** woc第一题写错了呵呵呵呵呵呵呵呵 人不能太浪,会遭报应的** ------------------------------------------------------ ...

随机推荐

  1. zabbix fping 监控网络质量

    1,zabbix server (proxy)安装fping wget http://www.fping.org/dist/fping-3.16.tar.gz tar zxvf fping-3.16. ...

  2. Xor_Sum 题解

    题目 You are given a positive integer \(N(1≦N≦10^{18})\). Find the number of the pairs of integers \(u ...

  3. Redis哨兵集群创建脚本--v2

    1. 基础环境 操作系统版本  CentOS Linux release 7.6.1810 (Core) Docker 版本  19.03.11, build 42e35e61f3 Redis  版本 ...

  4. 2020最新的Spring Boot 分布式锁的具体实现(内附代码)

    前言 面试总是会被问到有没有用过分布式锁.redis 锁,大部分读者平时很少接触到,所以只能很无奈的回答 "没有".本文通过 Spring Boot 整合 redisson 来实现 ...

  5. 博弈论 | 详解搞定组合博弈问题的SG函数

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天这篇是算法与数据结构专题的第27篇文章,我们继续深入博弈论问题.今天我们要介绍博弈论当中非常重要的一个定理和函数,通过它我们可以解决许多 ...

  6. element-ui自定义table表头,修改标题样式、添加tooltip及 :render-header使用简介

    修改列标题样式1.在列标题后面加一个图标. 以element-ui官方文档一个table表格为例,我们在地址的后面加一个定位标志的图标,代码如下: <template> <el-ta ...

  7. keepalived 热备

    概述 keepalived高可用集群      keepalived最初是为了LVS的,因为LVS无法进行自动检测服务器的节点状态(可以自动部署LVS)     keeplived后来加入VRRP给功 ...

  8. 阿里云centos7服务器nginx配置及常见问题解答

    前言: 本文参考了jackyzm的博客:https://www.cnblogs.com/jackyzm/p/9600738.html,进行了内容的更新,并请注意这里适用的版本是centos7的版本.并 ...

  9. python 装饰器(七):装饰器实例(四)类装饰器装饰类以及类方法

    类装饰器装饰类方法 不带参数 from functools import wraps import types class CatchException: def __init__(self,orig ...

  10. Python读取文件基本方法

    在日常开发过程中,经常遇到需要读取配置文件,这边就涉及到一个文本读取的方法. 这篇文章主要以Python读取文本的基础方法为本,添加读取整篇文本返回字符串,读取键值对返回字典,以及读取各个项返回列表的 ...