Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CC.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <iomanip>
- #include <string>
- #include <fstream>
- #include <string>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef string str;
- class Do {
- public:
- ll x, l, r, Add;
- Do *L, *R;
- Do()
- {
- x = 0;
- Add = 0;
- l = 0;
- r = 0;
- L = NULL;
- R = L;
- }
- friend Do *push(Do *);
- friend Do *build(Do *, ll, ll);
- friend void rec(Do *);
- friend ll query(Do *, ll, ll);
- friend Do *modify(Do *, ll, ll);
- friend Do *modify(Do *, ll, ll, ll);
- };
- ll a[100000];
- Do *push(Do *);
- Do *build(Do *, ll, ll);
- void rec(Do *);
- ll query(Do *, ll, ll);
- Do *modify(Do *, ll, ll);
- Do *modify(Do *, ll, ll, ll);
- int main()
- {
- Do *t = new Do();
- //После ввода массива a размером n
- t = build(t, 0, n - 1);
- system("pause");
- return 0;
- }
- Do *push(Do *t)
- {
- if (t->Add)
- {
- t->x += t->Add * (t->r - t->l + 1);
- if (t->l != t->r)
- {
- t->L->Add = t->Add;
- t->R->Add = t->Add;
- }
- t->Add = 0;
- }
- return t;
- }
- Do *build(Do *t, ll l, ll r)
- {
- if (l == r)
- {
- t->x = a[l];
- t->l = l;
- t->r = r;
- return t;
- }
- ll m = (r + l) / 2;
- t->L = new Do();
- t->R = new Do();
- t->l = l;
- t->r = r;
- t->L = build(t->L, l, m);
- t->R = build(t->R, m + 1, r);
- t->x = t->L->x + t->R->x;
- return t;
- }
- void rec(Do *t)
- {
- push(t);
- if (t->l == t->r)
- {
- return;
- }
- rec(t->L);
- rec(t->R);
- }
- ll query(Do *t, ll l, ll r)
- {
- push(t);
- if (r < t->l || t->r < l)
- return 0;
- if (l <= t->l && t->r <= r)
- return t->x;
- return query(t->L, l, r) + query(t->R, l, r);
- }
- Do *modify(Do *t, ll pos, ll val)
- {
- push(t);
- if (t->l == t->r)
- {
- t->x += val;
- return t;
- }
- if (t->L->r <= pos)
- t->L = modify(t->L, pos, val);
- else
- t->R = modify(t->R, pos, val);
- t->x = t->L->x + t->R->x;
- return t;
- }
- Do *modify(Do *t, ll l, ll r, ll val)
- {
- if (r < t->l || t->r < l)
- return t;
- if (l <= t->l && t->r <= r)
- {
- t->Add = val;
- push(t);
- return t;
- }
- modify(t->L, l, r, val);
- modify(t->R, l, r, val);
- t->x = t->L->x + t->R->x;
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement