Advertisement
Giatro

Caixas de Moedas

Mar 10th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef long long int ll;
  3. typedef unsigned long long int ull;
  4. typedef long double lld;
  5. #define fora(i, n) for (ll i = 0; i < n; i++)
  6. #define forai(i, n) for (ll i = 1; i <= n; i++)
  7. #define ford(i, n) for (ll i = n-1; i >= 0; i--)
  8. #define fordi(i, n) for (ll i = n; i >= 0; i--)
  9. #define forita(it, c) for(auto it = c.begin(); it != c.end(); it++)
  10. #define foritd(it, c) for(auto it = c.rbegin(); it != c.rend(); it++)
  11. #define f first
  12. #define s second
  13. #define sortvector(v) sort(v.begin(), v.end())
  14. #define sortvectorby(v, f) sort(v.begin(), v.end(), f)
  15. #define pb push_back
  16. #define mk make_pair
  17. #define pll pair<ll, ll>
  18. #define cout1(a) cout << (a)
  19. #define cout2(a, b) cout << (a) << " " << (b)
  20. #define cout3(a, b, c) cout << (a) << " " << (b) << " " << (c)
  21. #define cout1e(a) cout1(a) << endl
  22. #define cout2e(a, b) cout2(a, b) << endl
  23. #define cout3e(a, b, c) cout3(a, b, c) << endl
  24. #define get1(a) cin >> (a)
  25. #define get2(a, b) cin >> (a) >> (b)
  26. #define get3(a, b, c) cin >> (a) >> (b) >> (c)
  27. #define get4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
  28. #define INF LLONG_MAX
  29. #define MINF LLONG_MIN
  30. #define M 1000000007
  31.  
  32. using namespace std;
  33.  
  34. int n, q, k, a, b, o;
  35. int seg[412345], lz[412345], arr[112345];
  36.  
  37. void build(int l, int r, int i) {
  38.   if (l == r)
  39.     seg[i] = arr[l];
  40.   else {
  41.     int m = l + (r-l)/2;
  42.     build(l, m, 2*i);
  43.     build(m+1, r, 2*i+1);
  44.     seg[i] = seg[2*i] + seg[2*i+1];
  45.   }
  46.   lz[i] = 0;
  47. }
  48.  
  49. void push(int l, int r, int i) {
  50.   if (lz[i] != 0) {
  51.     seg[i] = lz[i]*(r-l+1);
  52.     if (l != r)
  53.       lz[2*i] = lz[2*i+1] = lz[i];
  54.     lz[i] = 0;
  55.   }
  56. }
  57.  
  58. void update(int l, int r, int i, int ql, int qr, int x) {
  59.   push(l, r, i);
  60.   if (ql > r || qr < l) return;
  61.   if (ql <= l && r <= qr) {
  62.     lz[i] = x; push(l, r, i);
  63.   } else {
  64.     int m = l + (r-l)/2;
  65.     update(l, m, 2*i, ql, qr, x);
  66.     update(m+1, r, 2*i+1, ql, qr, x);
  67.     seg[i] = seg[2*i] + seg[2*i+1];
  68.   }
  69. }
  70.  
  71. int query(int l, int r, int i, int ql, int qr) {
  72.   push(l, r, i);
  73.   if (r < ql || qr < l) return 0;
  74.   if (ql <= l && r <= qr) return seg[i];
  75.   int m = l + (r-l)/2;
  76.   return query(l, m, 2*i, ql, qr) + query(m+1, r, 2*i+1, ql, qr);
  77. }
  78.  
  79. int main(int argc, char const *argv[]) {
  80.   ios_base::sync_with_stdio(false);
  81.   cin.tie(NULL);
  82.  
  83.   get2(n, q);
  84.   forai (i, n) get1(arr[i]);
  85.   build(1, n, 1);
  86.  
  87.   // cout << endl;
  88.   // fora (i, 4*n) {
  89.   //   cout3e(i, seg[i], lz[i]);
  90.   // }
  91.   // cout << endl;
  92.  
  93.   fora (i, q) {
  94.     cin >> o;
  95.     if (o == 1) {
  96.       cin >> a >> b >> k;
  97.       update(1, n, 1, a, b, k);
  98.     } else {
  99.       cin >> a >> b;
  100.       cout << query(1, n, 1, a, b) << endl;
  101.     }
  102.  
  103.     // cout << endl;
  104.     // fora (i, 4*n) {
  105.     //   cout3e(i, seg[i], lz[i]);
  106.     // }
  107.     // cout << endl;
  108.   }
  109.  
  110.   return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement