@YYR召唤DaLao

P4114 Qtree1

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:49:26

。。。。kc你来干什么


by AK_Automata @ 2018-10-25 23:49:49

好的,我就不当灯泡了。。。@Fitzwilliam_Darcy


by AK_Automata @ 2018-10-25 23:50:02

当灯泡。。。@Fitzwilliam_Darcy


by Sshenyyyu @ 2018-10-25 23:50:26

你正好夹在中间,666


by kgory @ 2018-10-25 23:50:41

真有爱


by AK_Automata @ 2018-10-25 23:51:55

灯泡都清场了,散了散了。。@kgory


by Sshenyyyu @ 2018-10-25 23:52:01

请做一个好公民,不要乱说


by iwprc @ 2018-10-25 23:52:15

NOIP哪年考了树剖啊?


by 雪颜 @ 2018-10-25 23:52:45

@Fitzwilliam_Darcy 我从来召唤不出来。。@U41485 嗯,这就是人赢和人输的区别。。我大概是个loser


by Sshenyyyu @ 2018-10-25 23:52:46

@ U41485 单点改了还是不对怎么办


上一页 | 下一页