Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 5e4;
- struct T
- {
- long long lazy, val;
- };
- T Tree[4*N+1];
- int n, m, q;
- void down(int id)
- {
- long long up = Tree[id].lazy;
- Tree[id*2].lazy += up;
- Tree[id*2].val += up;
- Tree[id*2 + 1].lazy += up;
- Tree[id*2 + 1].val += up;
- Tree[id].lazy = 0;
- }
- long long GetMax(int id, int TreeL, int TreeR, int l, int r)
- {
- if (l > r)
- {
- return 0;
- }
- if (l == TreeL && r == TreeR)
- {
- return Tree[id].val;
- }
- down(id);
- int mid = (TreeL + TreeR)/2;
- return max(GetMax(id * 2, TreeL, mid, l, min(mid, r)), GetMax(id * 2 + 1, mid + 1, TreeR, max(l, mid + 1), r));
- }
- void Update(int id, int TreeL, int TreeR, int l, int r, long long add)
- {
- if (l > r)
- {
- return ;
- }
- if (l == TreeL && r == TreeR)
- {
- Tree[id].lazy += add;
- Tree[id].val += add;
- return ;
- }
- down(id);
- int mid = (TreeL + TreeR)/2;
- Update(id * 2, TreeL, mid, l, min(mid, r), add);
- Update(id * 2 + 1, mid + 1, TreeR, max(l, mid + 1), r, add);
- Tree[id].val = max(Tree[id*2].val, Tree[id*2 + 1].val);
- }
- void read()
- {
- cin >> n >> m;
- int l, r, k;
- for (int i = 1; i <= m; ++ i)
- {
- cin >> l >> r >> k;
- Update(1, 1, n, l, r, k);
- }
- }
- void solve()
- {
- int l, r;
- cin >> q;
- while (q--)
- {
- cin >> l >> r;
- cout << GetMax(1, 1, n, l, r) << '\n';
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- read();
- solve();
- }
Add Comment
Please, Sign In to add comment