离线后以宗教为第一关键字,操作时间为第二关键字排序。

<法一>块状树,线下ac,线上tle……

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
int f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define N 100001
int en,first[N],next[N<<1],v[N<<1];
void AddEdge(const int &U,const int &V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
int tops[N],en4;
bool delta[N];
struct THING{int op,type,x,y,t,p;}th[N*3];
bool operator < (const THING &a,const THING &b)
{return a.type!=b.type ? a.type<b.type : a.t<b.t;}
int n,m,w[N],ty[N],en2,en3;
int fa[N],dep[N],siz[N],top[N],sz,maxv[N],sumv[N];
int anss[N];
void makeblock()
{
q.push(1);
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
dep[v[i]]=dep[U]+1;
fa[v[i]]=U;
if(siz[top[U]]<sz)
{
++siz[top[U]];
top[v[i]]=top[U];
}
q.push(v[i]);
}
}
}
void dfs(int U,int Sumnow,int Maxnow)
{
maxv[U]=Maxnow;
sumv[U]=Sumnow;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&top[v[i]]==top[U])
dfs(v[i],Sumnow+w[v[i]],max(Maxnow,w[v[i]]));
}
void dfs0(int U)
{
maxv[U]=sumv[U]=w[U]=0;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&top[v[i]]==top[U])
dfs0(v[i]);
}
void update(const int &p,const int &val)
{
if(!delta[top[p]])
{
dfs0(top[p]);
delta[top[p]]=1;
}
w[p]=val;
if(p==top[p]) dfs(p,val,val);
else dfs(p,val+sumv[fa[p]],max(val,maxv[fa[p]]));
}
int Query_max(int U,int V)
{
int res=0;
while(U!=V)
{
if(top[U]==top[V])
{
if(dep[U]<dep[V]) swap(U,V);
res=max(res,delta[top[U]]?w[U]:0);
U=fa[U];
}
else
{
if(dep[top[U]]<dep[top[V]]) swap(U,V);
res=max(res,delta[top[U]]?maxv[U]:0);
U=fa[top[U]];
}
}
return max(res,delta[top[U]]?w[U]:0);
}
int Query_sum(int U,int V)
{
int res=0;
while(U!=V)
{
if(top[U]==top[V])
{
if(dep[U]<dep[V]) swap(U,V);
res+=(delta[top[U]]?w[U]:0);
U=fa[U];
}
else
{
if(dep[top[U]]<dep[top[V]]) swap(U,V);
res+=(delta[top[U]]?sumv[U]:0);
U=fa[top[U]];
}
}
return res+(delta[top[U]]?w[U]:0);
}
int main()
{
int A,B; char op[3];
R(n); R(m);
for(int i=1;i<=n;++i)
{
R(w[i]); R(ty[i]);
th[i].type=ty[i];
th[i].x=i;
th[i].t=i;
th[i].y=w[i];
}
for(int i=1;i<n;++i)
{
R(A); R(B);
AddEdge(A,B);
AddEdge(B,A);
}
sz=sqrt(0.03*(double)n);
for(int i=1;i<=n;i++)
{
top[i]=i;
siz[i]=1;
}
makeblock();
en2=n;
for(int i=1;i<=m;++i)
{
op[0]=getchar(); op[1]=getchar(); R(A); R(B);
if(op[0]=='C'&&op[1]=='C')
{
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=0;
th[en2].t=en2;
ty[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='C'&&op[1]=='W')
{
w[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='Q'&&op[1]=='S')
{
th[++en2].op=1;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
else
{
th[++en2].op=2;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
}
sort(th+1,th+en2+1);
memset(w,0,sizeof(w));
for(int i=1;i<=n;i++) if(top[i]==i) tops[++en4]=i;
for(int i=1;i<=en4;++i) delta[tops[i]]=1;
for(int i=1;i<=en2;++i)
{
if(th[i].type!=th[i-1].type&&i!=1)
for(int j=1;j<=en4;++j) delta[tops[j]]=0;
if(!th[i].op)
update(th[i].x,th[i].y);
else if(th[i].op==1)
anss[th[i].p]=Query_sum(th[i].x,th[i].y);
else
anss[th[i].p]=Query_max(th[i].x,th[i].y);
}
for(int i=1;i<=en3;++i)
P(anss[i]),puts("");
return 0;
}

<法二>树链剖分+线段树

#include<cstdio>
#include<algorithm>
using namespace std;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define N 100001
int en,first[N],next[N<<1],v[N<<1];
void AddEdge(const int &U,const int &V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
struct THING{int op,type,x,y,t,p;}th[N*3];
bool operator < (const THING &a,const THING &b)
{return a.type!=b.type ? a.type<b.type : a.t<b.t;}
int n,m,w[N],ty[N],en2,en3;
int anss[N];
int dep[N],siz[N],fa[N],son[N],top[N],Num[N],tot;
void dfs(int U,int d)
{
dep[U]=d;
siz[U]=1;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
fa[v[i]]=U;
dfs(v[i],d+1);
siz[U]+=siz[v[i]];
if(siz[v[i]]>siz[son[U]])
son[U]=v[i];
}
}
void dfs2(int U)
{
if(son[U])
{
top[son[U]]=top[U];
Num[son[U]]=++tot;
dfs2(son[U]);
}
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&v[i]!=son[U])
{
top[v[i]]=v[i];
Num[v[i]]=++tot;
dfs2(v[i]);
}
}
int maxv[N<<2],sumv[N<<2];
bool delta[N<<2];
void pushdown(const int &rt)
{
if(delta[rt])
{
delta[rt<<1]=delta[rt<<1|1]=1;
maxv[rt<<1]=maxv[rt<<1|1]=sumv[rt<<1]=sumv[rt<<1|1]=0;
delta[rt]=0;
}
}
void update(int p,int v,int rt,int l,int r)
{
if(l==r)
{
maxv[rt]=sumv[rt]=v;
return;
}
pushdown(rt);
int m=(l+r>>1);
if(p<=m) update(p,v,lson);
else update(p,v,rson);
sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1];
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
int query1(int ql,int qr,int rt,int l,int r)
{
if(ql<=l&&r<=qr) return sumv[rt];
pushdown(rt);
int m=(l+r>>1),res=0;
if(ql<=m) res+=query1(ql,qr,lson);
if(m<qr) res+=query1(ql,qr,rson);
return res;
}
int query2(int ql,int qr,int rt,int l,int r)
{
if(ql<=l&&r<=qr) return maxv[rt];
pushdown(rt);
int m=(l+r>>1),res=0;
if(ql<=m) res=max(res,query2(ql,qr,lson));
if(m<qr) res=max(res,query2(ql,qr,rson));
return res;
}
int Query1(int U,int V)
{
int f1=top[U],f2=top[V],res=0;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
res+=query1(Num[f1],Num[U],1,1,n);
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
return res+query1(Num[U],Num[V],1,1,n);
}
int Query2(int U,int V)
{
int f1=top[U],f2=top[V],res=0;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
res=max(res,query2(Num[f1],Num[U],1,1,n));
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
return max(res,query2(Num[U],Num[V],1,1,n));
}
int main()
{
int A,B; char op[3];
R(n); R(m);
for(int i=1;i<=n;++i)
{
R(w[i]); R(ty[i]);
th[i].type=ty[i];
th[i].x=i;
th[i].t=i;
th[i].y=w[i];
}
for(int i=1;i<n;++i)
{
R(A); R(B);
AddEdge(A,B);
AddEdge(B,A);
}
top[1]=1;
Num[1]=++tot;
dfs(1,1);
dfs2(1);
en2=n;
for(int i=1;i<=m;++i)
{
op[0]=getchar(); op[1]=getchar(); R(A); R(B);
if(op[0]=='C'&&op[1]=='C')
{
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=0;
th[en2].t=en2;
ty[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='C'&&op[1]=='W')
{
w[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='Q'&&op[1]=='S')
{
th[++en2].op=1;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
else
{
th[++en2].op=2;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
}
sort(th+1,th+en2+1);
for(int i=1;i<=en2;++i)
{
if(th[i].type!=th[i-1].type&&i!=1)
delta[1]=1;
if(!th[i].op)
update(Num[th[i].x],th[i].y,1,1,n);
else if(th[i].op==1)
anss[th[i].p]=Query1(th[i].x,th[i].y);
else
anss[th[i].p]=Query2(th[i].x,th[i].y);
}
for(int i=1;i<=en3;++i)
P(anss[i]),puts("");
return 0;
}

【块状树】【树链剖分】【线段树】bzoj3531 [Sdoi2014]旅行的更多相关文章

  1. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  2. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  3. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  4. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  5. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  6. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  7. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  8. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  9. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  10. bzoj2243[SDOI2011]染色 树链剖分+线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 9012  Solved: 3375[Submit][Status ...

随机推荐

  1. Codeforces Round #350 (Div. 2) D1

    D1. Magic Powder - 1 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. ng的ngModel用来处理表单操作

    https://segmentfault.com/a/1190000009126012

  3. linux+GraphicsMagick 安装

    转摘自:http://blog.csdn.net/fhqsse220/article/details/12995763 GraphicsMagick 安装 下载软件:download:ftp://ft ...

  4. vue之axios使用

    axios是vue-resource后出现的Vue请求数据的插件.vue更新到2.0之后,作者尤大就宣告不再对vue-resource更新,而是推荐的axios. 下面我们来使用axios npm i ...

  5. Mybatis如何查询部分字段

    解决问题:数据库表里面很多字段不太需要,有时只想取到里面的部分字段的值,如果重新定义 DTO 会比较麻烦. BookMapper.xml 文件中定义如下: ` <!-- Book全部字段 --& ...

  6. DOM遍历查找结点

    一.遍历API(2个) 1.深度优先原则遍历NodeIterator 节点迭代器 创建遍历API对象: var iterator=document.createNodeIterator(开始的父节点对 ...

  7. [BZOJ3698]XWW的难题解题报告|上下界网络流|有源汇最大流

    XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A,满足XW ...

  8. [BZOJ1005]Prufer数列+排列组合

    一棵树的Prufer数列 每次在剩下的树中找到标号最小的叶子节点(对于无根树而言即是度数为1的节点),删去. 同时将其父节点(即与其相连的唯一点)加入Prufer数列当中. 一个Prufer数列所对应 ...

  9. setInterval的使用和停用

    var res = self.setInterval(function(){ if(typeof(UE.getEditor('editor').body.innerHTML) != "und ...

  10. mysql 表的类型

    MySQL 数据表主要支持六种类型 ,分别是:BDB.HEAP.ISAM.MERGE.MYISAM.InnoBDB. 这六种又分为两类,一类是”事务安全型”(transaction-safe),包括B ...