zoukankan      html  css  js  c++  java
  • hdu 5316 Magician 线段树

    2015 Multi-University Training Contest 3  1001

      1 #include "bits/stdc++.h"
      2 using namespace std;
      3 #define M 100010
      4 #define lson l,m,rt<<1
      5 #define rson m+1,r,rt<<1|1
      6 
      7 int T, n, m;
      8 int ans[M];
      9 struct Node
     10 {
     11     int l, r;
     12     long long sum11, sum12, sum21, sum22;
     13 }node[M<<2], query_node;
     14 const long long INF = 100000000000000 + 1;
     15 
     16 bool First;
     17 
     18 inline void PushPlus(int rt)
     19 {
     20     node[rt].sum11 = max(node[rt<<1].sum11 + node[rt<<1|1].sum21, node[rt<<1].sum12 + node[rt<<1|1].sum11);
     21     node[rt].sum12 = max(node[rt<<1].sum11 + node[rt<<1|1].sum22, node[rt<<1].sum12 + node[rt<<1|1].sum12);
     22     node[rt].sum21 = max(node[rt<<1].sum21 + node[rt<<1|1].sum21, node[rt<<1].sum22 + node[rt<<1|1].sum11);
     23     node[rt].sum22 = max(node[rt<<1].sum21 + node[rt<<1|1].sum22, node[rt<<1].sum22 + node[rt<<1|1].sum12);
     24 
     25     node[rt].sum11 = max(node[rt].sum11, node[rt<<1|1].sum11);
     26     node[rt].sum12 = max(node[rt].sum12, node[rt<<1|1].sum12);
     27     node[rt].sum21 = max(node[rt].sum21, node[rt<<1|1].sum21);
     28     node[rt].sum22 = max(node[rt].sum22, node[rt<<1|1].sum22);
     29 
     30     node[rt].sum11 = max(node[rt].sum11, node[rt<<1].sum11);
     31     node[rt].sum12 = max(node[rt].sum12, node[rt<<1].sum12);
     32     node[rt].sum21 = max(node[rt].sum21, node[rt<<1].sum21);
     33     node[rt].sum22 = max(node[rt].sum22, node[rt<<1].sum22);
     34 }
     35 
     36 
     37 void Merge_Node(Node &newNode, Node lNode, Node rNode)
     38 {
     39     newNode.sum11 = max(lNode.sum11 + rNode.sum21, lNode.sum12 + rNode.sum11);
     40     newNode.sum12 = max(lNode.sum11 + rNode.sum22, lNode.sum12 + rNode.sum12);
     41     newNode.sum21 = max(lNode.sum21 + rNode.sum21, lNode.sum22 + rNode.sum11);
     42     newNode.sum22 = max(lNode.sum21 + rNode.sum22, lNode.sum22 + rNode.sum12);
     43 
     44     newNode.sum11 = max(newNode.sum11, rNode.sum11);
     45     newNode.sum12 = max(newNode.sum12, rNode.sum12);
     46     newNode.sum21 = max(newNode.sum21, rNode.sum21);
     47     newNode.sum22 = max(newNode.sum22, rNode.sum22);
     48 
     49     newNode.sum11 = max(newNode.sum11, lNode.sum11);
     50     newNode.sum12 = max(newNode.sum12, lNode.sum12);
     51     newNode.sum21 = max(newNode.sum21, lNode.sum21);
     52     newNode.sum22 = max(newNode.sum22, lNode.sum22);
     53 }
     54 
     55 void Build(int l, int r, int rt)
     56 {
     57     if(l == r) {
     58         scanf("%d", &ans[l]);
     59         node[rt].sum11 = node[rt].sum12 = node[rt].sum21 = node[rt].sum22 = -INF;
     60         if(l & 1) {
     61             node[rt].sum11 = ans[l];
     62         }
     63         else {
     64             node[rt].sum22 = ans[l];
     65         }
     66 //        printf("l == %d  r == %d  sum11 == %lld  sum12 == %lld  sum21 == %lld  sum22 == %lld
    ", l, r, node[rt].sum11, node[rt].sum12, node[rt].sum21, node[rt].sum22);
     67         return ;
     68     }
     69     int m = ( l + r )>>1;
     70     Build(lson);
     71     Build(rson);
     72     PushPlus(rt);
     73 //    printf("l == %d  r == %d  sum11 == %lld  sum12 == %lld  sum21 == %lld  sum22 == %lld
    ", l, r, node[rt].sum11, node[rt].sum12, node[rt].sum21, node[rt].sum22);
     74 }
     75 
     76 void Update(int p, int newVal, int l, int r, int rt)
     77 {
     78     if(l == r) {
     79         if(l & 1) {
     80             node[rt].sum11 = newVal;
     81         }
     82         else {
     83             node[rt].sum22 = newVal;
     84         }
     85         return ;
     86     }
     87     int m = (l + r) >> 1;
     88     if(p <= m)
     89         Update(p, newVal, lson);
     90     else
     91         Update(p, newVal, rson);
     92     PushPlus(rt);
     93 }
     94 
     95 void Query(int L,int R,int l,int r,int rt)
     96 {
     97     if(L <= l && r <= R) {
     98 //        if(First) {
     99 //            First = 0;
    100 //            query_node = node[rt];
    101 //        }
    102 //        else {
    103             Merge_Node(query_node, query_node, node[rt]);
    104 //        }
    105         return ;
    106     }
    107     int m = (l + r) >> 1;
    108     if(L <= m)
    109         Query(L, R, lson);
    110     if(R > m)
    111         Query(L, R, rson);
    112 }
    113 
    114 
    115 int main()
    116 {
    117     int i, cmd, L, R, POS, VAL;
    118     scanf("%d", &T);
    119     while(T--) {
    120         scanf("%d%d", &n, &m);
    121         Build(1, n, 1);
    122         for(i = 1; i <= m; ++i) {
    123             scanf("%d", &cmd);
    124             if(cmd == 0) {
    125                 scanf("%d%d", &L, &R);
    126 //                First = 1;
    127                 query_node.sum11 = query_node.sum12 = query_node.sum21 = query_node.sum22 = -INF;
    128                 Query(L, R, 1, n, 1);
    129                 printf("%lld
    ", max( max(query_node.sum11, query_node.sum12), max(query_node.sum21, query_node.sum22) ));
    130             }
    131             else {
    132                 scanf("%d%d", &POS, &VAL);
    133                 Update(POS, VAL, 1, n, 1);
    134             }
    135         }
    136     }
    137 }
  • 相关阅读:
    总结几个面试题
    产生下一个排列数的算法
    所谓码农
    简记微软实习生面试
    二维数组作为函数的参数传递
    详细解说 STL 排序(Sort)
    copy()之绝版应用
    STL标准模板库(简介)
    访问控制和继承方式
    常用软件开发模型比较分析
  • 原文地址:https://www.cnblogs.com/AC-Phoenix/p/4684232.html
Copyright © 2011-2022 走看看