Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #pragma GCC optimize("O3")
  3. #pragma GCC optimize("unroll-loops")
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. #include <ext/pb_ds/tree_policy.hpp>
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. #define GLHF ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
  9. #define f first
  10. #define s second
  11. #define eb emplace_back
  12. #define pb push_back
  13. #define ppb pop_back
  14. #define pf push_front
  15. #define ppf pop_front
  16. #define len(a) (int)a.size()
  17.  
  18. using namespace std;
  19. using namespace __gnu_pbds;
  20.  
  21.  
  22. typedef long long ll;
  23. typedef long double ld;
  24.  
  25. const ll MOD = 1e9 + 7;
  26. const int INF = 1e9;
  27. const int N = 2e5 + 2;
  28. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  29.  
  30. ll T1;
  31. void timer()
  32. {
  33.     ll T2(clock());
  34.     cout << "Exec time: " << (T2-T1) << "ms" << endl;
  35. }
  36.  
  37. template<class A, class B> ostream& operator << (ostream& out, const pair<A, B>& a)
  38. {
  39.     out << "(" << a.x << ", " << a.y << ")";
  40. }
  41.  
  42. template<class A> ostream& operator << (ostream& out, const vector<A>& a)
  43. {
  44.     out << "[";
  45.     for(auto it = a.begin(); it != a.end(); ++it)
  46.     {
  47.         if(it != a.begin()) out << ", ";
  48.         out << *it;
  49.     }
  50.     out << "]";
  51. }
  52.  
  53. template<class A> ostream& operator << (ostream& out, const set<A>& a)
  54. {
  55.     out << "{";
  56.     for(auto it = a.begin(); it != a.end(); ++it)
  57.     {
  58.         if(it != a.begin()) out << ", ";
  59.         out << *it;
  60.     }
  61.     out << "}";
  62. }
  63.  
  64. template<class A, class B> ostream& operator << (ostream& out, const map<A, B>& a)
  65. {
  66.     out << "{" << endl;
  67.     for(auto it = a.begin(); it != a.end(); ++it)
  68.     {
  69.         out << "   " << it->x << ": " << it->y;
  70.         if(it != a.end()) out << ", ";
  71.         out << endl;
  72.     }
  73.     out << "}";
  74. }
  75.  
  76. template<class A> istream& operator >> (istream& in, vector<A>& a)
  77. {
  78.     for(auto &e : a)
  79.         in >> e;
  80.     return in;
  81. }
  82.  
  83. template<class A, class B> istream& operator >> (istream& in, pair<A, B>& a)
  84. {
  85.     in >> a.x >> a.y;
  86.     return in;
  87. }
  88.  
  89. ll binpow(ll a, ll n)
  90. {
  91.     if(n == 0) return 1;
  92.     if(n % 2) return (binpow(a, n - 1) * a) % MOD;
  93.     else
  94.     {
  95.         ll b = binpow(a, n / 2);
  96.         return (b * b) % MOD;
  97.     }
  98. }
  99.  
  100. //GOOD LUCK BEWARE OF INDIAN HACKERS
  101.  
  102. ll t[4 * N], m[4 * N], k[4 * N];
  103. vector<int> a(N);
  104.  
  105. void push(int v, int tl, int tr)
  106. {
  107.     if(m[v] != -1)
  108.     {
  109.         m[v * 2] = m[v * 2 + 1] = m[v];
  110.         m[v] = -1;
  111.         int tm = (tr + tl) / 2;
  112.         t[v * 2] = (tm - tl + 1) * 1ll * m[v * 2];
  113.         t[v * 2 + 1] = (tr - tm) * 1ll * m[v * 2 + 1];
  114.     }
  115.     if(k[v] != -1)
  116.     {
  117.         k[v * 2] = k[v * 2 + 1] = k[v];
  118.         k[v] = -1;
  119.         int tm = (tl + tr) / 2;
  120.         t[v * 2] += (tm - tl + 1) * 1ll * k[v * 2];
  121.         t[v * 2 + 1] += (tr - tm) * 1ll * k[v * 2 + 1];
  122.     }
  123. }
  124.  
  125. void build(int v, int tl, int tr)
  126. {
  127.     if(tl == tr)
  128.     {
  129.         t[v] = a[tl];
  130.         m[v] = a[tl];
  131.         k[v] = 0;
  132.         return;
  133.     }
  134.     push(v, tl, tr);
  135.     int tm = (tl + tr) / 2;
  136.     build(v * 2, tl, tm);
  137.     build(v * 2 + 1, tm + 1, tr);
  138.     t[v] = t[v * 2] + t[v * 2 + 1];
  139. }
  140.  
  141. ll sum(int v, int tl, int tr, int l, int r)
  142. {
  143.     if(tl > r || tr < l)
  144.         return 0;
  145.     if(l == tl && r == tr)
  146.         return t[v];
  147.     push(v, tl, tr);
  148.     int tm = (tl + tr) / 2;
  149.     return sum(v * 2, tl, tm, l, min(r, tm)) +
  150.             sum(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r);
  151. }
  152.  
  153. void add(int v, int tl, int tr, int l, int r, int val)
  154. {
  155.     if(tl > r || tr < l)
  156.         return;
  157.     if(l == tl && r == tr)
  158.     {
  159.         k[v] = val;
  160.         m[v] = -1;
  161.         t[v] += (tr - tl + 1) * 1ll * val;
  162.         return;
  163.     }
  164.     push(v, tl, tr);
  165.     int tm = (tl + tr) / 2;
  166.     add(v * 2, tl, tm, l, min(tm, r), val);
  167.     add(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, val);
  168.     t[v] = t[v * 2] + t[v * 2 + 1];
  169. }
  170.  
  171. void upd(int v, int tl, int tr, int l, int r, int val)
  172. {
  173.     if(tl > r || tr < l)
  174.         return;
  175.     if(l == tl && r == tr)
  176.     {
  177.         m[v] = val;
  178.         k[v] = -1;
  179.         t[v] = (tr - tl + 1) * 1ll * val;
  180.         return;
  181.     }
  182.     push(v, tl, tr);
  183.     int tm = (tl + tr) / 2;
  184.     upd(v * 2, tl, tm, l, min(r, tm), val);
  185.     upd(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, val);
  186.     t[v] = t[v * 2] + t[v * 2 + 1];
  187. }
  188.  
  189. int main()
  190. {
  191.     GLHF;
  192.     int n, q;
  193.     cin >> n >> q;
  194.     for(int i = 1; i <= n; ++i)
  195.         cin >> a[i];
  196.     build(1, 1, n);
  197.     while(q--)
  198.     {
  199.         int i, x, y;
  200.         cin >> i >> x >> y;
  201.         if(i == 1)
  202.         {
  203.             int b;
  204.             cin >> b;
  205.             add(1, 1, n, x, y, b);
  206.         }
  207.         else if(i == 2)
  208.         {
  209.             int b;
  210.             cin >> b;
  211.             upd(1, 1, n, x, y, b);
  212.         }
  213.         else
  214.             cout << sum(1, 1, n, x, y) << endl;
  215.     }
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement