Advertisement
erfanul007

LOJ 1164

Mar 1st, 2021
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <iostream>
  3. // #include <cstdio>
  4. // #include <cstdlib>
  5. // #include <algorithm>
  6. // #include <cmath>
  7. // #include <vector>
  8. // #include <set>
  9. // #include <map>
  10. // #include <queue>
  11. // #include <ctime>
  12. // #include <cassert>
  13. // #include <complex>
  14. // #include <string>
  15. // #include <cstring>
  16. // #include <queue>
  17. // #include <bitset>
  18.  
  19. using namespace std;
  20.  
  21. // #pragma GCC optimize("Ofast,no-stack-protector")n
  22. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  23. // #pragma GCC optimize("unroll-loops")
  24.  
  25.  
  26. #define ll              long long int
  27. #define ull             unsigned long long int
  28. #define vi              vector< int >
  29. #define vll             vector< ll >
  30.  
  31. #define sc              scanf
  32. #define pf              printf
  33. #define cspf(i)         pf("Case %d:\n", i)
  34. #define spc             pf(" ")
  35. #define line            pf("\n")
  36.  
  37. #define ff              first
  38. #define ss              second
  39. #define mp              make_pair
  40. #define pb              push_back
  41. #define ppb             pop_back
  42. #define tp(v,j)         get<j>(v)
  43. #define Log(b,x)        (log(x)/log(b))
  44.  
  45. #define FOR(i,x,y)      for(int i = int(x); i < int(y); i++)
  46. #define ROF(i,x,y)      for(int i = int(x)-1; i >= int(y); i--)
  47. #define clr(arr,x)      memset(arr, x, sizeof arr)
  48. #define vout(v)         for(int w=0;w<(int)v.size();w++){if(w) spc; cout<<v[w];}
  49. #define all(v)          v.begin(), v.end()
  50. #define rall(v)         v.rbegin(), v.rend()
  51. #define unq(v)          sort(all(v)),(v).resize(unique(all(v))-v.begin())
  52. #define fastIO          ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
  53.  
  54. #define sc1(x)          sc("%d",&x)
  55. #define sc2(x,y)        sc("%d %d", &x, &y)
  56. #define sc3(x,y,z)      sc("%d %d %d", &x, &y, &z)
  57. #define scl1(x)         sc("%lld",&x);
  58. #define scl2(x,y)       sc("%lld %lld", &x, &y)
  59. #define scf1(x)         sc("%lf",&x)
  60. #define scf2(x,y)       sc("%lf %lf", &x, &y)
  61.  
  62. #define pf1(x)          pf("%d",x)
  63. #define pf2(x,y)        pf("%d %d", x, y)
  64. #define pf3(x,y,z)      pf("%d %d %d", x, y, z)
  65. #define pfl1(x)         pf("%lld",x)
  66. #define pfl2(x,y)       pf("%lld %lld",x,y)
  67.  
  68. #define MOD             1000000007
  69. #define MaxN            100001
  70. #define MAX             (ull)(1e19)
  71. #define inf             0x3f3f3f3f
  72. #define PI              acos(-1.0)  // 3.1415926535897932
  73. #define eps             1e-6
  74.  
  75. template <class T> inline T bigMod(T p,T e,T M){T ret=1; for(;e>0;e>>=1){ if(e&1) ret=(ret*p)%M; p=(p*p)%M;} return (T)ret;}
  76. template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
  77. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  78. template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  79. template <class T> inline T SQR(T a){return a*a;}
  80.  
  81. int dx[] = { 1,-1, 0, 0};                //graph moves
  82. int dy[] = { 0, 0, 1,-1};               //graph moves
  83.  
  84. struct sqrt_dcmp{
  85.     ll sum, lazy;
  86.     int st, en;
  87. }bucket[350];
  88.  
  89. int n, q, sq;
  90. ll a[MaxN];
  91.  
  92. void construct(){
  93.     sq = ceil(sqrt((double)n));
  94.     for(int i=0, j=0; i<n; i+=sq, j++){
  95.         bucket[j].st = i;
  96.         bucket[j].sum = 0;
  97.         bucket[j].lazy = 0;
  98.         int k=i-1;
  99.         while(k<i+sq-1 && k<n-1){
  100.             k++;
  101.             a[k]=0;
  102.         }
  103.         bucket[j].en = k;
  104.     }
  105. }
  106.  
  107. ll RangeSum(int st, int en){
  108.     int fst = st/sq;
  109.     int lst = en/sq;
  110.     ll sum=0;
  111.  
  112.     for(int i=bucket[fst].st; i<=bucket[fst].en; i++) a[i]+=bucket[fst].lazy;
  113.     bucket[fst].lazy=0;
  114.  
  115.     if(fst==lst){
  116.         for(int i=st; i<=en; i++) sum += a[i];
  117.         return sum;
  118.     }
  119.  
  120.     for(int i=st; i<=bucket[fst].en; i++) sum += a[i];
  121.  
  122.     for(int i=fst+1; i<lst; i++) sum += bucket[i].sum;
  123.  
  124.     for(int i=bucket[lst].st; i<=bucket[lst].en; i++) a[i]+=bucket[lst].lazy;
  125.     bucket[lst].lazy=0;
  126.  
  127.     for(int i=bucket[lst].st; i<=en; i++) sum += a[i];
  128.     return sum;
  129. }
  130.  
  131. void addval(int st, int en, int val){
  132.     int fst = st/sq;
  133.     int lst = en/sq;
  134.     for(int i=bucket[fst].st; i<=bucket[fst].en; i++) a[i]+=bucket[fst].lazy;
  135.     bucket[fst].lazy=0;
  136.  
  137.     if(fst == lst){
  138.         for(int i=st; i<=en; i++) a[i] += val;
  139.         bucket[fst].sum = 0;
  140.         for(int i=bucket[fst].st; i<=bucket[fst].en; i++) bucket[fst].sum += a[i];
  141.         return;
  142.     }
  143.  
  144.     for(int i=st; i<=bucket[fst].en; i++) a[i]+=val;
  145.  
  146.     bucket[fst].sum = 0;
  147.     for(int i=bucket[fst].st; i<=bucket[fst].en; i++) bucket[fst].sum += a[i];
  148.  
  149.     for(int i=fst+1; i<lst; i++){
  150.         bucket[i].lazy += val;
  151.         bucket[i].sum += (1LL*(bucket[i].en-bucket[i].st+1)*val);
  152.     }
  153.  
  154.     for(int i=bucket[lst].st; i<=bucket[lst].en; i++) a[i]+=bucket[lst].lazy;
  155.     bucket[lst].lazy=0;
  156.  
  157.     for(int i=bucket[lst].st; i<=en; i++) a[i]+=val;
  158.  
  159.     bucket[lst].sum = 0;
  160.     for(int i=bucket[lst].st; i<=bucket[lst].en; i++) bucket[lst].sum += a[i];
  161. }
  162.  
  163. int main(){
  164.  
  165.     #ifndef ONLINE_JUDGE
  166.         clock_t tStart = clock();
  167.         freopen("input.txt", "r", stdin);
  168.         freopen("output.txt", "w", stdout);
  169.     #endif
  170.  
  171.     int t, ca=0; sc1(t);
  172.  
  173.     while(t--){
  174.         sc2(n, q);
  175.         // FOR(i,0,n) sc1(a[i]);
  176.         construct();
  177.         cspf(++ca);
  178.         FOR(i,0,q){
  179.             int x, st, en; sc3(x, st, en);
  180.             // st--, en--;
  181.             if(x==0){
  182.                 int val; sc1(val);
  183.                 addval(st, en, val);
  184.             }
  185.             else{
  186.                 pfl1(RangeSum(st, en)); line;
  187.             }
  188.         }
  189.     }
  190.    
  191.     #ifndef ONLINE_JUDGE
  192.         fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
  193.     #endif
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement