Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- #include <tuple>
- #include <iomanip>
- #include <stdio.h>
- #include <map>
- #include <bitset>
- #include <set>
- #include <stack>
- #include <queue>
- #include <unordered_set>
- #include <cassert>
- #include <stdlib.h>
- #include <time.h>
- #include <random>
- //#pragma GCC optimize("Ofast,no-stack-protector")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
- //#pragma GCC optimize("unroll-loops")
- //#pragma GCC optimize("fast-math")
- //#pragma GCC optimize("section-anchors")
- //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
- //#pragma GCC optimize("vpt")
- //#pragma GCC optimize("rename-registers")
- //#pragma GCC optimize("move-loop-invariants")
- //#pragma GCC optimize("unswitch-loops")
- //#pragma GCC optimize("function-sections")
- //#pragma GCC optimize("data-sections")
- //#pragma GCC optimize("branch-target-load-optimize")
- //#pragma GCC optimize("branch-target-load-optimize2")
- //#pragma GCC optimize("btr-bb-exclusive")
- #define int long long
- #define ll long long
- #define ull unsigned long long
- #define all(a) a.begin(), a.end()
- #define pii pair<int, int>
- #define pb push_back
- #define ld long double
- using namespace std;
- const int INF = 1e17;
- //const int mod = 2600000069;
- //const int p = 179;
- const int MAXN = 300001;
- mt19937 my_rand(time(0));
- struct node {
- int x, y = my_rand(), sum = 0;
- node *l, *r;
- node() {
- x = -1;
- l = r = 0;
- }
- };
- typedef node* pnode;
- typedef pair<pnode, pnode> p;
- vector<node> a(MAXN);
- int ind = 0;
- pnode root = 0;
- pnode merge(pnode a, pnode b) {
- if (!a) return b;
- if (!b) return a;
- if (a->y > b->y) {
- a->r = merge(a->r, b);
- a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
- return a;
- } else {
- b->l = merge(a, b->l);
- b->sum = (b->r ? b->r->sum : 0) + (b->l ? b->l->sum : 0) + b->x;
- return b;
- }
- }
- void split(pnode a, int x, pnode& l, pnode& r) {
- if (!a) {
- l = r = 0;
- return;
- }
- if (a->x < x) {
- split(a->r, x, l, r);
- a->r = l;
- a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
- l = a;
- } else {
- split(a->l, x, l, r);
- a->l = r;
- a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
- r = a;
- }
- }
- bool check(pnode a, int x) {
- if (!a) return 0;
- if (a->x == x) return 1;
- if (a->x < x) return check(a->r, x);
- else return check(a->l, x);
- }
- void add(int x) {
- if (check(root, x)) return;
- p q;
- split(root, x, q.first, q.second);
- pnode t = &a[ind++];
- t->x = x;
- t->sum = x;
- // cout << "here\n";
- root = merge(q.first, merge(t, q.second));
- // cout << "after\n";
- }
- int last = 0;
- void sum(int l, int r) {
- if (!root) {last = 0; cout << "0\n"; return;}
- p q1;
- split(root, r + 1, q1.first, q1.second);
- if (!q1.first) {
- last = 0;
- cout << "0\n";
- root = merge(q1.first, q1.second);
- return;
- }
- p q2;
- split(q1.first, l, q2.first, q2.second);
- if (!q2.second) {
- last = 0;
- cout << "0\n";
- root = merge(merge(q2.first, q2.second), q1.second);
- return;
- }
- last = q2.second->sum;
- cout << q2.second->sum << "\n";
- root = merge(merge(q2.first, q2.second), q1.second);
- }
- const int mod = 1e9;
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n;
- cin >> n;
- char t;
- int x, x1;
- for (int i = 0; i < n; i++) {
- // for (int i = 0; i < ind; i++) {
- // cout << a[i].x << " " << a[i].y << "\n";
- // }
- cin >> t >> x;
- if (t == '+') {
- // cout << "last: " << last <<"\n";
- add((x + last) % mod);
- last = 0;
- } else {
- cin >> x1;
- sum(x, x1);
- }
- }
- }
- /*
- 4
- + 4
- + 7
- ? 5 6
- ? 3 5
- -> 0 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement