Guest User

Gss3 spoj Fenwick

a guest
Sep 21st, 2025
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4. #include <random>
  5. #include <cassert>
  6. #include <chrono>
  7. #include <tuple>
  8.  
  9. using namespace std;
  10.  
  11. // Fenwick Tree (Range Update/Range Query) - Provided in the prompt
  12. #define LSB(x)((x) & -(x))
  13. #define PARENT(x)(x + LSB(x))
  14. #define LEFT_SIBLING(x) ((x) - LSB(x))
  15.  
  16. using namespace std;
  17.  
  18. FILE * fpbuf = stdin;
  19.  
  20. const int BUFSIZE = 6140;
  21.  
  22. int ibuf = 0, ibufsz = 0;
  23.  
  24. char buffer[BUFSIZE];
  25.  
  26. #define fillbuf() { ibufsz = fread(buffer, sizeof(char), BUFSIZE, fpbuf); ibuf = 0; }
  27.  
  28. inline int readint() {
  29.     int no = 0;
  30.     while (ibuf == ibufsz || buffer[ibuf] == '\n' || buffer[ibuf] == '\r' || buffer[ibuf] == ' ') {
  31.         if (ibuf == ibufsz) {
  32.             fillbuf();
  33.             if (ibufsz == 0) break;
  34.         } else ibuf++;
  35.     }
  36.     int sgn = 1;
  37.     if (buffer[ibuf] == '-') {
  38.         sgn *= -1;
  39.         ++ibuf;
  40.     }
  41.  
  42.     while (1) {
  43.         if (ibuf == ibufsz) {
  44.             fillbuf();
  45.             if (ibufsz == 0) break;
  46.         }
  47.         char ch = buffer[ibuf++];
  48.         if (ch == '\r' || ch == '\n' || ch == EOF || ch == ' ') break;
  49.         no = (no << 3) + (no << 1) + ch - 48;
  50.     }
  51.     return no * sgn;
  52. }
  53.  
  54. inline int readstr(char * d, bool spacebrk = true) {
  55.     while (ibuf == ibufsz || buffer[ibuf] == '\n' || buffer[ibuf] == '\r' || buffer[ibuf] == ' ') {
  56.         if (ibuf == ibufsz) {
  57.             fillbuf();
  58.             if (ibufsz == 0) return 0;
  59.         } else ibuf++;
  60.     }
  61.     int i = 0;
  62.     while (true) {
  63.         if (ibuf == ibufsz) {
  64.             fillbuf();
  65.             if (ibufsz == 0) break;
  66.         }
  67.         char ch = buffer[ibuf++];
  68.         if (ch == '\r' || ch == '\n' || ch == EOF || (spacebrk && ch == ' ')) break;
  69.         d[i++] = ch;
  70.     }
  71.     d[i] = '\0';
  72.     return i;
  73. }
  74.  
  75. inline void fastwritestr(char * s) {
  76.     while (*s) putchar(*s++);
  77. }
  78.  
  79. char numbuf[20];
  80.  
  81. inline void fastwriteint(int no) {
  82.     numbuf[0] = '0';
  83.     int i = no == 0 ? 1 : 0;
  84.     bool neg = no < 0;
  85.     if (neg) no *= -1;
  86.     while (no) {
  87.         numbuf[i++] = no % 10 + 48;
  88.         no /= 10;
  89.     }
  90.     if (neg) putchar('-');
  91.     while (i > 0) putchar(numbuf[--i]);
  92. }
  93.  
  94. #define LSB(x) ((x) & -(x))
  95. #define foreach_fenwick_children(child, nd) \
  96.     for (int sibling = LEFT_SIBLING(nd), child = (nd) - 1; child > sibling; child -= LSB(child))
  97.    
  98. const int MAXN = 50001;
  99. const int LOGN = 16;
  100.  
  101. struct VALUE {
  102.     int best_prefix;
  103.     int best_suffix;
  104.     int best;
  105.     int sum;
  106. };
  107.  
  108. VALUE point[MAXN];
  109. VALUE tree[MAXN];
  110. int n_fenwick;
  111.  
  112. VALUE combine_value(const VALUE &a, const VALUE &b) {
  113.     int pref = max(a.best_prefix, a.sum + b.best_prefix);
  114.     int suf  = max(b.best_suffix, b.sum + a.best_suffix);
  115.     int sum  = a.sum + b.sum;
  116.  
  117.     int best = max(a.best, b.best);
  118.     best = max(best, a.best_suffix + b.best_prefix);
  119.  
  120.     return {pref, suf, best, sum};
  121. }
  122.  
  123. void combine(int nd) {
  124.     if (nd > n_fenwick) return;
  125.     tree[nd] = point[nd];
  126.     foreach_fenwick_children(chld, nd) {
  127.         tree[nd] = combine_value(tree[chld], tree[nd]);
  128.     }
  129. }
  130.  
  131. void update(int nd, int val) {
  132.     point[nd]={ val, val, val, val };
  133.     while (nd <= n_fenwick) {
  134.         combine(nd);
  135.         nd=PARENT(nd);
  136.     }
  137. }
  138.  
  139. VALUE range_query_fenwick(int l, int r) {
  140.     VALUE ans;
  141.     bool empty = true;
  142.  
  143.     while (l <= r) {
  144.         int nr = LEFT_SIBLING(r);
  145.         bool exceed = nr < l - 1;
  146. const VALUE &segment = exceed ? point[r] : tree[r];
  147. ans = empty ? segment : combine_value(segment, ans);
  148.             r = exceed ? r-1 : nr;
  149.         empty = false;
  150.     }
  151.     return ans;
  152. }
  153.  
  154. int main() {
  155.     int n;
  156.     n = readint();
  157.     n_fenwick = n;
  158.  
  159.     for (int i = 1; i <= n; ++i) {
  160.         int v = readint();
  161.         point[i] = {v, v, v, v};
  162.         combine(i);
  163.     }
  164.  
  165.     int q = readint();
  166.     while (q--) {
  167.         int t, a, b;
  168.         t = readint();
  169.         a = readint();
  170.         b = readint();
  171.         if (t == 0) {
  172.             update(a, b);
  173.             continue;
  174.         }
  175.         int res = range_query_fenwick(a, b).best;
  176.         fastwriteint(res);
  177.         putchar('\n');
  178.     }
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment