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

<法一>块状树,线下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. LaTeX的图片插入及排版[转]

    LaTeX中一般只直接支持插入eps(Encapsulated PostScript)格式的图形文件, 因此在图片插入latex文档之前应先设法得到图片的eps格式的文件. UNIX下的各种应用软件都 ...

  2. SCOI2010 传送带 [三分/模拟退火]

    题目描述 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段AB和线段CD.lxhgww在AB上的移动速度为P,在CD上的移动速度为Q,在平面上的移动速度R.现在lxh ...

  3. [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能

    首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检 ...

  4. BZOJ 2457 双端队列(思维

    2457: [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 582  Solved: 253[Submit][Sta ...

  5. Java并发(10)- 简单聊聊JDK中的七大阻塞队列

    引言 JDK中除了上文提到的各种并发容器,还提供了丰富的阻塞队列.阻塞队列统一实现了BlockingQueue接口,BlockingQueue接口在java.util包Queue接口的基础上提供了pu ...

  6. 浏览器web端详解

    一.背景介绍浏览器是前端工程师或页面重构师工作中必不可少的,WEB页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验,特别是浏览器渲染页面的原理,页面渲染就是浏览器将HTML ...

  7. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  8. jQuery点击自身以外地方关闭弹出层

    转载自:http://blog.163.com/abkiss@126/blog/static/325941002012111754349590/ 主要功能是点击显示,然后通过点击页面的任意位置都能关闭 ...

  9. java中final修饰符的使用

    1.final修饰符的用法: final可以修饰变量,被final修饰的变量被赋初始值之后,不能对它重新赋值. final可以修饰方法,被final修饰的方法不能被重写. final可以修饰类,被fi ...

  10. request.getRequestDispatcher(url).forward(request, response)

    request.getRequestDispatcher().forward(request, response) 意思是将客户端的请求转向到 getRequestDispatcher()方法中参数定 ...