Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define DIM 50007
  6.  
  7. #define INF 1000000007
  8.  
  9. long long n;
  10.  
  11. struct Tnode{ long long sum, maxprefix, maxsuffix, maxsum; } T[4*DIM];
  12. long long a[DIM];
  13.  
  14. Tnode combine(Tnode &node1, Tnode &node2) {
  15.     Tnode resnode;
  16.     resnode.sum = node1.sum + node2.sum;
  17.     resnode.maxprefix = max(node1.maxprefix, node1.sum + node2.maxprefix);
  18.     resnode.maxsuffix = max(node1.maxsuffix + node2.sum, node2.maxsuffix);
  19.     resnode.maxsum = max(  (node1.maxsuffix + node2.maxprefix), max(node1.maxsum , node2.maxsum));
  20.  
  21.     return resnode;
  22. }
  23.  
  24. void BuildTree(int v, int tl, int tr) {
  25.     if(tl==tr) {
  26.         T[v] = { a[tl], a[tl], a[tl], a[tl] }; //відрізок з одного елемента
  27.         return;
  28.     }
  29.  
  30.     int tm = (tl+tr)/2;
  31.     BuildTree(2*v+1, tl, tm);
  32.     BuildTree(2*v+2, tm+1, tr);
  33.  
  34.     T[v] = combine(T[2*v+1] , T[2*v+2]);
  35. }
  36.  
  37. void modify(int v, int tl, int tr, int x, int val) {
  38.     if(x < tl || tr < x) return;
  39.  
  40.     if(tl==tr) {
  41.         T[v] = { val, val, val, val };
  42.         return;
  43.     }
  44.  
  45.     int tm = (tl+tr)/2;
  46.     modify(2*v+1, tl, tm, x, val);
  47.     modify(2*v+2, tm+1, tr, x, val);
  48.  
  49.     T[v] = combine(T[2*v+1], T[2*v+2]);
  50. }
  51.  
  52. Tnode getquery(int v, int tl, int tr, int L, int R) {
  53.     if(R < tl || tr < L) return {0,-INF,-INF,-INF};
  54.  
  55.     if(L <= tl && tr <= R) {
  56.         return T[v];
  57.     }
  58.  
  59.     int tm = (tl+tr)/2;
  60.     Tnode node1 = getquery(2*v+1, tl, tm, L, R);
  61.     Tnode node2 = getquery(2*v+2, tm+1, tr, L, R);
  62.  
  63.     return combine(node1, node2);
  64. }
  65.  
  66. int main() {
  67.  
  68.     scanf("%lld\n",&n);
  69.     for(int i = 1; i <= n; i++)
  70.         scanf("%lld", &a[i]);
  71.  
  72.     BuildTree(0,1,n);
  73.  
  74.     long long k,L,R,type;
  75.     Tnode item;
  76.  
  77.     scanf("%lld",&k);
  78.     for(int i = 1; i <= k; i++) {
  79.         scanf("%lld %lld %lld", &type, &L, &R);
  80.  
  81.         if(type == 0) modify(0,1,n,L,R);
  82.         else {
  83.             item = getquery(0, 1, n, L, R);
  84.             printf("%lld\n", item.maxsum);
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement