Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <algorithm>
- template <typename T>
- T func(T a, T b) {
- return a + b;
- }
- const int64_t INF = 1e15;
- class SegmentTree {
- public:
- SegmentTree(std::vector<int64_t>& values) : size(values.size()), t(4 * size, 0), values(values), c( 4 * size, 0) {
- Build(0, 0, size, values);
- }
- void ChangeVal(size_t pos, int64_t val) {
- ChangeValRec(0, 0, size, pos, val);
- }
- void ChangeSeg(size_t ask_l, size_t ask_r, int64_t val) {
- ChangeSegRec(0, 0, size, ask_l, ask_r + 1, val);
- }
- int64_t Ask(size_t ask_l, size_t ask_r) {
- return AskRec(0, 0, size, ask_l, ask_r + 1);
- }
- int64_t GetKth(size_t k) {
- if (Ask(0, size) < k) {
- return -INF;
- }
- return GetKthRec(0, 0, size, k);
- }
- int64_t GetKthOnSeg(size_t ask_l, size_t ask_r, size_t k) {
- int64_t previous = Ask(0, ask_l);
- int64_t prefix = Ask(0, ask_r + 1);
- if (prefix - previous < k) {
- return -INF;
- }
- return GetKth(previous + k);
- }
- int64_t GetPref(int64_t sum) {
- if (sum == 0) {
- return 0;
- }
- if (Ask(0, size) < sum) {
- return INF;
- }
- return GetPrefRec(0, 0, size, sum);
- }
- private:
- void Update(size_t v, int64_t l = 0, int64_t r = 0) {
- int64_t m = (r + l) / 2;
- t[v] = func(GetVal(2 * v + 1, l, m), GetVal(2 * v + 2, m, r));
- }
- void Push(size_t v) {
- c[2 * v + 1] += c[v];
- c[2 * v + 2] += c[v];
- c[v] = 0;
- }
- int64_t GetVal(size_t v, int64_t l, int64_t r) {
- return t[v] + c[v] * (r - l);
- }
- void Build(size_t v, size_t l, size_t r, std::vector<int64_t>& values) {
- if (r - l == 1) {
- t[v] = values[l];
- return;
- }
- size_t m = (l + r) / 2;
- Build(2 * v + 1, l, m, values);
- Build(2 * v + 2, m, r, values);
- Update(v);
- }
- void ChangeValRec(size_t v, size_t l, size_t r, size_t pos, int64_t val) {
- if (r - l == 1) {
- t[v] = val;
- return;
- }
- Push(v);
- size_t m = (l + r) / 2;
- if (pos < m) {
- ChangeValRec(2 * v + 1, l, m, val, pos);
- } else {
- ChangeValRec(2 * v + 2, m, r, val, pos);
- }
- Update(v, l, r);
- }
- void ChangeSegRec(size_t v, size_t l, size_t r, size_t ask_l, size_t ask_r, int64_t val) {
- if (ask_r <= l || r <= ask_l) {
- return;
- }
- if (ask_l <= l && r <= ask_r) {
- c[v] += val;
- return;
- }
- Push(v);
- size_t m = (r + l) / 2;
- ChangeSegRec(2 * v + 1, l, m, ask_l, ask_r, val);
- ChangeSegRec(2 * v + 2, m, r, ask_l, ask_r, val);
- Update(v, l, r);
- }
- int64_t AskRec(size_t v, size_t l, size_t r, size_t ask_l, size_t ask_r) {
- if (ask_r <= l || r <= ask_l) {
- return Neutral;
- }
- if (ask_l <= l && r <= ask_r) {
- return GetVal(v, l, r);
- }
- Push(v);
- size_t m = (l + r) / 2;
- int64_t ans = func(AskRec(2 * v + 1, l, m, ask_l, ask_r), AskRec(2 * v + 2, m, r, ask_l, ask_r));
- Update(v, l, r);
- return ans;
- }
- int64_t GetKthRec(size_t v, size_t l, size_t r, size_t k) {
- if (r - l == 1) {
- return values[l];
- }
- Push(v);
- size_t m = (r + l) / 2;
- int64_t ans = 0;
- if (t[2 * v + 1] < k) {
- ans = GetKthRec(2 * v + 2, m, r, k - t[2 * v + 1]);
- } else {
- ans = GetKthRec(2 * v + 1, l, m, k);
- }
- Update(v);
- return ans;
- }
- int64_t GetPrefRec(size_t v, size_t l, size_t r, int64_t sum) {
- if (r - l == 1) {
- return r;
- }
- size_t m = (r + l) / 2;
- if (GetVal(2 * v + 1, l, m) < sum) {
- return GetPrefRec(2 * v + 2, m, r, sum - t[2 * v + 1]);
- } else {
- return GetPrefRec(2 * v + 1, l, m, sum);
- }
- }
- size_t size;
- std::vector<int64_t> t;
- std::vector<int64_t> c;
- std::vector<int64_t> values;
- int64_t Neutral = 0;
- };
- struct Event {
- int64_t l = 0;
- int64_t r = 0;
- int64_t index = 0;
- bool operator<(Event& other) const {
- return r < other.r;
- }
- };
- std::istream& operator>>(std::istream& input, Event& e) {
- input >> e.l >> e.r;
- return input;
- }
- int main() {
- size_t n;
- std::cin >> n;
- std::vector<int64_t> values(n);
- for (auto& val : values) {
- std::cin >> val;
- }
- SegmentTree tree(values);
- size_t q;
- std::cin >> q;
- while (q--) {
- int64_t type;
- std::cin >> type;
- if (type == 1) {
- int64_t l, r;
- std::cin >> l >> r;
- int64_t ans = tree.Ask(l, r);
- if (ans != INF) {
- std::cout << ans << '\n';
- } else {
- std::cout << "NONE\n";
- }
- } else {
- int64_t pos, x;
- std::cin >> pos >> x;
- tree.ChangeVal(pos, x);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement