Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : Another Update-Query Problem
Source : toph.co
Category : Data Structure
Algorithm : Segment Tree, Lazy Propagation
Verdict : Accepted
- #include "bits/stdc++.h"
-
- using namespace std;
-
- #define FI freopen("in.txt", "r", stdin)
- #define FO freopen("out.txt", "w", stdout)
- #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
- #define REP(i, a, b) for (int i = a; i <= b; i++)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
- #define rev(a) reverse(All(a))
- #define sz(a) (int)a.size()
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- using namespace __gnu_pbds;
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
- #include "ext/rope"
- using namespace __gnu_cxx;
-
-
-
-
-
-
-
-
-
-
- #define trace1(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
- #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
- #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
- #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define popcount(mask) __builtin_popcount(mask) // count set bit
- #define popcountLL(mask) __builtin_popcountll(mask) // for long long
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 1e7
- #define mod 1000000007
-
- static const int maxn = 2e5 + 5;
- static const int logn = 21;
-
- struct segmentTree
- {
- ll lazy, sum1, sum2;
- segmentTree(ll lazy = 0, ll sum1 = 0, ll sum2 = 0) :
- lazy(lazy), sum1(sum1), sum2(sum2) {}
-
- inline void add(const segmentTree &p)
- {
- sum1 = (sum1 + p.sum1) % mod;
- sum2 = (sum2 + p.sum2) % mod;
- }
- } Tree[maxn << 2];
-
- inline ll getSum(ll n)
- {
- if (n <= 0) return 0;
- ll sum = (n * (n + 1)) % mod;
- sum = (sum * 500000004 * 1LL) % mod;
- return sum;
- }
-
- inline void updateLazy(int node, int a, int b, ll lazy)
- {
- ll man = ((b - a + 1) * lazy * 1LL) % mod;
- Tree[node].sum1 = (Tree[node].sum1 + man) % mod;
- ll val = (getSum(b) - getSum(a-1) + mod) % mod;
- val = (val * lazy * 1LL) % mod;
- Tree[node].sum2 = (Tree[node].sum2 + val) % mod;
- Tree[node].lazy = (Tree[node].lazy + lazy) % mod;
- }
-
- inline void updateNode(int node, int a, int b)
- {
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- ll lazy = Tree[node].lazy;
- ll man = ((mid - a + 1) * lazy * 1LL) % mod;
- Tree[lft].sum1 = (Tree[lft].sum1 + man) % mod;
- ll val = (getSum(mid) - getSum(a-1) + mod) % mod;
- val = (val * lazy * 1LL) % mod;
- Tree[lft].sum2 = (Tree[lft].sum2 + val) % mod;
- Tree[lft].lazy = (Tree[lft].lazy + lazy) % mod;
- man = ((b - mid) * lazy * 1LL) % mod;
- Tree[rgt].sum1 = (Tree[rgt].sum1 + man) % mod;
- val = (getSum(b) - getSum(mid) + mod) % mod;
- val = (val * lazy * 1LL) % mod;
- Tree[rgt].sum2 = (Tree[rgt].sum2 + val) % mod;
- Tree[rgt].lazy = (Tree[rgt].lazy + lazy) % mod;
-
- Tree[node].lazy = 0;
- }
-
- inline void marge(int node)
- {
- int lft = node << 1;
- int rgt = lft | 1;
- Tree[node].sum1 = (Tree[lft].sum1 + Tree[rgt].sum1) % mod;
- Tree[node].sum2 = (Tree[lft].sum2 + Tree[rgt].sum2) % mod;
- }
-
- ll arr[maxn];
-
- inline void build(int node, int a, int b)
- {
- if (a == b)
- {
- Tree[node].lazy = 0;
- Tree[node].sum1 = arr[a];
- Tree[node].sum2 = ((ll)a * arr[a]) % mod;
- return;
- }
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- build(lft, a, mid);
- build(rgt, mid+1, b);
- marge(node);
- }
-
- inline void update(int node, int a, int b, int i, int j, ll x)
- {
- if (a == i && b == j)
- {
- updateLazy(node, a, b, x);
- return;
- }
- if (Tree[node].lazy > 0)
- {
- updateNode(node, a, b);
- }
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- if (j <= mid)
- {
- update(lft, a, mid, i, j, x);
- }
- else if (i > mid)
- {
- update(rgt, mid+1, b, i, j, x);
- }
- else
- {
- update(lft, a, mid, i, mid, x);
- update(rgt, mid+1, b, mid+1, j, x);
- }
- marge(node);
- }
-
- inline segmentTree query(int node, int a, int b, int i, int j)
- {
- if (a == i && b == j)
- {
- return Tree[node];
- }
- if (Tree[node].lazy > 0)
- {
- updateNode(node, a, b);
- }
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- segmentTree res = segmentTree();
- if (j <= mid)
- {
- segmentTree p = query(lft, a, mid, i, j);
- res.add(p);
- }
- else if (i > mid)
- {
- segmentTree q = query(rgt, mid+1, b, i, j);
- res.add(q);
- }
- else
- {
- segmentTree p = query(lft, a, mid, i, mid);
- segmentTree q = query(rgt, mid+1, b, mid+1, j);
- res.add(p);
- res.add(q);
- }
- marge(node);
- return res;
- }
-
- int main()
- {
-
- int tc = read();
- FOR(tcase, tc)
- {
- int n = read();
- int q = read();
- FOR(i, n) arr[i] = readLL();
- build(1, 1, n);
- printf("Case %d:\n", tcase);
- while (q--)
- {
- int type = read();
- if (type == 1)
- {
- int l = read();
- int r = read();
- ll x = readLL();
- assert(l <= r);
- update(1, 1, n, l, r, x);
- }
- else
- {
- int l = read();
- int r = read();
- ll d = readLL();
- d %= mod;
- assert(l <= r);
- segmentTree res = query(1, 1, n, l, r);
- ll suma = res.sum1 % mod;
- assert(suma >= 0);
- ll bad = (l * suma * 1LL) % mod;
- ll bad2 = (res.sum2 - bad + mod) % mod;
- ll sumd = (d * bad2 * 1LL) % mod;
- assert(sumd >= 0);
- ll ans = (suma + sumd) % mod;
- printf("%lld\n", ans);
- }
- }
- FOR(i, n) arr[i] = 0;
- For(i, maxn*4) Tree[i] = segmentTree();
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.