题目链接:P3313 [SDOI2014]旅行
题意:一棵树,树上的点有不同的权值以及属性,支持四种操作:
- 修改某个点的属性
- 修改某个点的点权
- 询问从x到y的链上,与x相同的属性的点的权值和。
- 询问从x到y的链上,与x相同属性的点权最大值。
数据范围:树的点数,属性值,询问次数 < = 1 0 5 <=10^5 <=105,一个点的点权在所有时刻都是 < = 1 0 4 <=10^4 <=104的正整数。
首先树剖,处理出每个点的dfs序对应的权值及属性值。
然后对于出现过的每个属性值,才去建树,依次做就好。
动态开点的话意思就是只有出现过的点才为他开空间,否则不开。
具体这四个操作都很好做吧,就不再赘述。
那这道题唯一的WA点就是单点修改吧,你应该修改一条边的两个端点中深度较深的节点,很多AC了的代码这块都写的不对。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
int root[maxn],tot;
struct Tree{
int lc,rc,maxx,sum;
}tree[maxn*50];
int build(){
++tot;
tree[tot].lc=tree[tot].rc=tree[tot].sum=tree[tot].maxx=0;
return tot;
}
void insert(int p,int l,int r,int id,int val){
if(l==r){
tree[p].maxx=tree[p].sum=val;
return ;
}
int mid=(l+r)>>1;
if(id<=mid){
if(!tree[p].lc) tree[p].lc=build();
insert(tree[p].lc,l,mid,id,val);
}
else{
if(!tree[p].rc) tree[p].rc=build();
insert(tree[p].rc,mid+1,r,id,val);