Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- using namespace std;
- #define NNN 100001
- #define ll long long
- int T, N, Q, s, p, q, v;
- ll bit1[NNN + 1], bit2[NNN + 1];
- void update(ll bit[], int i, ll val)
- {
- while (i <= NNN)
- {
- bit[i] += val;
- i += i & -i;
- }
- }
- ll query(ll bit[], int i)
- {
- ll sum = 0;
- while (i > 0)
- {
- sum += bit[i];
- i -= i & -i;
- }
- return sum;
- }
- int main()
- {
- scanf("%d", &T);
- while (T--)
- {
- scanf("%d %d", &N, &Q);
- memset(bit1, 0, sizeof(bit1));
- memset(bit2, 0, sizeof(bit2));
- while (Q--)
- {
- scanf("%d ", &s);
- if (s == 0)
- {
- scanf("%d %d %d", &p, &q, &v);
- update(bit1, p, v);
- update(bit1, q + 1, -v);
- update(bit2, q + 1, (ll)v * q);
- update(bit2, p, -(ll)v * (p - 1));
- }
- else
- {
- scanf("%d %d", &p, &q);
- ll ans = query(bit1, q) * q + query(bit2, q) -
- query(bit1, p - 1) * (p - 1) - query(bit2, p - 1);
- printf("%lld\n", ans);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement