zoukankan      html  css  js  c++  java
  • SPOJ GSS7 Can you answer these queries VII

    Can you answer these queries VII

    Time Limit: 1000ms
    Memory Limit: 262144KB
    This problem will be judged on SPOJ. Original ID: GSS7
    64-bit integer IO format: %lld      Java class name: Main

    Given a tree with N ( N<=100000 ) nodes. Each node has a interger value x_i ( |x_i|<=10000 ).

    You have to apply Q ( Q<=100000 ) operations:

    1. 1 a b : answer the maximum contiguous sum (maybe empty,will always larger than or equal to 0 ) from the path a->b ( inclusive ).

    2. 2 a b c : change all value in the path a->b ( inclusive ) to c.

    Input

    first line consists one interger N.

    next line consists N interger x_i.

    next N-1 line , each consists two interger u,v , means that node u and node v are connected

    next line consists 1 interger Q.

    next Q line : 1 a b or 2 a b c .

    Output

    For each query, output one line the maximum contiguous sum.

    Example

    Input:
    5
    -3 -2 1 2 3
    1 2
    2 3
    1 4
    4 5
    3
    1 2 5
    2 3 4 2
    1 2 5
    
    Output:
    5
    
    9
     

    Source

     
    解题:LCT
      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int maxn = 100010;
      4 struct arc{
      5     int to,next;
      6     arc(int x = 0,int y = -1){
      7         to = x;
      8         next  = y;
      9     }
     10 }e[maxn<<1];
     11 struct LCT{
     12     int fa[maxn],ch[maxn][2],sz[maxn],parent[maxn];
     13     int key[maxn],ls[maxn],rs[maxn],ms[maxn],sum[maxn];
     14     bool reset[maxn];
     15     inline void pushup(int x){
     16         sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]];
     17         sum[x] = key[x] + sum[ch[x][0]] + sum[ch[x][1]];
     18         ls[x] = max(ls[ch[x][0]],sum[ch[x][0]] + key[x] + ls[ch[x][1]]);
     19         rs[x] = max(rs[ch[x][1]],sum[ch[x][1]] + key[x] + rs[ch[x][0]]);
     20         ms[x] = max(ms[ch[x][0]],ms[ch[x][1]]);
     21         ms[x] = max(ms[x],key[x] + rs[ch[x][0]] + ls[ch[x][1]]);
     22     }
     23     inline void set(int x,int val){
     24         if(!x) return;
     25         reset[x] = true;
     26         key[x] = val;
     27         sum[x] = sz[x]*val;
     28         ls[x] = rs[x] = ms[x] = max(0,sum[x]);
     29     }
     30     inline void pushdown(int x){
     31         if(reset[x]){
     32             set(ch[x][0],key[x]);
     33             set(ch[x][1],key[x]);
     34             reset[x] = false;
     35         }
     36     }
     37     void rotate(int x,int kd){
     38         int y = fa[x];
     39         pushdown(y);
     40         pushdown(x);
     41         ch[y][kd^1] = ch[x][kd];
     42         fa[ch[x][kd]] = y;
     43         fa[x] = fa[y];
     44         ch[x][kd] = y;
     45         fa[y] = x;
     46         if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x;
     47         pushup(y);
     48     }
     49     void splay(int x,int goal = 0){
     50         pushdown(x);
     51         int y = x;
     52         while(fa[y]) y = fa[y];
     53         if(x != y){
     54             parent[x] = parent[y];
     55             parent[y] = 0;
     56             while(fa[x] != goal){
     57                 pushdown(fa[fa[x]]);
     58                 pushdown(fa[x]);
     59                 pushdown(x);
     60                 if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]);
     61                 else{
     62                     int y = fa[x],z = fa[y],s = (ch[z][0] == y);
     63                     if(x == ch[y][s]){
     64                         rotate(x,s^1);
     65                         rotate(x,s);
     66                     }else{
     67                         rotate(y,s);
     68                         rotate(x,s);
     69                     }
     70                 }
     71             }
     72             pushup(x);
     73         }
     74     }
     75     void access(int x){
     76         for(int y = 0; x; x = parent[x]){
     77             splay(x);
     78             fa[ch[x][1]] = 0;
     79             parent[ch[x][1]] = x;
     80             ch[x][1] = y;
     81             fa[y] = x;
     82             parent[y] = 0;
     83             y = x;
     84             pushup(x);
     85         }
     86     }
     87     void update(int x,int y,int z){
     88         access(y);
     89         for(y = 0; x; x = parent[x]){
     90             splay(x);
     91             if(!parent[x]){
     92                 key[x] = z;
     93                 set(y,z);
     94                 set(ch[x][1],z);
     95                 return;
     96             }
     97             fa[ch[x][1]] = 0;
     98             parent[ch[x][1]] = x;
     99             ch[x][1] = y;
    100             fa[y] = x;
    101             parent[y] = 0;
    102             y = x;
    103             pushup(x);
    104         }
    105     }
    106     int query(int x,int y){
    107         access(y);
    108         for(y = 0; x; x = parent[x]){
    109             splay(x);
    110             if(!parent[x]){
    111                 int ret = max(ms[y],ms[ch[x][1]]);
    112                 ret = max(ret,key[x] + ls[y] + ls[ch[x][1]]);
    113                 return ret;
    114             }
    115             fa[ch[x][1]] = 0;
    116             parent[ch[x][1]] = x;
    117             ch[x][1] = y;
    118             fa[y] = x;
    119             parent[y] = 0;
    120             y = x;
    121             pushup(x);
    122         }
    123     }
    124     void init(){
    125         memset(fa,0,sizeof fa);
    126         memset(parent,0,sizeof parent);
    127         memset(reset,false,sizeof reset);
    128         memset(ch,0,sizeof ch);
    129         sz[0] = 0;
    130     }
    131 }lct;
    132 int head[maxn],tot;
    133 void add(int u,int v){
    134     e[tot] = arc(v,head[u]);
    135     head[u] = tot++;
    136 }
    137 void dfs(int u,int fa){
    138     for(int i = head[u]; ~i; i = e[i].next){
    139         if(e[i].to == fa) continue;
    140         lct.parent[e[i].to] = u;
    141         dfs(e[i].to,u);
    142     }
    143 }
    144 int main(){
    145     int n,m,u,v,x,y,z,op;
    146     while(~scanf("%d",&n)){
    147         memset(head,-1,sizeof head);
    148         tot =  0;
    149         lct.init();
    150         for(int i = 1; i <= n; ++i){
    151             lct.sz[i] = 1;
    152             scanf("%d",&lct.key[i]);
    153             lct.sum[i] = lct.key[i];
    154             lct.ls[i] = lct.rs[i] = lct.ms[i] = max(0,lct.key[i]);
    155         }
    156         for(int i = 1; i < n; ++i){
    157             scanf("%d%d",&u,&v);
    158             add(u,v);
    159             add(v,u);
    160         }
    161         dfs(1,1);
    162         scanf("%d",&m);
    163         while(m--){
    164             scanf("%d%d%d",&op,&x,&y);
    165             if(op == 1) printf("%d
    ",lct.query(x,y));
    166             else{
    167                 scanf("%d
    ",&z);
    168                 lct.update(x,y,z);
    169             }
    170         }
    171     }
    172     return 0;
    173 }
    View Code
  • 相关阅读:
    OpenGL编程 基础篇(七)对象的变换——用实心体绘制3D场景
    mysql在查询结果列表前添加一列递增的序号列(最简)
    将中文字符串分割为数组 解决str_split中文乱码php
    file_get_contents 抓取网页乱码。
    js下的sleep实现
    js获取当前时间(昨天、今天、明天)
    JPA为字段设置默认值
    js实现点击按钮弹出上传文件的窗口
    Bootstrap使用模态框modal实现表单提交弹出框
    springboot使用Freemarker继承
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4887114.html
Copyright © 2011-2022 走看看