erfanul007

LOJ 1164 seg

Mar 1st, 2021
368
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int      ll;
  5.  
  6. #define Max 2000009
  7. #define MAX 2147483647
  8. #define read()             freopen("input.txt", "r", stdin)
  9. #define write()            freopen("output.txt", "w", stdout)
  10.  
  11. ll Tree[Max],lazy[Max];
  12.  
  13. void UpdateLazy(int node,int L,int R,int x,int y,ll val)
  14. {
  15.     int mid = (L+R)/2;
  16.     int Left = 2*node;
  17.     int Right = Left+1;
  18.     if(lazy[node]){
  19.         Tree[node]+=(R-L+1)*lazy[node];
  20.         if(L!=R){
  21.             lazy[Left]+=lazy[node];
  22.             lazy[Right]+=lazy[node];
  23.         }
  24.         lazy[node]=0;
  25.     }
  26.     if(L>y || R<x) return;
  27.     if(L>=x && R<=y){
  28.         Tree[node]+=(R-L+1)*val;
  29.         if(L!=R){
  30.             lazy[Left]+=val;
  31.             lazy[Right]+=val;
  32.         }
  33.         return;
  34.     }
  35.     UpdateLazy(Left,L,mid,x,y,val);
  36.     UpdateLazy(Right,mid+1,R,x,y,val);
  37.     Tree[node] = Tree[Left]+Tree[Right];
  38. }
  39.  
  40. ll RangeSum(int node,int L,int R,int x,int y)
  41. {
  42.     int mid = (L+R)/2;
  43.     int Left = 2*node;
  44.     int Right = Left+1;
  45.     if(lazy[node]){
  46.         Tree[node]+=(R-L+1)*lazy[node];
  47.         if(L!=R){
  48.             lazy[Left]+=lazy[node];
  49.             lazy[Right]+=lazy[node];
  50.         }
  51.         lazy[node]=0;
  52.     }
  53.     if(L>y || R<x) return 0;
  54.     if(L>=x && R<=y){
  55.         return Tree[node];
  56.     }
  57.     ll sum1 = RangeSum(Left,L,mid,x,y);
  58.     ll sum2 = RangeSum(Right,mid+1,R,x,y);
  59.     return sum1+sum2;
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.     //read();
  66.     //write();
  67.     int t;
  68.     scanf("%d",&t);
  69.     for(int j=1;j<=t;j++){
  70.         int n,q,k,x,y;
  71.         ll v;
  72.         scanf("%d %d",&n,&q);
  73.         memset(Tree,0,sizeof(Tree));
  74.         memset(lazy,0,sizeof(lazy));
  75.         printf("Case %d:\n",j);
  76.         for(int i=0;i<q;i++){
  77.             scanf("%d",&k);
  78.             if(!k){
  79.                 scanf("%d %d %lld",&x,&y,&v);
  80.                 UpdateLazy(1,1,n,x+1,y+1,v);
  81.             }
  82.             else{
  83.                 scanf("%d %d",&x,&y);
  84.                 printf("%lld\n",RangeSum(1,1,n,x+1,y+1));
  85.             }
  86.         }
  87.     }
  88.     return 0;
  89. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×