fork download
  1. #include <cmath>
  2. #include <cstring>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. struct list
  8. {
  9. long sum,bestsum,left,right;
  10. };
  11.  
  12. list* make_new_node(long s,long bs,long l,long r)
  13. {
  14. list* temp=new list();
  15. temp->sum=s;
  16. temp->bestsum=bs;
  17. temp->left=l;
  18. temp->right=r;
  19. return temp;
  20. }
  21.  
  22. void build(long arr[],list* tree[],int node,int low,int high)
  23. {
  24. if(low>high)
  25. return;
  26. if(low==high)
  27. {
  28. tree[node]=make_new_node(arr[low],arr[low],arr[low],arr[low]);
  29. return;
  30. }
  31.  
  32. build(arr,tree,2*node+1,low,(high+low)/2);
  33. build(arr,tree,2*node+2,(high+low)/2+1,high);
  34.  
  35. // tree[node]=NULL;
  36. long a=tree[2*node+1]->sum+tree[2*node+2]->sum;
  37. long b=max(max(tree[2*node+1]->bestsum,tree[2*node+2]->bestsum),tree[2*node+1]->right+tree[2*node+2]->left);
  38. long c=max(tree[2*node+1]->left,tree[2*node+1]->sum+tree[2*node+2]->left);
  39. long d=max(tree[2*node+1]->right+tree[2*node+2]->sum,tree[2*node+2]->right);
  40.  
  41. tree[node]=make_new_node(a,b,c,d);
  42. }
  43.  
  44. list* query(list* tree[],int node,int low,int high,int l,int r)
  45. {
  46. if(low>high || low>r || high<l)
  47. return make_new_node(-15008,-15008,-15008,-15008);
  48. if(low>=l && high<=r)
  49. return tree[node];
  50.  
  51. list *a=query(tree,2*node+1,low,(high+low)/2,l,r);
  52. list *b=query(tree,2*node+2,(high+low)/2+1,high,l,r);
  53.  
  54. long w=a->sum+b->sum;
  55. long x=max(max(a->bestsum,b->bestsum),a->right+b->left);
  56. long y=max(a->left,a->sum+b->left);
  57. long z=max(a->right+b->sum,b->right);
  58. tree[node]=make_new_node(w,x,y,z);
  59.  
  60. return tree[node];
  61. }
  62.  
  63. int main()
  64. {
  65. //ios_base::sync_with_stdio(false);
  66. int n;
  67. scanf("%d",&n);
  68. // cin>>n;
  69. long arr[n];
  70. list* tree[5*n];
  71. for(int i=0;i<n;i++)
  72. scanf("%ld",&arr[i]);
  73. // cin>>arr[i];
  74. build(arr,tree,0,0,n-1);
  75.  
  76. int q;
  77. scanf("%d",&q);
  78. for(int i=0;i<q;i++)
  79. {
  80. int l,r;
  81. scanf("%d%d",&l,&r);
  82. // cout<<query(tree,0,0,n-1,l-1,r-1)->bestsum<<endl;
  83. printf("%ld\n",query(tree,0,0,n-1,l-1,r-1)->bestsum);
  84. }
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008
-15008