Sshenyyyu @ 2018-10-25 23:42:40
代码只是一个形式,主要想召唤出YYR
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
#include <iostream>
using namespace std;
#define ll long long
#define ull unsigned long long
const int Maxn=10000001;
const int oo=2147483647;
int xx1123,yy1123,n,m,z[Maxn],x[Maxn],y[Maxn]; string s;
int head[Maxn],next[Maxn],to[Maxn],w[Maxn],tow[Maxn],tou[Maxn],cnt;
int size[Maxn],son[Maxn],fa[Maxn],deep[Maxn],top[Maxn],id[Maxn],Cnt;
struct node { int l,r,w; }tree[Maxn];
void insert_interval(int u,int v,int p) { ++cnt; next[cnt]=head[u]; head[u]=cnt; to[cnt]=v; w[cnt]=p; }
void dfs_1(int u,int f) {
fa[u]=f;
deep[u]=deep[f]+1;
size[u]=1;
for(int i=head[u]; ~i; i=next[i]) {
int v=to[i];
if(v==f) continue;
tow[v]=w[i];
dfs_1(v,u);
if(size[v]>size[son[u]]) son[u]=v;
}
}
void dfs_2(int u,int topf) {
++Cnt;
id[u]=Cnt;
tou[Cnt]=u;
if(!son[u]) return;
dfs_2(son[u],topf);
for(int i=head[u]; ~i; i=next[i]) {
int v=to[i];
if(v==son[u]||v==fa[u]) continue;
dfs_2(v,v);
}
}
void build_tree(int index,int l,int r) {
tree[index].l=l;
tree[index].r=r;
tree[index].w=0;
if(l==r)
return;
int mid=(l+r)/2;
build_tree(index*2,l,mid);
build_tree(index*2+1,mid+1,r);
tree[index].w=max(tree[index*2].w,tree[index*2+1].w);
}
void pushdown(int index) {
tree[index*2].w=max(tree[index*2].w,tree[index].w);
tree[index*2+1].w=max(tree[index*2+1].w,tree[index].w);
}
void change_tree(int index,int l,int r,int k) {
if(tree[index].l>=l&&tree[index].r<=r) {
tree[index].w=max(k,tree[index].w);
return;
}
pushdown(index);
int mid=(tree[index].l+tree[index].r)/2;
if(l<=mid) change_tree(index*2,l,r,k);
if(r>mid) change_tree(index*2+1,l,r,k);
//tree[index].w=max(tree[index*2].w,tree[index*2+1].w);
}
int query_tree(int index,int l,int r) {
if(tree[index].l==tree[index].r) return tree[index].w;
int ans=0;
int mid=(tree[index].l+tree[index].r)/2;
pushdown(index);
if(l<=mid) ans=max(ans,query_tree(index*2,l,r));
if(r>mid) ans=max(ans,query_tree(index*2+1,l,r));
return ans;
}
int query_interval(int x,int y) {
int ans=0;
while(top[x]!=top[y]) {
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ans=max(ans,query_tree(1,id[top[x]],id[x]));
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y)
ans=max(ans,query_tree(1,id[x]+1,id[y]));
return ans;
}
void change_interval(int x,int y,int k) {
while(top[x]!=top[y]) {
if(deep[top[x]]<deep[top[y]]) swap(x,y);
change_tree(1,id[top[x]],id[x],k);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
change_tree(1,id[x]+1,id[y],k);
}
int main() {
memset(head,-1,sizeof(head));
cin>>n;
for(int i=1; i<n; i++) {
cin>>x[i]>>y[i]>>z[i];
insert_interval(x[i],y[i],z[i]);
insert_interval(y[i],x[i],z[i]);
}
dfs_1(1,0);
dfs_2(1,1);
build_tree(1,1,n);
for(int i=1; i<n; i++) change_interval(x[i],y[i],z[i]);
while(true) {
cin>>s;
if(s[0]=='D') break;
else if(s[0]=='Q') {
cin>>xx1123>>yy1123;
if(xx1123==yy1123) cout<<0<<endl;
else cout<<query_interval(xx1123,yy1123)<<endl;
}
else if(s[0]=='C') {
cin>>xx1123>>yy1123;
change_interval(x[xx1123],y[xx1123],yy1123);
}
}
return 0;
}
by Sshenyyyu @ 2018-10-25 23:53:45
@U41485 不考我就去dp了,yyr问你个问题,在什么情况下,dp状态可以省去维数
by 雪颜 @ 2018-10-25 23:56:26
作为人输的我直接被当成空气。。可以可以,你们继续。。
by iwprc @ 2018-10-25 23:56:31
@Fitzwilliam_Darcy
很多很多情况
by Sshenyyyu @ 2018-10-25 23:57:05
by Sshenyyyu @ 2018-10-25 23:57:22
@U41485 举几个例子呗!!!
by iwprc @ 2018-10-25 23:58:27
P1430 序列取数
状态一维
by iwprc @ 2018-10-25 23:59:58
P4072 [SDOI2016]征途
状态一维
by Sshenyyyu @ 2018-10-26 00:00:50
好的,我去看看,我好像只能设2维的呀哎呀呀呀!!
by iwprc @ 2018-10-26 00:03:08
P2467 [SDOI2010]地精部落
状态一维
by iwprc @ 2018-10-26 00:04:01
P4363 [九省联考2018]一双木棋chess
状态一维