@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 iwprc @ 2018-10-25 23:43:29

啊?


by Sshenyyyu @ 2018-10-25 23:44:36

@U41485 666好久没@你了,好像@你呀!!@U41485@U41485@U41485@U41485@U41485@U41485@U41485


by AK_Automata @ 2018-10-25 23:45:28

。。。


by AK_Automata @ 2018-10-25 23:45:42

%%%orz@U41485


by iwprc @ 2018-10-25 23:47:09

谁告诉你单点修改区间查询要pushdown的?


by AK_Automata @ 2018-10-25 23:47:39

又是紫题&树剖。。。


by Sshenyyyu @ 2018-10-25 23:47:45

不知道,我也不知道什么时候要pushdown呀


by 繁星灬夏若離 @ 2018-10-25 23:48:30

%%% 今年不考树剖真对不起您这份执着。。。。 qwq


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

楼上2位。。。


by Sshenyyyu @ 2018-10-25 23:49:11

@繁星灬夏若離 楼上是我npy


| 下一页