• lct树上查询最大值和路径长


      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 #define ll long long
      4 #define M 1000001
      5 struct tonod{
      6     int fa,ch[2],sum,maxx,key;
      7     bool rev,is_root;
      8 }tree[M];
      9 int gs(int x){return x==tree[tree[x].fa].ch[1];}
     10 void pushrev(int x){
     11     if(!x)return;
     12     swap(tree[x].ch[0],tree[x].ch[1]);
     13     tree[x].rev^=1;
     14 }
     15 void pushdown(int x){
     16     if(tree[x].rev){
     17         pushrev(tree[x].ch[0]);
     18         pushrev(tree[x].ch[1]);
     19         tree[x].rev=0;
     20     }
     21 }
     22 void update(int x){
     23     tree[x].maxx=tree[x].key;
     24     tree[x].sum=tree[tree[x].ch[0]].sum+tree[tree[x].ch[1]].sum+tree[x].key;
     25     if(tree[x].ch[0]){
     26         tree[x].maxx=max(tree[tree[x].ch[0]].maxx,tree[x].maxx);
     27     }
     28     if(tree[x].ch[1]) tree[x].maxx=max(tree[x].maxx,tree[tree[x].ch[1]].maxx);
     29 }
     30 void rotate(int x){
     31     if(tree[x].is_root)return;
     32     int k=gs(x),fa=tree[x].fa;
     33     int fafa=tree[fa].fa;
     34     pushdown(fa);pushdown(x);
     35     tree[fa].ch[k]=tree[x].ch[k^1];
     36     if(tree[x].ch[k^1])tree[tree[x].ch[k^1]].fa=fa;
     37     tree[x].ch[k^1]=fa;
     38     tree[fa].fa=x;
     39     tree[x].fa=fafa;
     40     if(!tree[fa].is_root)tree[fafa].ch[fa==tree[fafa].ch[1]]=x;
     41     else tree[x].is_root=1,tree[fa].is_root=0;
     42     update(fa);update(x);
     43 }
     44 void push(int x){
     45     if(!tree[x].is_root)push(tree[x].fa);
     46     pushdown(x);
     47 }
     48 void splaying(int x){
     49     push(x);
     50     for(int fa;!tree[x].is_root;rotate(x)){
     51         if(!tree[fa=tree[x].fa].is_root){
     52             rotate((gs(x)==gs(fa))?fa:x);
     53         }
     54     }
     55 }
     56 void access(int x){
     57     int y=0;
     58     do{
     59         splaying(x);
     60         tree[tree[x].ch[1]].is_root=1;
     61         tree[tree[x].ch[1]=y].is_root=0;
     62         update(x);
     63         x=tree[y=x].fa;
     64     }while(x);
     65 }
     66 void moveroot(int x){
     67     access(x);
     68     splaying(x);
     69     pushrev(x);
     70 }
     71 void link(int u,int v){
     72     moveroot(u);
     73     tree[u].fa=v;
     74 }
     75 void cut(int u,int v){
     76     moveroot(u);
     77     access(v);splaying(v);
     78     pushdown(v);
     79     tree[u].fa=tree[v].ch[0]=0;
     80     update(v);
     81 }
     82 void insert(int x,int key){
     83     tree[x].sum=tree[x].maxx=tree[x].key=key;
     84     splaying(x);update(x);
     85 }
     86 int query_max(int x,int y){
     87     moveroot(x);access(y);splaying(y);
     88     printf("%d
    ",tree[y].maxx);
     89 }
     90 int query_sum(int x,int y){
     91     moveroot(x);access(y);splaying(y);
     92     printf("%d
    ",tree[y].sum);
     93 }
     94 int main(){
     95     int n;
     96     scanf("%d",&n);
     97     for(int i=1;i<=n;i++)
     98     tree[i].is_root=1;
     99     for(int i=1,a,b;i<n;i++)
    100     scanf("%d%d",&a,&b),link(a,b);
    101     for(int i=1,a;i<=n;i++)
    102     scanf("%d",&a),insert(i,a);
    103     int m;
    104     scanf("%d",&m);
    105     while(m--){
    106         char tp[10];
    107         scanf("%s",tp);
    108         if(tp[0]=='C'){
    109             int x,y;
    110             scanf("%d%d",&x,&y);
    111             insert(x,y);
    112         }
    113         else if(tp[1]=='M'){
    114             int x,y;
    115             scanf("%d%d",&x,&y);
    116             query_max(x,y);
    117         }else{
    118             int x,y;
    119             scanf("%d%d",&x,&y);
    120             query_sum(x,y);
    121         }
    122     }
    123 }
    hh
  • 相关阅读:
    前端 “一键换肤“ 的 N 种方案及css中var()和:root用法
    给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效
    Python三方模块
    Lazarus+LAMW强制APP屏幕方向
    定制lazarus compiler config
    CentOS8的网络IP配置详解
    mongoDB导入导出
    离线安装docker并导入导出镜像
    Docker初级实战
    dwm 美化
  • 原文地址:https://www.cnblogs.com/mzh2017/p/9193681.html
Copyright © 2020-2023  润新知