nicuvlad76

Untitled

Jan 14th, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <algorithm>
  2. #include <fstream>
  3. #define N (1<<18)
  4. using namespace std;
  5. ifstream fin("maxq.in");
  6. ofstream fout("maxq.out");
  7.  
  8. int n,m;
  9. int v[N];
  10. long long A[N<<1], B[N<<1], C[N<<1], Sum[N<<1];
  11.  
  12. #define lf (n<<1)
  13. #define rt (lf+1)
  14. #define md ((l+r)>>1)
  15.  
  16. void Build(int n, int l, int r)
  17. {
  18.     if (l==r)
  19.     {
  20.         A[n]=B[n]=C[n]=max(v[l],0);
  21.         Sum[n]=v[l];
  22.     }
  23.     else
  24.     {
  25.       Build(lf,l,md);
  26.       Build(rt, md+1, r);
  27.       A[n]=max(A[lf], Sum[lf]+A[rt]);
  28.       B[n]=max(B[lf]+Sum[rt],B[rt]);
  29.       C[n]=max(max(C[lf], C[rt]),B[lf]+A[rt]);
  30.       Sum[n]=Sum[lf]+Sum[rt];
  31.     }
  32. }
  33. int p,x;
  34. void Update(int n, int l, int r)
  35. {
  36.     if(l==r){
  37.         A[n]=B[n]=C[n]=max(x,0);
  38.         Sum[n]=x;
  39.     }
  40.     else{
  41.         if(p<=md)Update(lf,l,md);
  42.         else Update(rt,md+1,r);
  43.       A[n]=max(A[lf], Sum[lf]+A[rt]);
  44.       B[n]=max(B[lf]+Sum[rt],B[rt]);
  45.       C[n]=max(max(C[lf], C[rt]),B[lf]+A[rt]);
  46.       Sum[n]=Sum[lf]+Sum[rt];
  47.     }
  48. }
  49.  
  50. int a, b;
  51. long long sol, S;
  52. void Query(int n, int l, int r)
  53. {
  54.     if(a<=l && r<=b)
  55.     {
  56.         if(S<0) S=0;
  57.         sol=max(sol, max(S+A[n],C[n]));
  58.         S=max(S+Sum[n], B[n]);
  59.     }
  60.     else{
  61.         if(a<=md) Query(lf,l,md);
  62.         if(b>md) Query(rt,md+1,r);
  63.     }
  64. }
  65.  
  66. int main()
  67. {
  68.     int i,t, v1,v2;
  69.     fin>>n;
  70.     for(i=1;i<=n;i++)
  71.         fin>>v[i];
  72.     Build(1,1,n);
  73.     fin>>m;
  74.     while(m--)
  75.     {
  76.         fin>>t>>v1>>v2;
  77.         if(t==0)
  78.         {
  79.             p=v1+1;
  80.             x=v2;
  81.             Update(1,1,n);
  82.         }
  83.         else
  84.         {
  85.             S=sol=0;
  86.             a=v1+1;
  87.             b=v2+1;
  88.             Query(1,1,n);
  89.             fout<<sol<<"\n";
  90.         }
  91.     }
  92.     return(0);
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment