Advertisement
tiom4eg

day 4, C 100

Jan 12th, 2023
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. #define ook order_of_key
  37. #define fbo find_by_order
  38. using namespace __gnu_pbds;
  39. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  40. using namespace std;
  41. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  42. #define int long long
  43. const int INF = 1e9 + 7, MD = 998244353, MAX = 505, R = 1 << 19, MOD = 1e9 + 7, MOD2 = 1e9 + 9, MOD12 = MOD * MOD, MOD22 = MOD2 * MOD2, LG = 20, B1 = 300007, B2 = 107, S = 10008;
  44. const ll INFLL = 1e18 + 7;
  45.  
  46. int sum[LG], sum2[LG];
  47.  
  48. struct segtree {
  49.     struct node { int s, si, sii, d; };
  50.     vector <node> t;
  51.     void merge(int v) {
  52.         int l = R >> __lg(2 * v);
  53.         t[v].s = (t[2 * v].s + t[2 * v + 1].s) % MOD;
  54.         t[v].si = (t[2 * v].si + t[2 * v + 1].si + l * t[2 * v + 1].s) % MOD;
  55.         t[v].sii = (t[2 * v].sii + t[2 * v + 1].sii + 2 * l * t[2 * v + 1].si + ((l * l) % MOD) * t[2 * v + 1].s);
  56.     }
  57.     void tag(int v, int x, int l) { t[v].s = x * l % MOD, t[v].si = x * sum[__lg(l)] % MOD, t[v].sii = x * sum2[__lg(l)] % MOD, t[v].d = x; }
  58.     void push(int v, int l) {
  59.         if (l == 1 || !t[v].d) return;
  60.         tag(2 * v, t[v].d, l / 2), tag(2 * v + 1, t[v].d, l / 2);
  61.         t[v].d = 0;
  62.     }
  63.     void build(vi& a) {
  64.         t.resize(2 * R);
  65.         FOR(i, 0, sz(a)) tag(R + i, a[i], 1);
  66.         RFOR(i, R - 1, 1) merge(i);
  67.     }
  68.     void upd(int ql, int qr, int x, int v = 1, int nl = 0, int nr = R) {
  69.         push(v, nr - nl);
  70.         if (qr <= nl || nr <= ql) return;
  71.         if (ql <= nl && nr <= qr) {
  72.             tag(v, x, nr - nl), push(v, nr - nl);
  73.             return;
  74.         }
  75.         int nm = (nl + nr) / 2;
  76.         upd(ql, qr, x, 2 * v, nl, nm), upd(ql, qr, x, 2 * v + 1, nm, nr);
  77.         merge(v);
  78.     }
  79.     int get(int ql, int qr, int v = 1, int nl = 0, int nr = R) {
  80.         push(v, nr - nl);
  81.         if (qr <= nl || nr <= ql) return 0;
  82.         if (ql <= nl && nr <= qr) return (t[v].sii + 2 * (nl - ql) * t[v].si + ((nl - ql) * (nl - ql) % MOD) * t[v].s) % MOD;
  83.         int nm = (nl + nr) / 2;
  84.         return (get(ql, qr, 2 * v, nl, nm) + get(ql, qr, 2 * v + 1, nm, nr)) % MOD;
  85.     }
  86. } t;
  87.  
  88. signed main() {
  89.     fastIO;
  90.     int n, q; cin >> n >> q;
  91.     vi a(n); EACH(e, a) cin >> e;
  92.     t.build(a);
  93.     while (q--) {
  94.         int o, l, r; cin >> o >> l >> r, --l;
  95.         if (o == 1) {
  96.             int x; cin >> x;
  97.             t.upd(l, r, x);
  98.         }
  99.         else cout << t.get(l, r) << endl;
  100.     }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement