text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 6; long long sum[MAX_N << 2][5]; long long a[MAX_N], b[MAX_N], num[MAX_N << 2]; int q[MAX_N]; char ch[10]; void up(int rt) { num[rt] = num[rt << 1] + num[rt << 1 | 1]; for (int i = 0; i < 5; ++i) { sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][((i - num[rt << 1] % 5 + 5) % 5)]; } } void update(int rt, int l, int r, int x, int key) { if (l == r) { sum[rt][1] = x ? 0 : b[key]; num[rt] = !x; return; } int mid = (l + r) >> 1; if (key <= mid) update(rt << 1, l, mid, x, key); else update(rt << 1 | 1, mid + 1, r, x, key); up(rt); } int init(int m) { int top = 0; for (int i = 1; i <= m; i++) { scanf("%s", ch); if (ch[0] == 'a') { q[i] = 0; scanf("%lld", &a[i]); b[++top] = a[i]; } else if (ch[0] == 'd') { q[i] = 1; scanf("%lld", &a[i]); } else { q[i] = 2; } } sort(b + 1, b + 1 + top); memset(num, 0, sizeof(num)); memset(sum, 0, sizeof(sum)); return top; } int main() { int key, n, m; while (~scanf("%d", &m)) { n = init(m); for (int i = 1; i <= m; ++i) { if (q[i] == 0) { int xb = lower_bound(b + 1, b + 1 + n, a[i]) - b; update(1, 1, n, 0, xb); } else if (q[i] == 1) { int xb = lower_bound(b + 1, b + 1 + n, a[i]) - b; update(1, 1, n, 1, xb); } else printf("%lld\n", sum[1][3]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int MAXN = 1e5 + 10; int q, m, lis[MAXN]; long long tmp[5]; char opt[5]; struct Node { int l, r, s; long long x[5]; int laz; } node[MAXN * 4]; struct Ques { int o, x; } ques[MAXN]; void build(int rt, int l, int r) { node[rt].l = l, node[rt].r = r; if (l == r) return; int mid = ((l + r) >> 1); build((rt << 1), l, mid); build((rt << 1 | 1), mid + 1, r); } inline void change(int rt, int o) { for (int i = 0; i < 5; i++) tmp[i] = node[rt].x[i]; for (int i = 0; i < 5; i++) node[rt].x[i] = tmp[((i - o) % 5 + 5) % 5]; node[rt].laz = (node[rt].laz + o) % 5; } inline void down(int rt) { if (node[rt].laz) { change((rt << 1), node[rt].laz); change((rt << 1 | 1), node[rt].laz); node[rt].laz = 0; } } inline void up(int rt) { node[rt].s = node[(rt << 1)].s + node[(rt << 1 | 1)].s; for (int i = 0; i < 5; i++) node[rt].x[i] = node[(rt << 1)].x[i] + node[(rt << 1 | 1)].x[i]; } void update(int rt, int p, int d, int o) { if (node[rt].l == node[rt].r) { if (o == 1) { node[rt].x[d] = lis[p]; node[rt].s = 1; } else { node[rt].x[0] = node[rt].x[1] = node[rt].x[2] = 0; node[rt].x[3] = node[rt].x[4] = 0; node[rt].s = 0; } return; } down(rt); int mid = ((node[rt].l + node[rt].r) >> 1); if (p <= mid) { update((rt << 1), p, d, o); change((rt << 1 | 1), o); } else update((rt << 1 | 1), p, d, o); up(rt); } long long query(int rt, int l, int r, int o) { if (node[rt].l == l && node[rt].r == r) { if (o) return node[rt].s; else return node[rt].x[3]; } down(rt); int mid = ((node[rt].l + node[rt].r) >> 1); if (r <= mid) return query((rt << 1), l, r, o); else if (l > mid) return query((rt << 1 | 1), l, r, o); else return query((rt << 1), l, mid, o) + query((rt << 1 | 1), mid + 1, r, o); } int main() { cin >> q; for (int i = 1; i <= q; i++) { scanf("%s", opt); if (opt[0] == 'a') { ques[i].o = 1; scanf("%d", &ques[i].x); lis[++m] = ques[i].x; } else if (opt[0] == 's') ques[i].o = 2; else { ques[i].o = 3; scanf("%d", &ques[i].x); } } if (q == 1) { if (ques[1].o == 2) printf("0\n"); return 0; } sort(lis + 1, lis + 1 + m); m = unique(lis + 1, lis + 1 + m) - lis - 1; build(1, 1, m); for (int i = 1; i <= q; i++) { if (ques[i].o == 1) { int p = lower_bound(lis + 1, lis + 1 + m, ques[i].x) - lis; int d = query(1, 1, p, 1) + 1; update(1, p, d % 5, 1); } else if (ques[i].o == 2) { cout << query(1, 1, m, 0) << endl; } else { int p = lower_bound(lis + 1, lis + 1 + m, ques[i].x) - lis; update(1, p, 233, -1); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v; int n; int main() { cin >> n; v.reserve(n); for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == "sum") { int l = v.size(); long long sum = 0; for (int i = 2; i < l; i += 5) { sum += v[i]; } cout << sum << endl; } else { int x; cin >> x; if (s == "add") { v.insert(lower_bound(v.begin(), v.end(), x), x); } else { v.erase(lower_bound(v.begin(), v.end(), x)); } } } }
#include <bits/stdc++.h> using namespace std; vector<int> num; int n; int main() { ios_base::sync_with_stdio(false); string s; int x; cin >> n; long long ans = 0; for (int i = 0; i < n; i++) { cin >> s; if (s == "del" || s == "add") { cin >> x; int pos = lower_bound(num.begin(), num.end(), x) - num.begin(); if (s == "del") num.erase(num.begin() + pos); else num.insert(num.begin() + pos, x); } else { ans = 0; for (int i = 2; i < num.size(); i += 5) ans += num[i]; cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; struct node { long long sum[5]; int cnt; } nod[maxn << 2]; struct Node { int op; int v; } p[maxn]; char cmd[5]; int a[maxn]; int cnt; int get(int x) { int l = 0, r = cnt - 1, m; while (l <= r) { m = l + r >> 1; if (a[m] == x) return m + 1; else if (a[m] > x) { r = m - 1; } else l = m + 1; } } void pushup(int o) { int lson = o << 1; int rson = o << 1 | 1; nod[o].cnt = nod[lson].cnt + nod[rson].cnt; for (int i = 0; i < 5; ++i) { nod[o].sum[i] = nod[lson].sum[i]; } for (int i = 0; i < 5; ++i) { if (i != 0) { nod[o].sum[(i + nod[lson].cnt) % 5] += nod[rson].sum[i]; } else { nod[o].sum[(nod[lson].cnt + 5) % 5] += nod[rson].sum[i]; } } } void update(int pos, int c, int l, int r, int o) { if (l == r) { if (c == 0) { memset(nod[o].sum, 0, sizeof(nod[o].sum)); nod[o].cnt = 0; } else { nod[o].cnt = 1; memset(nod[o].sum, 0, sizeof(nod[o].sum)); nod[o].sum[1] = a[pos - 1]; } return; } int m = l + r >> 1; if (m >= pos) { update(pos, c, l, m, o << 1); } else { update(pos, c, m + 1, r, o << 1 | 1); } pushup(o); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", cmd); if (cmd[0] == 'a') { p[i].op = 0; scanf("%d", &p[i].v); a[cnt++] = p[i].v; } else if (cmd[0] == 'd') { p[i].op = 1; scanf("%d", &p[i].v); a[cnt++] = p[i].v; } else { p[i].op = 2; } } sort(a, a + cnt); cnt = unique(a, a + cnt) - a; memset(nod, 0, sizeof nod); for (int i = 0; i < n; ++i) { if (p[i].op == 2) { printf("%I64d\n", nod[1].sum[3]); } else if (p[i].op == 0) { update(get(p[i].v), 1, 1, n, 1); } else { update(get(p[i].v), 0, 1, n, 1); } } return 0; }
#include <bits/stdc++.h> using namespace std; int cnt[1010000], n, size, excnt, ex[1010000], x[1010000], y[1010000], op[1010000], i, id, bas = (1 << 17); long long a[1010000][6]; char ch[1010000]; void init() { sort(ex + 1, ex + 1 + excnt); size = unique(ex + 1, ex + 1 + excnt) - ex - 1; for (i = 1; i <= n; i++) y[i] = lower_bound(ex + 1, ex + 1 + size, x[i]) - ex; } void pushup(int rt) { int ls = rt << 1, rs = ls + 1; for (int i = 0; i <= 4; i++) a[rt][i] = a[ls][i]; for (int i = 0; i <= 4; i++) a[rt][(i + cnt[ls]) % 5] += a[rs][i]; cnt[rt] = cnt[ls] + cnt[rs]; } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", ch); if (ch[0] == 's') op[i] = 2; else { scanf("%d", &x[i]); ex[++excnt] = x[i]; op[i] = ch[0] == 'a' ? 0 : 1; } } init(); for (i = 1; i <= n; i++) { if (op[i] == 2) printf("%I64d\n", a[1][3]); else { id = bas + y[i]; a[id][1] = (1 - op[i]) * x[i]; cnt[id] = (1 - op[i]); for (id /= 2; id >= 1; id /= 2) pushup(id); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 9; int n, sq, cnt[320]; bool freq[MAX]; long long sum[320][5], temp[5]; vector<pair<int, int> > q; vector<int> v; map<int, int> decode; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s[0] == 's') { q.push_back({2, -1}); } else { int x; cin >> x; int type = (s[0] == 'a' ? 0 : 1); q.push_back({type, x}); v.push_back(x); } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (int i = 0; i < v.size(); ++i) { decode[i] = v[i]; } sq = (int)sqrt(n + .0) + 1; int lst = v.size() / sq; for (int i = 0; i < n; ++i) { int type = q[i].first; if (type == 2) { long long ans = 0; for (int j = 0; j <= lst; ++j) { ans += sum[j][2]; } cout << ans << "\n"; } else if (type == 0) { int cntPrv = 0, x = q[i].second, pos = lower_bound(v.begin(), v.end(), x) - v.begin(), bucket = pos / sq; for (int j = 0; j < bucket; ++j) { cntPrv += cnt[j]; } for (int j = 0; j < 5; ++j) { sum[bucket][j] = 0; } ++cnt[bucket]; freq[pos] = 1; int index = cntPrv % 5; for (int j = bucket * sq, en = (bucket + 1) * sq - 1; j <= en; ++j) { if (freq[j]) { sum[bucket][index] += decode[j]; ++index; index %= 5; } } for (int j = bucket + 1; j <= lst; ++j) { for (int k = 0; k < 5; ++k) { temp[k] = sum[j][k]; } for (int k = 0; k < 5; ++k) { sum[j][k] = temp[(k - 1 + 5) % 5]; } } } else { int cntPrv = 0, x = q[i].second, pos = lower_bound(v.begin(), v.end(), x) - v.begin(), bucket = pos / sq; for (int j = 0; j < bucket; ++j) { cntPrv += cnt[j]; } for (int j = 0; j < 5; ++j) { sum[bucket][j] = 0; } --cnt[bucket]; freq[pos] = 0; int index = cntPrv % 5; for (int j = bucket * sq, en = (bucket + 1) * sq - 1; j <= en; ++j) { if (freq[j]) { sum[bucket][index] += decode[j]; ++index; index %= 5; } } for (int j = bucket + 1; j <= lst; ++j) { for (int k = 0; k < 5; ++k) { temp[k] = sum[j][k]; } for (int k = 0; k < 5; ++k) { sum[j][k] = temp[(k + 1) % 5]; } } } } return 0; }
#include <bits/stdc++.h> using namespace std; map<long long, int> map1; map<long long, int> map2; long long a[200010]; struct Node { int left, right, cnt; long long s[5]; } node[200010 * 10]; void Build(int l, int r, int p) { node[p].left = l; node[p].right = r; node[p].cnt = 0; for (int i = 0; i < 5; i++) node[p].s[i] = 0; if (l == r) { map1[a[l]] = p; return; } int mid = (l + r) >> 1; Build(l, mid, p << 1); Build(mid + 1, r, p << 1 | 1); } void Add(long long x) { int tt = map1[x]; node[tt].cnt = 1; node[tt].s[0] = x; tt >>= 1; while (tt) { node[tt].cnt++; int tmp = node[tt << 1].cnt; for (int i = 0; i < 5; i++) node[tt].s[i] = node[tt << 1].s[i]; for (int i = 0; i < 5; i++) node[tt].s[(tmp + i) % 5] += node[tt << 1 | 1].s[i]; tt >>= 1; } } void Del(long long x) { int tt = map1[x]; node[tt].cnt = 0; for (int i = 0; i < 5; i++) node[tt].s[i] = 0; tt >>= 1; while (tt) { if (node[tt].cnt > 0) node[tt].cnt--; int tmp = node[tt << 1].cnt; for (int i = 0; i < 5; i++) node[tt].s[i] = node[tt << 1].s[i]; for (int i = 0; i < 5; i++) node[tt].s[(tmp + i) % 5] += node[tt << 1 | 1].s[i]; tt >>= 1; } } struct Data { int id; long long x; } data[200010 * 5]; char str[10]; string s; int main() { string s; int n, m, i, j; while (~scanf("%d", &n)) { m = 0; for (i = 1; i <= n; i++) { scanf("%s", &str); if (strcmp(str, "add") == 0) { scanf("%I64d", &data[i].x); data[i].id = 0; a[++m] = data[i].x; } else if (strcmp(str, "del") == 0) { scanf("%I64d", &data[i].x); data[i].id = 1; } else data[i].id = 2; } a[++m] = 0; sort(a + 1, a + m + 1); map1.clear(); Build(1, m, 1); map2.clear(); Add(0); map2[0] = 1; for (i = 1; i <= n; i++) { if (data[i].id == 0) { if (map2[data[i].x] == 0) { map2[data[i].x] = 1; Add(data[i].x); } } else if (data[i].id == 1) { if (map2[data[i].x] == 1) { map2[data[i].x] = 0; Del(data[i].x); } } else printf("%I64d\n", node[1].s[3]); } } return 0; }
#include <bits/stdc++.h> using namespace std; int A[1 << 17]; int X[1 << 17]; int RMQ[2 << 17]; long long Sum[8][2 << 17]; int I[32]; void Add(int a, int b) { int i; for (i = 0; i < 18; i++) RMQ[I[i] + (a >> i)] += b; } void Update(int a) { Sum[0][a] = (RMQ[a] == 0 ? 0 : X[a]); int i, j; for (i = 1; i < 18; i++) { int pos = a >> i; int left = pos << 1; int right = left + 1; int cnt = RMQ[I[i - 1] + left] % 5; for (j = 0; j < 5; j++) Sum[j][I[i] + pos] = Sum[j][I[i - 1] + left] + Sum[(j + 5 - cnt) % 5][I[i - 1] + right]; } } int main() { int N; scanf("%d", &N); int i; int cnt = 0; for (i = 0; i < N; i++) { char buf[8]; scanf("%s", buf); if (buf[0] == 's') A[i] = 0; else { scanf("%d", &A[i]); X[cnt] = A[i]; ++cnt; if (buf[0] == 'd') A[i] = -A[i]; } } sort(X, X + cnt); cnt = unique(X, X + cnt) - X; memset(RMQ, 0, sizeof(RMQ)); memset(Sum, 0, sizeof(Sum)); I[0] = 0; for (i = 1; i < 18; i++) I[i] = I[i - 1] + (1 << (18 - i)); for (i = 0; i < N; i++) if (A[i] < 0) { int pos = lower_bound(X, X + cnt, -A[i]) - X; Add(pos, -1); Update(pos); } else if (A[i] > 0) { int pos = lower_bound(X, X + cnt, A[i]) - X; Add(pos, 1); Update(pos); } else printf("%lld\n", Sum[2][I[17]]); return 0; };
#include <bits/stdc++.h> using namespace std; const int N = 1 << 17, D = 2 * N + 10; int n, a[N], b[N], c[N], s[D], te[N]; long long t[D][5]; void update(int i, int x) { i += N - 1; if (x) s[i] = 1, t[i][0] = x; else s[i] = t[i][0] = 0; for (i >>= 1; i; i >>= 1) { s[i] = s[i << 1] + s[i << 1 | 1]; for (int j = 0; j < 5; j++) { int k = (s[i << 1] + j) % 5; t[i][k] = t[i << 1][k] + t[i << 1 | 1][j]; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { char es[10]; scanf("%s", es); if (es[0] != 's') { scanf("%d", &a[i]); b[i] = a[i]; if (es[0] == 'd') te[i] = 1; } else te[i] = 2; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) c[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; for (int i = 1; i <= n; i++) if (!te[i]) update(c[i], a[i]); else if (te[i] == 1) update(c[i], 0); else printf("%lld\n", t[1][2]); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> vec; int n; string Coms[] = {"add", "del", "sum"}; bool flag = true; int main() { scanf("%d", &n); string com; int k; long long ans = 0; for (int i = 0; i < n; i++) { cin >> com; if (com == Coms[0]) { scanf("%d", &k); flag = false; vec.insert(lower_bound(vec.begin(), vec.end(), k), k); } else if (com == Coms[1]) { scanf("%d", &k); flag = false; vec.erase(lower_bound(vec.begin(), vec.end(), k)); } else { if (flag) printf("%lld\n", ans); else { ans = 0; for (int j = 2; j < (int)vec.size(); j += 5) ans += vec[j]; printf("%lld\n", ans); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a; for (int i = 0; i < n; i++) { string s; cin >> s; if (s[0] == 's') { long long ret = 0; for (int i = 2; i < a.size(); i += 5) ret += a[i]; cout << ret << endl; } else { int x; cin >> x; if (s[0] == 'a') a.insert(lower_bound(a.begin(), a.end(), x), x); else a.erase(lower_bound(a.begin(), a.end(), x)); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Treap { struct tree { int x, y, si; long long sum[5]; tree *l, *r; }; tree mem[100100]; int mpos; tree *nil, *root; Treap() { mpos = 1; nil = mem; root = nil; nil->l = nil->r = nil; } void Calc(tree *t) { if (t == nil) return; t->si = t->l->si + 1 + t->r->si; for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[i]; int sh = t->l->si; t->sum[sh % 5] += t->x; sh++; for (int i = 0; i < 5; i++) t->sum[(i + sh) % 5] += t->r->sum[i]; } tree *nn(int x) { tree *t = mem + mpos++; t->y = (rand() << 15) + rand(); t->x = x; t->l = t->r = nil; Calc(t); return t; } void split(tree *t, tree **l, tree **r, int x) { if (t == nil) *l = *r = nil; else if (t->x < x) *l = t, split(t->r, &t->r, r, x); else *r = t, split(t->l, l, &t->l, x); Calc(*l), Calc(*r); } void merge(tree **t, tree *l, tree *r) { if (l == nil || r == nil) { *t = max(l, r); return; } if (l->y < r->y) *t = l, merge(&(*t)->r, (*t)->r, r); else *t = r, merge(&(*t)->l, l, (*t)->l); Calc(*t); } void add(tree **t, int x) { tree *l, *r, *m; split(*t, &l, &r, x); merge(&m, l, nn(x)); merge(t, m, r); } void del(tree **t, int x) { tree *l, *m, *r, *tmp; split(*t, &l, &tmp, x); split(tmp, &m, &r, x + 1); merge(t, l, r); } void Add(int x) { add(&root, x); } void Del(int x) { del(&root, x); } } t; int main() { int n; scanf("%d", &n); srand(unsigned(time(NULL))); while (n--) { char com[9]; int x; scanf("%s", com); if (!strcmp(com, "add")) scanf("%d", &x), t.Add(x); else if (!strcmp(com, "del")) scanf("%d", &x), t.Del(x); else printf("%I64d\n", t.root->sum[2]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; int n, value; char str[200]; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { cin >> str; if (strcmp(str, "add") == 0) { cin >> value; v.insert(lower_bound(v.begin(), v.end(), value), value); } else if (strcmp(str, "del") == 0) { cin >> value; v.erase(lower_bound(v.begin(), v.end(), value)); } else if (strcmp(str, "sum") == 0) { long long sum = 0; for (int j = 2; j < v.size(); j += 5) { sum += v[j]; } cout << sum << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; void solve(); int main() { int t = 1; while (t--) solve(); return 0; } struct treap { typedef struct _node { int v, y, cnt; long long sums[5]; _node *l, *r; _node(int X) : v(X), cnt(1), l(0), r(0) { y = (rand() << 16) | rand(); memset(sums, 0, sizeof(sums)); sums[0] = v; } void recalc() { cnt = 0; memset(sums, 0, sizeof sums); if (l) { for (int i = 0; i < 5; i++) sums[(i + cnt) % 5] += l->sums[i]; cnt += l->cnt; } sums[cnt++ % 5] += v; if (r) { for (int i = 0; i < 5; i++) sums[(i + cnt) % 5] += r->sums[i]; cnt += r->cnt; } } } * node; node root; node merge(node l, node r) { if (!l) return r; if (!r) return l; if (l->y < r->y) { l->r = merge(l->r, r); l->recalc(); return l; } else { r->l = merge(l, r->l); r->recalc(); return r; } } void split(node v, int key, node &l, node &r) { l = r = 0; if (!v) return; node tmp; if (v->v < key) { l = v; split(l->r, key, tmp, r); l->r = tmp; l->recalc(); } else { r = v; split(r->l, key, l, tmp); r->l = tmp; r->recalc(); } } treap() : root(0) {} void insert(int x) { node l, r; split(root, x, l, r); root = merge(merge(l, new _node(x)), r); } void erase(int x) { node l, m1, m2, r; split(root, x, l, m1); split(m1, x + 1, m2, r); if (m2) { delete m2; m2 = 0; } root = merge(l, r); } long long get(int mod) { if (!root) return 0; return root->sums[mod % 5]; } }; int num; string s; void solve() { cin >> num; treap tree; for (int i = 0; i < num; i++) { cin >> s; if (s[0] == 's') { printf("%I64d\n", tree.get(2)); continue; } int n; scanf("%d", &n); if (s[0] == 'd') { tree.erase(n); continue; } if (s[0] == 'a') { tree.insert(n); continue; } } }
#include <bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 20, SQ = 330; int n, query[N], sz[SQ], type[N]; long long sum[SQ][5]; bool mark[N]; vector<int> vec; void compress() { for (int i = 0; i < n; i++) if (query[i]) vec.push_back(query[i]); sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); for (int i = 0; i < n; i++) query[i] = (query[i] ? lower_bound(vec.begin(), vec.end(), query[i]) - vec.begin() : -1); } void update(int idx) { mark[idx] = !mark[idx]; for (int i = 0; i < 5; i++) sum[idx / SQ][i] = 0; sz[idx / SQ] = 0; for (int i = idx - idx % SQ; i < min(idx - idx % SQ + SQ, N); i++) if (mark[i]) { sz[idx / SQ]++; sum[idx / SQ][(sz[idx / SQ]) % 5] += vec[i]; } } long long get() { int _sz = 0; long long res = 0; for (int i = 0; i < SQ; i++) { res += sum[i][(3 - _sz % 5 + 5) % 5]; _sz += sz[i]; } return res; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s != "sum") cin >> query[i]; type[i] = (s == "sum" ? 2 : (s == "add" ? 1 : 0)); } compress(); for (int i = 0; i < n; i++) { if (type[i] == 2) cout << get() << "\n"; else { if (mark[query[i]] != type[i]) update(query[i]); } } return 0; }
#include <bits/stdc++.h> const int N = 1e5 + 7; struct Q { int opt, v; } qq[N]; int q, v[N], cnt; struct Seg { long long tree[N * 4][5]; int sz[N * 4]; void pushup(int p) { sz[p] = sz[p << 1] + sz[p << 1 | 1]; for (int i = 0; i < 5; i++) tree[p][i] = tree[p << 1][i] + tree[p << 1 | 1][((i - sz[p << 1]) % 5 + 5) % 5]; } void update(int p, int l, int r, int pos, int opt) { if (l == r) { if (opt == -1) tree[p][0] = 0, sz[p] = 0; else tree[p][0] = v[pos], sz[p] = 1; return; } int mid = l + r >> 1; if (pos <= mid) update(p << 1, l, mid, pos, opt); else update(p << 1 | 1, mid + 1, r, pos, opt); pushup(p); } long long query() { return tree[1][2]; } } seg; int main() { scanf("%d", &q); for (int i = 1; i <= q; i++) { static char op[10]; scanf("%s", op); if (op[0] == 'a' || op[0] == 'd') { qq[i].opt = op[0] == 'a' ? 1 : -1; scanf("%d", &qq[i].v); v[++cnt] = qq[i].v; } else { qq[i].opt = 0; } } std::sort(v + 1, v + 1 + cnt); cnt = std::unique(v + 1, v + 1 + cnt) - v - 1; for (int i = 1; i <= q; i++) { if (qq[i].opt == 0) { printf("%lld\n", seg.query()); } else { seg.update(1, 1, cnt, std::lower_bound(v + 1, v + 1 + cnt, qq[i].v) - v, qq[i].opt); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int nmax = 1e5 + 42; struct node { int numbers, l = -1, r = -1; long long sums[5] = {0, 0, 0, 0, 0}; }; int n; node tree[1 << 21]; int ind = 1; node my_merge(int l, int r) { node ret; ret.numbers = 0; ret.l = l; ret.r = r; if (l != -1) { for (int j = 0; j < 5; j++) ret.sums[j] = tree[l].sums[j]; ret.numbers = ret.numbers + tree[l].numbers; } if (r != -1) { for (int j = 0; j < 5; j++) ret.sums[(j + ret.numbers) % 5] += tree[r].sums[j]; ret.numbers = ret.numbers + tree[r].numbers; } return ret; } void update(int node, int l, int r, int pos, int value) { if (l == r) { tree[node].numbers = tree[node].numbers + value; tree[node].sums[0] = tree[node].sums[0] + value * pos; return; } int av = (l + r) / 2; if (pos <= av) { if (tree[node].l == -1) { ind++; tree[node].l = ind; } update(tree[node].l, l, av, pos, value); } else { if (tree[node].r == -1) { ind++; tree[node].r = ind; } update(tree[node].r, av + 1, r, pos, value); } tree[node] = my_merge(tree[node].l, tree[node].r); } int main() { scanf("%i", &n); for (int i = 1; i <= n; i++) { char inp[3]; for (int j = 0; j < 3; j++) { scanf("%c", &inp[j]); if ('a' > inp[j] || inp[j] > 'z') j--; } int x; if (inp[0] == 'a') { scanf("%i", &x); update(1, 1, 1e9, x, 1); } if (inp[0] == 'd') { scanf("%i", &x); update(1, 1, 1e9, x, -1); } if (inp[0] == 's') printf("%I64d \n", tree[1].sums[2]); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> a; int main() { int n; char s[15]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s); if (s[0] == 's') { long long ans = 0; for (int i = 2; i < a.size(); i += 5) ans += a[i]; printf("%I64d\n", ans); } else { int p; scanf("%d", &p); if (s[0] == 'a') a.insert(lower_bound(a.begin(), a.end(), p), p); else a.erase(lower_bound(a.begin(), a.end(), p)); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int LEN = 300; int x[100000], t[100000], z[100000]; pair<int, int> y[100000]; long long sum[100000 / LEN + 1][5]; int cnt[100000 / LEN + 1]; int w[100000 / LEN + 1][LEN]; int n, m, o; char s[10]; long long cal() { int cur = 0; long long ans = 0; for (int i = 0; i < o; i++) { ans += ::sum[i][(7 - cur) % 5]; cur = (cur + ::cnt[i]) % 5; } return ans; } void add(int x, int y) { int j = x / LEN; for (int i = cnt[j]; i >= 0; i--) if (i > 0 && w[j][i - 1] > y) w[j][i] = w[j][i - 1]; else { w[j][i] = y; break; } cnt[j]++; for (int i = 0; i < 5; i++) sum[j][i] = 0; for (int i = 0, k = 0; i < cnt[j]; i++, k = (k == 4 ? 0 : k + 1)) { sum[j][k] += w[j][i]; } } void del(int x, int y) { int j = x / LEN; for (int i = 0; i < cnt[j]; i++) if (w[j][i] == y) { for (int q = i; q + 1 < cnt[j]; q++) w[j][q] = w[j][q + 1]; break; } cnt[j]--; for (int i = 0; i < 5; i++) sum[j][i] = 0; for (int i = 0, k = 0; i < cnt[j]; i++, k = (k == 4 ? 0 : k + 1)) { sum[j][k] += w[j][i]; } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", &s); if (s[0] == 'a') t[i] = 0; else if (s[0] == 'd') t[i] = 1; else t[i] = 2; if (t[i] != 2) scanf("%d", &x[i]); y[i] = make_pair(x[i], i); } sort(y, y + n); int r = 0; for (int i = 0; i < n; i++) { if (i > 0 && y[i - 1].first != y[i].first) r++; z[y[i].second] = r; } o = (n + LEN - 1) / LEN; for (int i = 0; i < n; i++) { if (t[i] == 2) printf("%I64d\n", cal()); else if (t[i] == 0) add(z[i], x[i]); else del(z[i], x[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int Num; char CH[20]; const int inf = 0x3f3f3f3f; inline long long read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void P(int x) { Num = 0; if (!x) { putchar('0'); puts(""); return; } while (x > 0) CH[++Num] = x % 10, x /= 10; while (Num) putchar(CH[Num--] + 48); puts(""); } vector<int> s; int main() { char a[10]; int b, n = read(); for (int i = 0; i < n; i++) { scanf("%s", a); if (a[0] == 'a') { scanf("%d", &b); s.insert(lower_bound(s.begin(), s.end(), b), b); } if (a[0] == 'd') { scanf("%d", &b); s.erase(lower_bound(s.begin(), s.end(), b)); } if (a[0] == 's') { long long sum = 0; for (int j = 2; j < s.size(); j += 5) sum += s[j]; cout << sum << endl; } } }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const int NMAX = 1 << 17; int n, m; int cnt[2 * NMAX]; long long w[5][2 * NMAX]; void create_tree() { m = 1; while (m < n) m *= 2; for (int i = 0, _n = (5); i < _n; ++i) fill_n(w[i], 2 * m, 0); fill_n(cnt, 2 * m, 0); } inline void update_node(int u) { cnt[u] = cnt[2 * u] + cnt[2 * u + 1]; for (int i = 0, _n = (5); i < _n; ++i) { int nind = (i - cnt[2 * u]) % 5; if (nind < 0) nind += 5; w[i][u] = w[i][2 * u] + w[nind][2 * u + 1]; } } void add_tree(int ind, int val) { int vi = m + ind; w[0][vi] = val; cnt[vi] = 1; vi /= 2; while (vi != 0) { update_node(vi); vi /= 2; } } void remove_tree(int ind) { int vi = m + ind; w[0][vi] = 0; cnt[vi] = 0; vi /= 2; while (vi != 0) { update_node(vi); vi /= 2; } } inline long long check_tree() { return w[2][1]; } const int QMAX = 100000; enum { ADD, DEL, SUM }; struct query { int x, type; }; query Q[QMAX]; int nums[QMAX]; int main() { int N; scanf("%d", &N); for (int i = 0, _n = (N); i < _n; ++i) { static char buf[100]; scanf("%s", buf); if (buf[0] == 's') Q[i].type = SUM; else if (buf[0] == 'a') { Q[i].type = ADD; scanf("%d", &Q[i].x); } else { Q[i].type = DEL; scanf("%d", &Q[i].x); } } int NN = 0; for (int i = 0, _n = (N); i < _n; ++i) if (Q[i].type != SUM) nums[NN++] = Q[i].x; sort(nums, nums + NN); NN = unique(nums, nums + NN) - nums; ::n = NN; create_tree(); for (int i = 0, _n = (N); i < _n; ++i) if (Q[i].type == SUM) { printf("%I64d\n", check_tree()); } else if (Q[i].type == ADD) { int ind = lower_bound(nums, nums + NN, Q[i].x) - nums; add_tree(ind, Q[i].x); } else { int ind = lower_bound(nums, nums + NN, Q[i].x) - nums; remove_tree(ind); } }
#include <bits/stdc++.h> using namespace std; int q, n; int type[100100]; long long num[100100]; string s; vector<long long> v; long long sum[400100][5]; int cnt[400100]; int nu; void merge(int a, int b, int c) { cnt[a] = cnt[b] + cnt[c]; for (int i = 0; i < 5; i++) sum[a][i] = sum[b][i]; int g = cnt[b] % 5; for (int i = 0; i < 5; i++) sum[a][(i + g) % 5] += sum[c][i]; } void add(int id) { id += nu; cnt[id] = 1; sum[id][0] = v[id - nu]; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } void del(int id) { id += nu; cnt[id] = 0; sum[id][0] = 0; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } int main() { cin >> q; for (int i = 0; i < q; i++) { cin >> s; if (s == "add") { type[i] = 0; cin >> num[i]; v.push_back(num[i]); } else if (s == "del") { type[i] = 1; cin >> num[i]; } else { type[i] = 2; } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); n = v.size(); nu = 1; while (nu < n + 5) { nu *= 2; } for (int i = 0; i < q; i++) { if (num[i]) { num[i] = lower_bound(v.begin(), v.end(), num[i]) - v.begin(); } } for (int i = 0; i < q; i++) { if (type[i] == 0) { add(num[i]); } else if (type[i] == 1) { del(num[i]); } else { cout << sum[1][2] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long gen, n, m, i, j, x, y, cnt; string st; struct treap { long long rnd, zhi, ls, rs, sz, sum[5]; } tr[100005]; void lzhuan(long long &k) { long long p = tr[k].rs; tr[k].rs = tr[p].ls; tr[p].ls = k; tr[p].sz = tr[k].sz; k = p; } void rzhuan(long long &k) { long long p = tr[k].ls; tr[k].ls = tr[p].rs; tr[p].rs = k; tr[p].sz = tr[k].sz; k = p; } void getsum(long long k) { if (!k) return; tr[k].sz = tr[tr[k].ls].sz + tr[tr[k].rs].sz + 1; long long i, j = tr[tr[k].ls].sz; j %= 5; for (i = 0; i <= 4; i++) tr[k].sum[i] = tr[tr[k].ls].sum[i]; for (i = 0; i <= 4; i++) { j = (j + 1) % 5; tr[k].sum[j] += tr[tr[k].rs].sum[i]; } tr[k].sum[j] += tr[k].zhi; } void insert(long long &k, long long x) { if (tr[k].zhi == x) return; if (!k) { tr[++cnt].zhi = x; tr[cnt].ls = tr[cnt].rs = 0; tr[cnt].rnd = rand(); long long i; for (i = 0; i <= 4; i++) tr[cnt].sum[i] = 0; tr[cnt].sum[0] = x; tr[cnt].sz = 1; k = cnt; return; } if (x < tr[k].zhi) { insert(tr[k].ls, x); if (tr[tr[k].ls].rnd > tr[k].rnd) { rzhuan(k); getsum(tr[k].rs); } } else { insert(tr[k].rs, x); if (tr[tr[k].rs].rnd > tr[k].rnd) { lzhuan(k); getsum(tr[k].ls); } } getsum(k); } void erase(long long &k, long long x) { if (!k) return; if (tr[k].zhi == x) { if (!tr[k].ls || !tr[k].rs) k = tr[k].ls + tr[k].rs; else if (tr[tr[k].ls].rnd < tr[tr[k].rs].rnd) { rzhuan(k); erase(k, x); } else { lzhuan(k); erase(k, x); } } else if (tr[k].zhi < x) erase(tr[k].rs, x); else erase(tr[k].ls, x); getsum(k); } int main() { ios::sync_with_stdio(0); cin >> n; for (i = 1; i <= n; i++) { cin >> st; if (st == "add") { cin >> x; insert(gen, x); } if (st == "del") { cin >> x; erase(gen, x); } if (st == "sum") cout << tr[gen].sum[2] << endl; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:10000000") const long double LDINF = 9128739847123.00; const long double eps = 1e-9; const int INF = 1 << 30; const long double pi = fabsl(atan2(0.0, -1.0)); using namespace std; int n; const int TSIZE = 1 << 17; long long tree[2 * TSIZE + 1][5]; int cnt[2 * TSIZE + 1]; vector<int> all; int a[100010]; char type[100010][6]; void Load() { cin >> n; int i; { int c = 0; while ((c = getchar()) != 10 && c != EOF) ; }; for (i = 0; i < n; i++) { scanf("%s%d", type[i], &a[i]); all.push_back(a[i]); } sort(all.begin(), all.end()); all.erase(unique(all.begin(), all.end()), all.end()); } void update(int i) { while (i > 0) { cnt[i] = cnt[i << 1] + cnt[(i << 1) | 1]; for (int k = 0; k < 5; k++) tree[i][k] = tree[(i << 1)][k]; for (int k = 0; k < 5; k++) { tree[i][(cnt[i << 1] + k) % 5] += tree[(i << 1) | 1][k]; } i >>= 1; } } int find(int key) { int l = 0; int r = all.size() - 1; int mid; while (l <= r) { mid = (l + r) / 2; if (all[mid] == key) return mid; else if (all[mid] < key) l = mid + 1; else r = mid - 1; } throw 42; return -1; } void Solve() { int i; int j; for (i = 0; i < n; i++) { j = find(a[i]); if (strcmp(type[i], "add") == 0) { for (int k = 0; k < 5; k++) tree[TSIZE + j][k] = 0; tree[TSIZE + j][0] = a[i]; cnt[TSIZE + j] = 1; update((TSIZE + j) / 2); } else if (strcmp(type[i], "del") == 0) { for (int k = 0; k < 5; k++) tree[TSIZE + j][k] = 0; cnt[TSIZE + j] = 0; update((TSIZE + j) / 2); } else { printf("%I64d\n", tree[1][2]); } } } int main() { Load(); Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct wut { int op; int val; int nval; wut() {} wut(int a, int b) : op(a), val(b) {} }; struct node { long long sum[5]; int ile; node() { for (int i = 0; i < (5); ++i) sum[i] = 0; ile = 0; } }; const int N = 100010; wut tab[N]; int n; char tax[10]; int c; set<int> h; map<int, int> et; node tree[3 * N]; int roz; void insert(int value, int pos, bool add) { pos = roz + pos - 1; if (add) { tree[pos].sum[0] += value; tree[pos].ile++; } else { tree[pos].sum[0] -= value; tree[pos].ile--; } while (pos >> 1) { for (int i = 0; i < (5); ++i) tree[pos >> 1].sum[i] = tree[(pos >> 1) << 1].sum[i]; for (int i = 0; i < (5); ++i) { tree[pos >> 1].sum[i] += tree[((pos >> 1) << 1) + 1] .sum[(((i - tree[(pos >> 1) << 1].ile) % 5) + 5) % 5]; } tree[pos >> 1].ile = tree[(pos >> 1) << 1].ile + tree[((pos >> 1) << 1) + 1].ile; pos >>= 1; } } long long query() { return tree[1].sum[2]; } int main() { scanf("%d", &n); for (int i = 0; i < (n); ++i) { scanf("%s %d", tax, &c); if (tax[0] == 'a') tab[i] = wut(0, c); else if (tax[0] == 'd') tab[i] = wut(1, c); else tab[i] = wut(2, 0); if (tab[i].op <= 1) h.insert(tab[i].val); } int ind = 0; for (typeof((h).begin()) p = ((h).begin()); p != (h).end(); ++p) { et[*p] = ++ind; } for (int i = 0; i < (n); ++i) if (tab[i].op <= 1) { tab[i].nval = et[tab[i].val]; } roz = 1; while (roz < ind) roz <<= 1; for (int i = 0; i < (n); ++i) { if (tab[i].op == 2) { cout << query() << endl; } else { insert(tab[i].val, tab[i].nval, !tab[i].op); } } }
#include <bits/stdc++.h> using namespace std; vector<int> a; int n, x; char ord[9]; int main(int argc, char const *argv[]) { scanf("%d", &n); while (n--) { scanf("%s", ord); if (ord[0] == 's') { long long z = 0LL; for (int i = 2; i < a.size(); i += 5) z += a[i]; printf("%I64d\n", z); } else { scanf("%d", &x); if (ord[0] == 'a') a.insert(lower_bound(a.begin(), a.end(), x), x); else a.erase(lower_bound(a.begin(), a.end(), x)); } } return 0; }
#include <bits/stdc++.h> using namespace std; int q, n; int type[100100]; long long num[100100]; string s; vector<long long> v; long long sum[400100][5]; int cnt[400100]; int nu; void merge(int a, int b, int c) { cnt[a] = cnt[b] + cnt[c]; for (int i = 0; i < 5; i++) sum[a][i] = sum[b][i]; int g = cnt[b] % 5; for (int i = 0; i < 5; i++) sum[a][(i + g) % 5] += sum[c][i]; } void add(int id) { id += nu; cnt[id] = 1; sum[id][0] = v[id - nu]; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } void del(int id) { id += nu; cnt[id] = 0; sum[id][0] = 0; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } int main() { cin >> q; for (int i = 0; i < q; i++) { cin >> s; if (s == "add") { type[i] = 0; cin >> num[i]; v.push_back(num[i]); } else if (s == "del") { type[i] = 1; cin >> num[i]; } else { type[i] = 2; } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); n = v.size(); nu = 1; while (nu < n + 5) { nu *= 2; } for (int i = 0; i < q; i++) { if (num[i]) { num[i] = lower_bound(v.begin(), v.end(), num[i]) - v.begin(); } } for (int i = 0; i < q; i++) { if (type[i] == 0) { add(num[i]); } else if (type[i] == 1) { del(num[i]); } else { cout << sum[1][2] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 8 * 102400; int LEFT(int id) { return 2 * id + 1; } int RIGHT(int id) { return 2 * id + 2; } int cnt[N]; long long sum[N][5]; void build(int id, int x, int y) { for (int i = 0; i < 5; i++) sum[id][i] = 0; if (y - x == 1) return; int mid = (x + y) >> 1; build(LEFT(id), x, mid); build(RIGHT(id), mid, y); } void update(int id, int x, int y) { if (y - x == 1) return; int left = LEFT(id), right = RIGHT(id); cnt[id] = cnt[left] + cnt[right]; int cc = cnt[left] % 5; for (int i = 0; i < 5; i++) { sum[id][i] = sum[left][i] + sum[right][(i - cc + 5) % 5]; } } void insert(int id, int x, int y, int px, int pv) { if (y - x == 1) { cnt[id]++; sum[id][0] += pv; return; } int mid = (y + x) >> 1; if (px < mid) insert(LEFT(id), x, mid, px, pv); else insert(RIGHT(id), mid, y, px, pv); update(id, x, y); } void remove(int id, int x, int y, int px, int pv) { if (y - x == 1) { cnt[id]--; sum[id][0] -= pv; return; } int mid = (y + x) >> 1; if (px < mid) remove(LEFT(id), x, mid, px, pv); else remove(RIGHT(id), mid, y, px, pv); update(id, x, y); } int tp[N], num[N]; int x[N], xn; int main() { int n; scanf("%d", &n); xn = 0; for (int i = 0; i < n; i++) { char str[5]; scanf("%s", str); if (str[0] == 'a') { tp[i] = 0; scanf("%d", num + i); x[xn++] = num[i]; } else if (str[0] == 'd') { tp[i] = 1; scanf("%d", num + i); x[xn++] = num[i]; } else { tp[i] = 2; } } if (xn == 0) { for (int i = 0; i < n; i++) printf("0\n"); return 0; } sort(x, x + xn); xn = unique(x, x + xn) - x; build(0, 0, xn); for (int i = 0; i < n; i++) { if (tp[i] == 0) { int id = lower_bound(x, x + xn, num[i]) - x; insert(0, 0, xn, id, num[i]); } else if (tp[i] == 1) { int id = lower_bound(x, x + xn, num[i]) - x; remove(0, 0, xn, id, num[i]); } else { printf("%I64d\n", sum[0][2]); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long sumv[100005 << 2][5]; int lazy[100005 << 2]; long long tmp[5]; int value[100005]; int cntv[100005 << 2]; int flag[100005], vv[100005]; int n, m; void build(int rt, int L, int R) { if (L == R) return; int mid = L + R >> 1; build(rt << 1, L, mid); build(rt << 1 | 1, mid + 1, R); } void push_up(int rt) { for (int i = 0; i < 5; i++) { sumv[rt][i] = sumv[rt << 1][i] + sumv[rt << 1 | 1][i]; } cntv[rt] = cntv[rt << 1] + cntv[rt << 1 | 1]; } void push_down(int rt) { if (lazy[rt] != 0) { int tt = lazy[rt]; lazy[rt] = 0; for (int i = 0; i < 5; i++) { tmp[i] = sumv[rt << 1][i]; } for (int i = 0; i < 5; i++) { sumv[rt << 1][i] = tmp[(i - tt + 5) % 5]; } for (int i = 0; i < 5; i++) { tmp[i] = sumv[rt << 1 | 1][i]; } for (int i = 0; i < 5; i++) { sumv[rt << 1 | 1][i] = tmp[(i - tt + 5) % 5]; } lazy[rt << 1] += tt; lazy[rt << 1 | 1] += tt; lazy[rt << 1] %= 5; lazy[rt << 1 | 1] %= 5; } } int lower(int rt, int L, int R, int p) { if (R <= p) { return cntv[rt]; } int mid = L + R >> 1; if (p <= mid) { return lower(rt << 1, L, mid, p); } else { return cntv[rt << 1] + lower(rt << 1 | 1, mid + 1, R, p); } } void modify(int rt, int L, int R, int p, int flag, int id) { if (L == R) { if (flag) { sumv[rt][id] += value[p]; cntv[rt]++; } else { sumv[rt][id] -= value[p]; cntv[rt]--; } return; } int mid = L + R >> 1; push_down(rt); if (p <= mid) modify(rt << 1, L, mid, p, flag, id); else modify(rt << 1 | 1, mid + 1, R, p, flag, id); push_up(rt); } void update(int rt, int L, int R, int p, int tt) { if (p <= L) { lazy[rt] += tt; for (int i = 0; i < 5; i++) { tmp[i] = sumv[rt][i]; } for (int i = 0; i < 5; i++) { sumv[rt][i] = tmp[(i - tt + 5) % 5]; } lazy[rt] %= 5; return; } int mid = L + R >> 1; push_down(rt); if (p <= mid) update(rt << 1, L, mid, p, tt); update(rt << 1 | 1, mid + 1, R, p, tt); push_up(rt); } char ss[10]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%s", ss); if (ss[0] == 'a') { scanf("%d", &a); value[m++] = a; flag[i] = 0; vv[i] = a; } else if (ss[0] == 'd') { flag[i] = 1; scanf("%d", &a); vv[i] = a; } else { flag[i] = 2; } } if (m == 0) m = 1; sort(value, value + m); m = unique(value, value + m) - value; build(1, 0, m - 1); for (int i = 0; i < n; i++) { if (flag[i] == 0) { int p = lower_bound(value, value + m, vv[i]) - value; int rk = 0; if (p) rk = lower(1, 0, m - 1, p - 1); rk++; if (p < m - 1) { update(1, 0, m - 1, p + 1, 1); } modify(1, 0, m - 1, p, 1, rk % 5); } else if (flag[i] == 1) { int p = lower_bound(value, value + m, vv[i]) - value; int rk = 0; if (p) rk = lower(1, 0, m - 1, p - 1); rk++; if (p < m - 1) { update(1, 0, m - 1, p + 1, -1); } modify(1, 0, m - 1, p, 0, rk % 5); } else { printf("%I64d\n", sumv[1][3]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MN = 333 * 333, SQ = 333; pair<string, long long> qw[MN]; vector<long long> vect[MN / SQ]; long long sum[MN / SQ][5], sz[MN / SQ]; int32_t main() { long long q; cin >> q; vector<long long> vec; for (long long i = 0; i < q; i++) { string type; cin >> type; long long x = 0; if (type == "add" || type == "del") cin >> x; qw[i] = {type, x}; vec.push_back(x); } sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); for (long long i = 0; i < q; i++) qw[i] = {qw[i].first, lower_bound(vec.begin(), vec.end(), qw[i].second) - vec.begin()}; for (long long i = 0; i < q; i++) { long long num = qw[i].second; string str = qw[i].first; if (str == "sum") { long long now = 2, res = 0; for (long long i = 0; i < MN / SQ; i++) { res += sum[i][now]; now = (((now - sz[i]) % 5) + 5) % 5; } cout << res << '\n'; } else { long long cn = num / SQ; if (str == "del") { vector<long long> q = vect[cn]; vect[cn].clear(); for (long long u : q) if (u != num) vect[cn].push_back(u); } else { vect[cn].push_back(num); sort(vect[cn].begin(), vect[cn].end()); } for (long long i = 0; i < 5; i++) sum[cn][i] = 0; sz[cn] = vect[cn].size(); for (long long i = 0; i < vect[cn].size(); i++) sum[cn][i % 5] += vec[vect[cn][i]]; } } }
#include <bits/stdc++.h> using namespace std; struct treap { treap *l, *r; int val; long long sum[5]; int fix, siz; int lsiz() { if (l) return l->siz; else return 0; } int rsiz() { if (r) return r->siz; else return 0; } } * rt; void gtsum(treap *&a) { if (!a) return; a->siz = a->lsiz() + 1 + a->rsiz(); int j = (a->lsiz() % 5); for (int i = 0; i < 5; i++) a->sum[i] = 0; for (int i = 0; i < 5; i++) { if (a->l) a->sum[i] += a->l->sum[i]; } for (int i = 0; i < 5; i++) { if (a->r) a->sum[(i + j + 1) % 5] += a->r->sum[i]; } a->sum[(j + 1) % 5] += a->val; } void lrot(treap *&a) { treap *b = a->r; a->r = b->l; b->l = a; gtsum(a); gtsum(b); a = b; } void rrot(treap *&a) { treap *b = a->l; a->l = b->r; b->r = a; gtsum(a); gtsum(b); a = b; } void ins(treap *&a, int v) { if (!a) { a = new treap; a->val = v; a->siz = 1; a->l = NULL; a->r = NULL; a->fix = rand(); for (int i = 0; i < 5; i++) a->sum[i] = 0; a->sum[1] = v; return; } if (v <= a->val) { ins(a->l, v); if (a->l->fix < a->fix) rrot(a); } else { ins(a->r, v); if (a->r->fix < a->fix) lrot(a); } gtsum(a); } void del(treap *&a, int v) { if (v == a->val) { if (a->l && a->r) { if (a->l->fix < a->r->fix) { rrot(a); del(a->r, v); } else { lrot(a); del(a->l, v); } gtsum(a); } else { treap *t = a; if (a->l) a = a->l; else a = a->r; delete t; } } else if (v < a->val) del(a->l, v); else del(a->r, v); gtsum(a); } int n; int main() { srand(time(0)); cin >> n; while (n--) { string typ; cin >> typ; int x; if (typ == "add") { cin >> x; ins(rt, x); } else if (typ == "del") { cin >> x; del(rt, x); } else { long long ans = 0LL; if (rt) ans = rt->sum[3]; printf("%lld\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, e, f[200009], pas, g[200009], z[409][5], x[409], sq; map<long long, long long> mp; pair<string, long long> p[200009]; void updseg(long long q) { x[q] = 0; z[q][0] = 0; z[q][1] = 0; z[q][2] = 0; z[q][3] = 0; z[q][4] = 0; long long ka = 0; for (long long h = (q - 1) * sq + 1; h <= q * sq; h++) { if (f[h] != 0) { x[q]++; ka++; if (ka == 5) ka = 0; } z[q][ka] += f[h]; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> a; multiset<pair<long long, long long> > s; s; multiset<pair<long long, long long> >::iterator it; it; for (b = 1; b <= a; b++) { cin >> p[b].first; if (p[b].first == "sum") { continue; } cin >> p[b].second; if (p[b].first == "add") { s.insert(make_pair(p[b].second, b)); } } for (it = s.begin(); it != s.end(); it++) { pas++; g[(*it).second] = pas; } long ju = pas; sq = sqrt(pas); for (b = 1; b <= a; b++) { if (p[b].first == "sum") { long long ka = 0; pas = 0; for (c = 1; c <= a; c++) { if ((c - 1) * sq + 1 > ju) break; e = a; if (e > c * sq) e = c * sq; pas += z[c][(8 - ka) % 5]; ka += x[c]; ka %= 5; } cout << pas << endl; continue; } c = p[b].second; if (p[b].first == "del") { f[mp[c]] = 0; updseg((mp[c] - 1) / sq + 1); continue; } f[g[b]] = c; mp[c] = g[b]; updseg((g[b] - 1) / sq + 1); } return 0; }
#include <bits/stdc++.h> using namespace std; int cnt[1010000], n, size, excnt, ex[1010000], x[1010000], y[1010000], op[1010000], i, id, pos[1010000], bl, a[1010000]; char ch[1010000]; long long b[1010000][6]; void init() { sort(ex + 1, ex + 1 + excnt); size = unique(ex + 1, ex + 1 + excnt) - ex - 1; for (i = 1; i <= n; i++) y[i] = lower_bound(ex + 1, ex + 1 + size, x[i]) - ex; } void update(int id) { int l = (id - 1) * bl + 1, r = id * bl, t = 0; memset(b[id], 0, sizeof(b[id])); cnt[id] = 0; for (int i = l; i <= r; i++) if (a[i] > 0) { t++; if (t >= 5) t -= 5; b[id][t] += a[i]; cnt[id]++; } } long long calc() { long long ret = 0, t = 0; for (int i = 1; i <= size / bl + 1; i++) { ret += b[i][(8 - t) % 5]; (t += cnt[i]) %= 5; } return ret; } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", ch); if (ch[0] == 's') { op[i] = 2; if (n == 1) { cout << 0 << endl; return 0; } } else { scanf("%d", &x[i]); ex[++excnt] = x[i]; op[i] = ch[0] == 'a' ? 0 : 1; } } init(); bl = sqrt(size); for (i = 1; i <= size; i++) pos[i] = (i - 1) / bl + 1; for (i = 1; i <= n; i++) { if (op[i] == 2) printf("%I64d\n", calc()); else { if (op[i] == 0) a[y[i]] = x[i]; else a[y[i]] = 0; update(pos[y[i]]); } } }
#include <bits/stdc++.h> using namespace std; string st; long long n, m, i, j, x, y, rt = 0, cnt; struct treap { long long zhi, fix, lch, rch, sz, sum[5]; } tr[100005]; void getsum(long long k) { if (!k) return; tr[k].sz = tr[tr[k].lch].sz + tr[tr[k].rch].sz + 1; long long i, j = tr[tr[k].lch].sz; j %= 5; for (i = 0; i <= 4; i++) tr[k].sum[i] = tr[tr[k].lch].sum[i]; for (i = 0; i <= 4; i++) { j = (j + 1) % 5; tr[k].sum[j] += tr[tr[k].rch].sum[i]; } tr[k].sum[j] += tr[k].zhi; } void split(long long x, long long &s, long long &t, long long zhi) { int i; if (x == 0) { s = t = 0; return; } if (zhi < tr[x].zhi) { split(tr[x].lch, s, tr[x].lch, zhi); t = x; } if (zhi > tr[x].zhi) { split(tr[x].rch, tr[x].rch, t, zhi); s = x; } getsum(x); } void merge(long long s, long long t, long long &x) { if (!s || !t) { x = s + t; return; } if (tr[s].fix > tr[t].fix) { merge(tr[s].rch, t, tr[s].rch); x = s; } else { merge(s, tr[t].lch, tr[t].lch); x = t; } getsum(x); } void insert(long long &x, long long zhi) { long long s = 0, t = 0; split(x, s, t, zhi); cnt++; tr[cnt].zhi = tr[cnt].sum[0] = zhi; tr[cnt].lch = tr[cnt].rch = 0; tr[cnt].fix = rand(); tr[cnt].sz = 1; getsum(cnt); getsum(s); merge(s, cnt, x); getsum(x); getsum(t); merge(x, t, x); getsum(x); } void erase(long long &x, long long zhi) { if (tr[x].zhi == zhi) { getsum(tr[x].lch); getsum(tr[x].rch); merge(tr[x].lch, tr[x].rch, x); getsum(x); return; } if (zhi < tr[x].zhi) erase(tr[x].lch, zhi); if (zhi > tr[x].zhi) erase(tr[x].rch, zhi); getsum(x); } long long query() { return tr[rt].sum[2]; } int main() { ios::sync_with_stdio(0); cin >> n; for (i = 1; i <= n; i++) { cin >> st; if (st == "add") { cin >> x; insert(rt, x); } if (st == "del") { cin >> x; erase(rt, x); } if (st == "sum") { cout << query() << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int size, cnt, ex[1010000], n, siz[1010000], i; char ch[10]; struct node { long long a[6]; void clr() { for (int i = 0; i <= 4; i++) a[i] = 0; } } a[1010000]; struct node1 { int op, a; } q[1010000]; void init() { sort(ex + 1, ex + 1 + cnt); size = unique(ex + 1, ex + 1 + cnt) - ex - 1; for (i = 1; i <= n; i++) if (q[i].op <= 2) q[i].a = lower_bound(ex + 1, ex + 1 + size, q[i].a) - ex; } void pushup(int rt) { siz[rt] = siz[(rt << 1)] + siz[(rt << 1 | 1)]; a[rt].clr(); for (int i = 0; i <= 4; i++) a[rt].a[i] += a[(rt << 1)].a[i]; for (int i = 0; i <= 4; i++) a[rt].a[(i + siz[(rt << 1)]) % 5] += a[(rt << 1 | 1)].a[i]; } void modify(int rt, int l, int r, int x, int delta) { if (l == r) { if (delta == 0) a[rt].clr(), siz[rt] = 0; else a[rt].a[1] = ex[l], siz[rt] = 1; return; } int mid = (l + r) / 2; if (x <= mid) modify((rt << 1), l, mid, x, delta); else modify((rt << 1 | 1), mid + 1, r, x, delta); pushup(rt); } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", ch); if (ch[0] == 'a') q[i].op = 1, scanf("%d", &q[i].a), ex[++cnt] = q[i].a; if (ch[0] == 'd') q[i].op = 2, scanf("%d", &q[i].a); if (ch[0] == 's') q[i].op = 3; } init(); for (i = 1; i <= n; i++) { if (q[i].op == 1) modify(1, 1, size, q[i].a, 1); else if (q[i].op == 2) modify(1, 1, size, q[i].a, 0); else printf("%I64d\n", a[1].a[3]); } return 0; }
#include <bits/stdc++.h> #pragma GCC target("sse4.2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; const long long MAXN = 1000 * 1000 + 7, MOD = 1000 * 1000 * 1000 + 7; long long n, q, qu[MAXN], ans[MAXN], a[MAXN]; vector<long long> vec; map<long long, long long> pos; struct node { long long sum[5], cnt; } seg[MAXN << 2]; node merge(node a, node b) { node res; res.cnt = (a.cnt + b.cnt) % 5; res.cnt += 5; res.cnt %= 5; for (long long i = 0; i < 5; i++) { res.sum[i] = a.sum[i] + b.sum[(i + 5 - a.cnt) % 5]; } return res; } void upd(long long val, long long x, long long b = 0, long long e = n, long long ind = 1) { long long mid = (b + e) / 2; if (b + 1 == e) { seg[ind].cnt = (val > 0); seg[ind].sum[0] += val; return; } if (x < mid) upd(val, x, b, mid, ind << 1); else upd(val, x, mid, e, ind << 1 | 1); seg[ind] = merge(seg[ind << 1], seg[ind << 1 | 1]); return; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); ; cin >> q; string s; for (long long i = 0; i < q; i++) { cin >> s; if (s == "sum") continue; cin >> qu[i]; vec.push_back(qu[i]); if (s == "del") qu[i] *= -1; } sort(vec.begin(), vec.end()); vec.resize(distance(vec.begin(), unique(vec.begin(), vec.end()))); n = vec.size(); for (long long i = 0; i < n; i++) { pos[vec[i]] = i; } for (long long i = 0; i < q; i++) { if (qu[i] == 0) { cout << seg[1].sum[2] << '\n'; } else { upd(qu[i], pos[abs(qu[i])]); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const int LEN = 300; int n; int m, o; int t[100000], x[100000], z[100000]; pair<int, int> y[100000]; long long sum[100000 / LEN + 1][5]; int cnt[100000 / LEN + 1]; int w[100000 / LEN + 1][LEN]; long long calc() { int cur = 0; long long sum = 0; for (int i = 0; i < o; i++) { sum += ::sum[i][(7 - cur) % 5]; cur = (cur + ::cnt[i]) % 5; } return sum; } int add(int x, int y) { int j = x / LEN; for (int i = cnt[j]; i >= 0; i--) if (i > 0 && w[j][i - 1] > y) w[j][i] = w[j][i - 1]; else { w[j][i] = y; break; } for (int i = 0; i < 5; i++) sum[j][i] = 0; cnt[j]++; for (int i = 0, k = 0; i < cnt[j]; i++, k = (k == 4 ? 0 : k + 1)) sum[j][k] += w[j][i]; return 0; } int del(int x, int y) { int j = x / LEN; for (int i = 0; i < cnt[j]; i++) if (w[j][i] == y) { for (int k = i; k + 1 < cnt[j]; k++) w[j][k] = w[j][k + 1]; break; } for (int i = 0; i < 5; i++) sum[j][i] = 0; cnt[j]--; for (int i = 0, k = 0; i < cnt[j]; i++, k = (k == 4 ? 0 : k + 1)) sum[j][k] += w[j][i]; return 0; } char s[10]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s); if (s[0] == 'a') t[i] = 0; else if (s[0] == 'd') t[i] = 1; else t[i] = 2; if (t[i] != 2) scanf("%d", &x[i]); y[i] = make_pair(x[i], i); } sort(y, y + n); int r = 0; for (int i = 0; i < n; i++) { if (i > 0 && y[i - 1].first != y[i].first) r++; z[y[i].second] = r; } o = (n + LEN - 1) / LEN; for (int i = 0; i < n; i++) if (t[i] == 2) printf("%I64d\n", calc()); else if (t[i] == 0) { add(z[i], x[i]); } else { del(z[i], x[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> x; int n; void add() { int a; cin >> a; x.insert(lower_bound(x.begin(), x.end(), a), a); } void del() { int a; cin >> a; x.erase(lower_bound(x.begin(), x.end(), a)); } long long sum() { long long ans = 0; for (int i = 0; i * 5 + 2 < x.size(); i++) ans = ans + x[i * 5 + 2]; return ans; } int main() { cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "add") add(); if (s == "del") del(); if (s == "sum") printf("%I64d\n", sum()); } }
#include <bits/stdc++.h> using namespace std; long long h[100010], dat[100010], s[100010][5]; int n, root, tot, l[100010], r[100010], sum[100010]; void update(int x) { if (!x) return; int ss = sum[l[x]] + 1; for (int i = 0; i < 5; i++) s[x][i] = (long long)(s[l[x]][i] + s[r[x]][(i - ss % 5 + 5) % 5]); s[x][ss % 5] += (long long)dat[x]; sum[x] = ss + sum[r[x]]; h[x] = max(h[l[x]], h[r[x]]) + 1; } void zig(int &x) { int y = l[x]; l[x] = r[y]; r[y] = x; update(x); update(y); x = y; } void zag(int &x) { int y = r[x]; r[x] = l[y]; l[y] = x; update(x); update(y); x = y; } void balance(int &x) { update(x); if (h[l[x]] > h[r[x]] + 1) { if (h[l[l[x]]] < h[l[r[x]]]) zag(l[x]); zig(x); } if (h[r[x]] > h[l[x]] + 1) { if (h[r[r[x]]] < h[r[l[x]]]) zig(r[x]); zag(x); } } void ins(int &x, long long v) { if (!x) { dat[x = ++tot] = v; l[x] = r[x] = 0; } else ins(v < dat[x] ? l[x] : r[x], v); balance(x); } void del(int &x, long long v) { if ((!l[x]) && (!r[x])) { dat[x] = 0; x = 0; return; } if (dat[x] == v) { if (h[l[x]] > h[r[x]]) { zig(x); del(r[x], v); } else { zag(x); del(l[x], v); } } else del(v < dat[x] ? l[x] : r[x], v); balance(x); } void dfs(int x) { if (!x) return; dfs(l[x]); cout << dat[x] << " "; dfs(r[x]); } int main() { scanf("%d\n", &n); memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); memset(s, 0, sizeof(s)); memset(h, 0, sizeof(h)); memset(dat, 0, sizeof(dat)); memset(sum, 0, sizeof(sum)); root = 1; tot = 0; h[0] = -1; while (n--) { char x[10]; scanf("%s", &x); char y = x[0]; long long z; if (y == 'a') { scanf("%I64d", &z); ins(root, z); } else if (y == 'd') { scanf("%I64d", &z); del(root, z); } else printf("%I64d\n", s[root][3]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010, L = 320; long long sum[L << 1][5]; int m, ndel = 0; vector<int> vec[L << 1]; inline void upd(int& t) { for (int i = 0; i < 5; i++) sum[t][i] = 0; for (int i = 0; i < (int)vec[t].size(); i++) sum[t][i % 5] += (long long)vec[t][i]; } vector<int> tmp; inline void relax(void) { ndel = 0; tmp.resize(0); for (int i = 0; i < m; i++) { for (int j = 0; j < (int)vec[i].size(); j++) tmp.push_back(vec[i][j]); vec[i].resize(0); } m = 0; for (int i = 0; i < (int)tmp.size(); i++) { vec[m].push_back(tmp[i]); if ((int)vec[m].size() >= L) ++m; } m++; for (int i = 0; i < m; i++) upd(i); } int main() { int n; scanf("%d", &n); char cmd[20]; int x, idx, sz; long long ans; while (n--) { scanf("%s", cmd); if (*cmd == 's') { sz = 0; ans = 0; for (int i = 0; i < m; i++) { ans += sum[i][(2 - (sz % 5) + 5) % 5]; sz += (int)vec[i].size(); } printf("%I64d\n", ans); } else if (*cmd == 'a') { scanf("%d", &x); idx = 0; while (idx < m - 1 && vec[idx].back() < x) idx++; vec[idx].push_back(x); m = max(m, idx + 1); int j = (int)vec[idx].size() - 1; while (j && vec[idx][j] < vec[idx][j - 1]) { swap(vec[idx][j], vec[idx][j - 1]); j--; } if ((int)vec[idx].size() >= 2 * L) relax(); upd(idx); } else { ++ndel; scanf("%d", &x); int i = 0; while (vec[i].back() < x) i++; vec[i].erase(find(vec[i].begin(), vec[i].end(), x)); upd(i); if (ndel >= 2 * L) relax(); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline string toString(T a) { ostringstream os(""); os << a; return os.str(); } template <typename T> inline long long toLong(T a) { long long res; istringstream os(a); os >> res; return res; } template <typename T> inline T SQ(T a) { return a * a; } template <typename T> inline T gcd(T a, T b) { if (b == 0) return a; else return gcd(b, a % b); } template <typename T> inline unsigned long long bigmod(T a, T b, T m) { if (b == 0) return 1; else if (b % 2 == 0) return S(bigmod(a, b / 2, m)) % m; else return (a % m * bigmod(a, b - 1, m)) % m; } template <typename T> inline vector<string> parse(T str) { vector<string> res; string s; istringstream os(str); while (os >> s) res.push_back(s); return res; } template <typename T> inline unsigned long long dist(T A, T B) { unsigned long long res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); return res; } template <typename T> inline double cosAngle(T a, T b, T c) { double res = a * a + b * b - c * c; res = res / (2 * a * b); res = acos(res); return res; } template <typename T> inline T power(T base, int po) { T res = 1; if (base == 0) return 0; for (int i = (0); i < (po); i++) res *= base; return res; } template <typename T> inline bool isOn(T mask, T pos) { return mask & (1 << pos); } template <typename T> inline int Off(T mask, T pos) { return mask ^ (1 << pos); } template <typename T> inline int On(T mask, T pos) { return mask | (1 << pos); } struct nod { long long st, ed; nod *l, *r; int ase; long long arr[5]; nod() {} nod(int _x, int _y) { st = _x; ed = _y; ase = 0; l = r = NULL; for (int i = 0; i < 5; i++) arr[i] = 0; } }; void insert(long long val, nod *ob, int c) { long long s, e; s = ob->st; e = ob->ed; if (s == e && s == val) { ob->arr[1] += val * c; ob->ase += c; return; } long long mid = (s + e) / 2; if (ob->l == NULL) { ob->l = new nod(s, mid); } if (ob->r == NULL) { ob->r = new nod(mid + 1, e); } if (val <= mid) { insert(val, ob->l, c); } else insert(val, ob->r, c); int x, y; ob->ase = ob->l->ase + ob->r->ase; x = ob->l->ase; for (int i = 0; i < 5; i++) { y = (i - x + 5) % 5; if (y < 0) y += 5; ob->arr[i] = ob->l->arr[i] + ob->r->arr[y]; } return; } int main() { char s[5]; nod *root = new nod(1, 1000000000); int i, ii, j, k; int n; int x; cin >> n; for (int ii = (0); ii < (n); ii++) { scanf("%s", s); if (s[0] == 'a') { scanf("%d", &x); insert(x, root, 1); } else if (s[0] == 'd') { scanf("%d", &x); insert(x, root, -1); } else if (s[0] == 's') { cout << root->arr[3] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; int qread() { int w = 1, c, ret; while ((c = getchar()) > '9' || c < '0') w = (c == '-' ? -1 : 1); ret = c - '0'; while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + c - '0'; return ret * w; } string reads() { char c; string s; while ((c = getchar()) == ' ' || c == '\n') ; s += c; while ((c = getchar()) != ' ' && c != '\n') s += c; return s; } vector<int> P; long long t, ans; int main() { for (int T = qread(); T >= 1; T--) { switch (reads()[0]) { case 'a': t = qread(), P.insert(lower_bound(P.begin(), P.end(), t), t); break; case 'd': t = qread(), P.erase(lower_bound(P.begin(), P.end(), t)); break; case 's': ans = 0; for (int i = 2; i < P.size(); i += 5) ans += P[i]; printf("%lld\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k; char s[N]; vector<int> v; int main() { while (~scanf("%d", &n)) { v.clear(); for (int i = 0; i < n; i++) { scanf("%s", s); if (s[0] == 's') { long long ans = 0; for (int i = 2; i < v.size(); i += 5) ans += v[i]; printf("%I64d\n", ans); continue; } scanf("%d", &k); if (s[0] == 'a') v.insert(lower_bound(v.begin(), v.end(), k), k); else if (s[0] == 'd') v.erase(lower_bound(v.begin(), v.end(), k)); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Treap_Node { Treap_Node *left, *right; int value, fix, size; long long sum[5]; }; void Treap_Update(Treap_Node *&P) { P->size = 1; if (P->left) P->size += P->left->size; int pre = P->size; if (P->right) P->size += P->right->size; memset(P->sum, 0, sizeof(P->sum)); if (P->left) { for (int i = (0); i < (int)(5); ++i) { P->sum[i] += P->left->sum[i]; } } P->sum[pre % 5] += P->value; if (P->right) { for (int i = (0); i < (int)(5); ++i) { P->sum[(pre + i) % 5] += P->right->sum[i]; } } } void Treap_Left_Rotate(Treap_Node *&a) { Treap_Node *b = a->right; a->right = b->left; b->left = a; a = b; } void Treap_Right_Rotate(Treap_Node *&a) { Treap_Node *b = a->left; a->left = b->right; b->right = a; a = b; } void Treap_Insert(Treap_Node *&P, int value, int pos) { if (!P) { P = new Treap_Node; P->value = value; P->fix = rand(); P->size = 1; P->sum[1] = value; P->sum[0] = 0; P->left = NULL; P->right = NULL; } else if (((P->left) ? (P->left)->size : 0) >= pos) { Treap_Insert(P->left, value, pos); if (P->left->fix < P->fix) { Treap_Right_Rotate(P); Treap_Update(P->right); } Treap_Update(P); } else { Treap_Insert(P->right, value, pos - ((P->left) ? (P->left)->size : 0) - 1); if (P->right->fix < P->fix) { Treap_Left_Rotate(P); Treap_Update(P->left); } Treap_Update(P); } } void Treap_Insert(Treap_Node *&P, int value) { if (!P) { P = new Treap_Node; P->value = value; P->fix = rand(); P->size = 1; memset(P->sum, 0, sizeof(P->sum)); P->sum[1] = value; P->left = NULL; P->right = NULL; } else if (value <= P->value) { Treap_Insert(P->left, value); if (P->left->fix < P->fix) { Treap_Right_Rotate(P); Treap_Update(P->right); } Treap_Update(P); } else { Treap_Insert(P->right, value); if (P->right->fix < P->fix) { Treap_Left_Rotate(P); Treap_Update(P->left); } Treap_Update(P); } } void Treap_Delete(Treap_Node *&P, int value) { if (value == P->value) { if (!P->left || !P->right) { Treap_Node *t = P; P = (P->left ? P->left : P->right); delete t; } else { if (P->left->fix < P->right->fix) { Treap_Right_Rotate(P); Treap_Delete(P->right, value); } else { Treap_Left_Rotate(P); Treap_Delete(P->left, value); } } } else if (value < P->value) { Treap_Delete(P->left, value); } else { Treap_Delete(P->right, value); } if (P) Treap_Update(P); } void Treap_Split(Treap_Node *P, Treap_Node *&x, Treap_Node *&y, int a) { if (!a) { x = NULL; y = P; } else if (a == ((P) ? (P)->size : 0)) { x = P; y = NULL; } else { if (((P->left) ? (P->left)->size : 0) >= a) { y = P; Treap_Split(P->left, x, y->left, a); Treap_Update(y); } else { x = P; Treap_Split(P->right, x->right, y, a - ((P->left) ? (P->left)->size : 0) - 1); Treap_Update(x); } } } void Treap_Merge(Treap_Node *&P, Treap_Node *x, Treap_Node *y) { if (!x || !y) { P = x ? x : y; } else if (x->fix < y->fix) { Treap_Merge(x->right, x->right, y); Treap_Update(x); P = x; } else { Treap_Merge(y->left, x, y->left); Treap_Update(y); P = y; } } Treap_Node *root; void Revolve(int start, int end) { Treap_Node *head, *mid, *tail; Treap_Split(root, head, tail, end + 1); Treap_Split(head, head, mid, start); Treap_Merge(head, mid, head); Treap_Merge(root, head, tail); } void Treap_Init(Treap_Node *&P) { if (P == NULL) { return; } Treap_Init(P->left); Treap_Init(P->right); delete P; P = NULL; } int main() { Treap_Init(root); int n; cin >> n; root = NULL; for (int i = (0); i < (int)(n); ++i) { string s; int x; cin >> s; if (s[0] == 's') { if (!root) printf("0\n"); else printf("%I64d\n", root->sum[3]); continue; } cin >> x; if (s[0] == 'a') { Treap_Insert(root, x); } else if (s[0] == 'd') { Treap_Delete(root, x); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int M = 5; const int H = M / 2; const long long LINF = 1LL << 62; template <typename T, const int BUFSIZE> struct TreapMed { struct Node { T key, minkey, sums[M]; int fix, size; Node *left, *right; void print(int k) { printf("%d: key=%lld, minkey=%lld, fix=%d, size=%d: (", k, key, minkey, fix, size); for (int i = 0; i < M; i++) { if (i) putchar(','); printf("%lld", sums[i]); } putchar(')'); putchar('\n'); } void print() { print(0); } }; typedef Node* node; node root, nullnode, buf; Node buf0[BUFSIZE], buf1; const T TINF; TreapMed(T tinf) : TINF(tinf) { nullnode = &buf1; nullnode->left = nullnode->right = nullnode; nullnode->key = nullnode->minkey = TINF; nullnode->fix = nullnode->size = 0; memset(nullnode->sums, 0, sizeof(nullnode->sums)); clear(); srand(time(NULL)); } void clear() { buf = buf0; root = nullnode; } int size() { return root->size; } T medsum() { return root->sums[H]; } void update(node& t) { t->size = 1 + t->left->size + t->right->size; t->minkey = t->key; if (t->minkey > t->left->minkey) t->minkey = t->left->minkey; if (t->minkey > t->right->minkey) t->minkey = t->right->minkey; memcpy(t->sums, t->left->sums, sizeof(t->sums)); int sl = t->left->size % M; t->sums[sl++] += t->key; for (int i = 0; i < M; i++) t->sums[(i + sl) % M] += t->right->sums[i]; } node gen_node(T x) { node t = buf++; t->left = t->right = nullnode; t->key = t->minkey = x; t->fix = rand(); t->size = 1; memset(t->sums, 0, sizeof(t->sums)); t->sums[0] = x; return t; } void rot_l(node& k1) { node k2 = k1->right; k1->right = k2->left; k2->left = k1; update(k1); update(k2); k1 = k2; } void rot_r(node& k1) { node k2 = k1->left; k1->left = k2->right; k2->right = k1; update(k1); update(k2); k1 = k2; } void append(node& t, T x) { if (t == nullnode) t = gen_node(x); else { append(t->right, x); update(t); if (t->right->fix > t->fix) rot_l(t); } } void append(T x) { append(root, x); } void insertat(node& t, int i, T x) { if (t == nullnode) t = gen_node(x); else { int lsize = t->left->size; if (i == lsize) { append(t->left, x); update(t); if (t->left->fix > t->fix) rot_r(t); } else if (i < lsize) { insertat(t->left, i, x); update(t); if (t->left->fix > t->fix) rot_r(t); } else { insertat(t->right, i - (lsize + 1), x); update(t); if (t->right->fix > t->fix) rot_l(t); } } } void insertat(int i, T x) { insertat(root, i, x); } void insert(node& t, T x) { if (t == nullnode) t = gen_node(x); else { if (t->key == x) return; if (x < t->key) { insert(t->left, x); update(t); if (t->left->fix > t->fix) rot_r(t); } else { insert(t->right, x); update(t); if (t->right->fix > t->fix) rot_l(t); } } } void insert(T x) { insert(root, x); } void set(node& t, int i, T x) { if (t != nullnode) { int lsize = t->left->size; if (i == lsize) { t->key = t->minkey = x; update(t); } else if (i < lsize) { set(t->left, i, x); update(t); } else { set(t->right, i - (lsize + 1), x); update(t); } } } void set(int i, T x) { set(root, i, x); } void remove_node(node& t) { if (t == nullnode) return; if (t->left == nullnode || t->right == nullnode) { if (t->left == nullnode) t = t->right; else t = t->left; } else { if (t->left->fix < t->right->fix) { rot_l(t); remove_node(t->left); update(t); } else { rot_r(t); remove_node(t->right); update(t); } } } T removeat(node& t, int i) { T ret = TINF; if (t != nullnode) { int lsize = t->left->size; if (i == lsize) { ret = t->key; remove_node(t); } else if (i < lsize) { ret = removeat(t->left, i); update(t); } else { ret = removeat(t->right, i - (lsize + 1)); update(t); } } return ret; } T removeat(int i) { return removeat(root, i); } void remove(node& t, T x) { if (t != nullnode) { if (t->key == x) remove_node(t); else if (x < t->key) { remove(t->left, x); update(t); } else { remove(t->right, x); update(t); } } } void remove(T x) { remove(root, x); } int remove_range(node& t, int r0, int r1) { if (t == nullnode) return 0; if (r1 <= 0 || t->size <= r0) return 0; if (r0 <= 0 && t->size <= r1) { int n = t->size; t = nullnode; return n; } int lsize = t->left->size; int n = remove_range(t->left, r0, r1) + remove_range(t->right, r0 - (lsize + 1), r1 - (lsize + 1)); t->size = 1 + t->left->size + t->right->size; if (r0 <= lsize && lsize < r1) { n++; remove_node(t); } return n; } int remove_range(int r0, int r1) { return remove_range(root, r0, r1); } T min_range(node& t, int r0, int r1) { if (t == nullnode) return TINF; if (r1 <= 0 || t->size <= r0) return TINF; if (r0 <= 0 && t->size <= r1) return t->minkey; int lsize = t->left->size; T minval = TINF; if (r0 <= lsize && lsize < r1) minval = t->key; if (r0 <= lsize) { T minl = min_range(t->left, r0, r1); if (minval > minl) minval = minl; } if (lsize + 1 < r1) { T minr = min_range(t->right, r0 - (lsize + 1), r1 - (lsize + 1)); if (minval > minr) minval = minr; } return minval; } T min_range(int r0, int r1) { return min_range(root, r0, r1); } node find(T x) { node t = root; while (t != nullnode) { if (t->key == x) return t; if (x < t->key) t = t->left; else t = t->right; } return (node)NULL; } T findat(int i) { node t = root; while (t != nullnode) { int lsize = t->left->size; if (i == lsize) return t->key; if (i < lsize) t = t->left; else { i -= lsize + 1; t = t->right; } } return TINF; } int upper_bound(T x) { node t = root; int k = 0; while (t != nullnode) { if (t->key <= x) { k += t->left->size + 1; t = t->right; } else { t = t->left; } } return k; } void print(node t, int k, int indent) { if (t == nullnode) return; if (t != NULL) { for (int i = 0; i < indent; i++) cout << " "; t->print(k + t->left->size); print(t->left, k, indent + 1); print(t->right, k + t->left->size + 1, indent + 1); } } void print() { print(root, 0, 0); } void inorder(node t) { if (t != nullnode && t != NULL) { inorder(t->left); cout << t->key << ' '; inorder(t->right); } } void inorder() { inorder(root); cout << endl; } }; enum { ADD, DEL, SUM }; TreapMed<long long, MAX_N> trp(LINF); int main() { int n; scanf("%d", &n); while (n--) { char op[8]; scanf("%s", op); if (op[0] == 'a') { int x; scanf("%d", &x); trp.insert(x); } else if (op[0] == 'd') { int x; scanf("%d", &x); trp.remove(x); } else if (op[0] == 's') { printf("%lld\n", trp.medsum()); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 111; int n, val[N]; struct query { string type; int x; } q[N]; struct tree { vector<long long> prev; vector<int> cnt, lazy; vector<vector<long long> > sum; tree() { sum.assign(N * 4, vector<long long>(5, 0)); cnt.assign(N * 4, 0), lazy.assign(N * 4, 0); prev.assign(5, 0); } void down(int id) { for (int i = id * 2; i <= id * 2 + 1; ++i) { for (int j = 0; j < 5; ++j) { prev[j] = sum[i][j]; } for (int j = 0; j < 5; ++j) { int k = (j + lazy[id]) % 5; if (k < 0) k += 5; sum[i][j] = prev[k]; } lazy[i] += lazy[id]; } lazy[id] = 0; } void del(int id, int l, int r, int k) { if (l == r) { for (int i = 0; i < 5; ++i) sum[id][i] = 0; cnt[id] = 0; return; } down(id); int mid = (l + r) / 2; if (k <= mid) del(id * 2, l, mid, k); else del(id * 2 + 1, mid + 1, r, k); for (int i = 0; i < 5; ++i) sum[id][i] = sum[id * 2][i] + sum[id * 2 + 1][i]; cnt[id] = cnt[id * 2] + cnt[id * 2 + 1]; } void inc(int id, int l, int r, int k, int value, int pos) { if (l == r) { sum[id][pos] = value; cnt[id] = 1; return; } down(id); int mid = (l + r) / 2; if (k <= mid) inc(id * 2, l, mid, k, value, pos); else inc(id * 2 + 1, mid + 1, r, k, value, pos); for (int i = 0; i < 5; ++i) sum[id][i] = sum[id * 2][i] + sum[id * 2 + 1][i]; cnt[id] = cnt[id * 2] + cnt[id * 2 + 1]; } void upd(int id, int l, int r, int u, int v, int delta) { if (l > v || r < u) return; if (u <= l && r <= v) { for (int i = 0; i < 5; ++i) prev[i] = sum[id][i]; for (int i = 0; i < 5; ++i) { int k = (i + delta) % 5; if (k < 0) k += 5; sum[id][i] = prev[k]; } lazy[id] += delta; return; } down(id); int mid = (l + r) / 2; upd(id * 2, l, mid, u, v, delta); upd(id * 2 + 1, mid + 1, r, u, v, delta); for (int i = 0; i < 5; ++i) sum[id][i] = sum[id * 2][i] + sum[id * 2 + 1][i]; } int getCount(int id, int l, int r, int u, int v) { if (l > v || r < u) return 0; if (u <= l && r <= v) return cnt[id]; down(id); int mid = (l + r) / 2; int tmp = getCount(id * 2, l, mid, u, v); return tmp + getCount(id * 2 + 1, mid + 1, r, u, v); } } it; int m = 0; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> q[i].type; if (q[i].type != "sum") { cin >> q[i].x; val[++m] = q[i].x; } } sort(val + 1, val + m + 1); for (int i = 1; i <= n; ++i) { if (q[i].type == "add") { int k = lower_bound(val + 1, val + m + 1, q[i].x) - val; int cnt = it.getCount(1, 1, m, 1, k - 1); it.inc(1, 1, m, k, q[i].x, (cnt + 1) % 5); it.upd(1, 1, m, k + 1, m, -1); } else if (q[i].type == "del") { int k = lower_bound(val + 1, val + m + 1, q[i].x) - val; it.del(1, 1, m, k); it.upd(1, 1, m, k + 1, m, 1); } else { cout << it.sum[1][3] << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > q; vector<int> X; vector<int>::iterator it; int Q, s[100100 * 5]; long long f[100100 * 5][5]; void Update(int nd, int l, int r, int x) { if (l == r) { if (s[nd]) s[nd] = f[nd][0] = 0; else s[nd] = 1, f[nd][0] = X[x]; } else { int i, m = (l + r) / 2; if (x <= m) Update(nd * 2, l, m, x); else Update(nd * 2 + 1, m + 1, r, x); for (i = 0; i <= 4; i++) f[nd][i] = f[nd * 2][i] + f[nd * 2 + 1][(i + 5 - s[nd * 2]) % 5]; s[nd] = (s[nd * 2] + s[nd * 2 + 1]) % 5; } } int main() { int i, x; char s[11]; cin >> Q; for (i = 0; i <= Q - 1; i++) { scanf("%s", &s); if (s[0] == 's') q.push_back(make_pair(0, 0)); else { scanf("%d", &x); if (s[0] == 'a') q.push_back(make_pair(1, x)); else q.push_back(make_pair(-1, x)); X.push_back(x); } } sort(X.begin(), X.end()); it = unique(X.begin(), X.end()); X.resize(it - X.begin()); for (i = 0; i <= Q - 1; i++) if (q[i].first) { x = lower_bound(X.begin(), X.end(), q[i].second) - X.begin(); Update(1, 0, int(X.size()) - 1, x); } else printf("%lld\n", f[1][2]); return 0; }
#include <bits/stdc++.h> const int MAX_N = 1e5; struct Segt { int l, r; Segt *lc, *rc; int sum; long long a[5]; Segt(int l, int r, Segt *lc, Segt *rc) : l(l), r(r), lc(lc), rc(rc), sum(0) { memset(a, 0, sizeof(a)); } void update() { sum = lc->sum + rc->sum; memset(a, 0, sizeof(a)); int d = lc->sum % 5; for (int i = 0; i < 5; i++) { a[i] += lc->a[i]; a[i] += rc->a[(i - d + 5) % 5]; } } void add(int pos, int val) { if (l == r) { sum = 1; a[1] = val; } else { int mid = l + (r - l) / 2; if (pos <= mid) lc->add(pos, val); else if (pos > mid) rc->add(pos, val); update(); } } void del(int pos, int val) { if (l == r) { sum = 0; a[1] = 0; } else { int mid = l + (r - l) / 2; if (pos <= mid) lc->del(pos, val); else if (pos > mid) rc->del(pos, val); update(); } } static Segt *build(int l, int r) { if (l == r) return new Segt(l, r, NULL, NULL); else { int mid = l + (r - l) / 2; return new Segt(l, r, build(l, mid), build(mid + 1, r)); } } } * segt; struct Node { int l, r; int sum; long long a[5]; } a[MAX_N << 2]; void update(int rt) { a[rt].sum = a[rt << 1].sum + a[rt << 1 | 1].sum; memset(a[rt].a, 0, sizeof(a[rt].a)); int d = a[rt << 1].sum % 5; for (int i = 0; i < 5; i++) { a[rt].a[i] += a[rt << 1].a[i]; a[rt].a[i] += a[rt << 1 | 1].a[(i - d + 5) % 5]; } } void add(int rt, int pos, int val) { if (a[rt].l == a[rt].r) { a[rt].sum = 1; a[rt].a[1] = val; } else { int mid = a[rt].l + (a[rt].r - a[rt].l) / 2; if (pos <= mid) add(rt << 1, pos, val); else if (pos > mid) add(rt << 1 | 1, pos, val); update(rt); } } void del(int rt, int pos, int val) { if (a[rt].l == a[rt].r) { a[rt].sum = 0; a[rt].a[1] = 0; } else { int mid = a[rt].l + (a[rt].r - a[rt].l) / 2; if (pos <= mid) del(rt << 1, pos, val); else if (pos > mid) del(rt << 1 | 1, pos, val); update(rt); } } void build(int rt, int l, int r) { if (r < l) return; a[rt].l = l, a[rt].r = r; if (l == r) return; int mid = l + (r - l) / 2; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); } int n; struct Operation { int type, x; } opt[MAX_N + 1]; int tmp[MAX_N + 1], tot; std::map<int, int> mp; int main() { scanf("%d", &n); char s[10]; for (int i = 1; i <= n; i++) { scanf("%s", s); if (s[0] == 'a') { opt[i].type = 1; scanf("%d", &opt[i].x); tmp[++tot] = opt[i].x; } else if (s[0] == 'd') { opt[i].type = 2; scanf("%d", &opt[i].x); } else if (s[0] == 's') { opt[i].type = 3; } } std::sort(tmp + 1, tmp + tot + 1); tot = std::unique(tmp + 1, tmp + tot + 1) - tmp - 1; build(1, 1, tot); for (int i = 1; i <= tot; i++) mp[tmp[i]] = i; for (int i = 1; i <= n; i++) { if (opt[i].type == 1) { add(1, mp[opt[i].x], opt[i].x); } else if (opt[i].type == 2) { del(1, mp[opt[i].x], opt[i].x); } else if (opt[i].type == 3) { printf("%I64d\n", a[1].a[3]); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Treap { mt19937 ran; struct tree { int x, y, si; long long sum[5]; tree *l, *r; }; tree mem[100100]; int mpos; tree *nil, *root; Treap() { mpos = 1; nil = mem; root = nil; nil->l = nil->r = nil; } void Calc(tree *t) { if (t == nil) return; t->si = t->l->si + 1 + t->r->si; for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[i]; int sh = t->l->si; t->sum[sh % 5] += t->x; sh++; for (int i = 0; i < 5; i++) t->sum[(i + sh) % 5] += t->r->sum[i]; } tree *nn(int x) { tree *t = mem + mpos++; t->y = (ran() << 15) + ran(); t->x = x; t->l = t->r = nil; Calc(t); return t; } void split(tree *t, tree **l, tree **r, int x) { if (t == nil) *l = *r = nil; else if (t->x < x) *l = t, split(t->r, &t->r, r, x); else *r = t, split(t->l, l, &t->l, x); Calc(*l), Calc(*r); } void merge(tree **t, tree *l, tree *r) { if (l == nil || r == nil) { *t = max(l, r); return; } if (l->y < r->y) *t = l, merge(&(*t)->r, (*t)->r, r); else *t = r, merge(&(*t)->l, l, (*t)->l); Calc(*t); } void add(tree **t, int x) { tree *l, *r, *m; split(*t, &l, &r, x); merge(&m, l, nn(x)); merge(t, m, r); } void del(tree **t, int x) { tree *l, *m, *r, *tmp; split(*t, &l, &tmp, x); split(tmp, &m, &r, x + 1); merge(t, l, r); } void Add(int x) { add(&root, x); } void Del(int x) { del(&root, x); } } t; int main() { int n; scanf("%d", &n); while (n--) { char com[9]; int x; scanf("%s", com); if (!strcmp(com, "add")) { scanf("%d", &x); t.Add(x); } else if (!strcmp(com, "del")) { scanf("%d", &x); t.Del(x); } else printf("%I64d\n", t.root->sum[2]); } return 0; }
#include <bits/stdc++.h> const int N = 1e5 + 7; struct SplayTree { long long T[N], siz[N], sum[N][7]; int Rt, Ts, fa[N], ls[N][2]; void Up(int x) { siz[x] = siz[ls[x][0]] + siz[ls[x][1]] + 1; for (int i = 0; i < 5; ++i) sum[x][i] = sum[ls[x][0]][i] + sum[ls[x][1]][(i - (siz[ls[x][0]] + 1) % 5 + 5) % 5]; sum[x][(siz[ls[x][0]] + 1) % 5] += T[x]; } void Rotate(int x) { int y = fa[x], z = fa[y], s = ls[y][1] == x; fa[ls[z][ls[z][1] == y] = x] = z; fa[ls[y][s] = ls[x][s ^ 1]] = y; fa[ls[x][s ^ 1] = y] = x; Up(y); Up(x); } void Splay(int x) { for (; fa[x]; Rotate(x)) { if (fa[fa[x]]) if ((x == ls[fa[x]][0]) ^ (fa[x] == ls[fa[fa[x]]][0])) Rotate(x); else Rotate(fa[x]); } } void Ins(long long x) { T[++Ts] = x; int v = Rt; if (v) { for (int u = Rt; u = ls[u][T[u] < x]; v = u) ; fa[ls[v][T[v] < x] = Ts] = v; Splay(Rt = Ts); } else { Splay(Rt = Ts); } } int Next(int x) { if (!ls[x][0]) return 0; for (x = ls[x][0]; ls[x][1]; x = ls[x][1]) ; return x; } int Find(int v, long long x) { if (T[v] == x) return v; if (x < T[v]) return Find(ls[v][0], x); if (T[v] < x) return Find(ls[v][1], x); } void Del(long long x) { int v = Find(Rt, x), ne; Splay(Rt = v); if (ne = Next(v)) { fa[ls[ne][1] = ls[v][1]] = ne; fa[ls[v][0]] = 0; if (ls[v][1]) Splay(Rt = ls[v][1]); else Rt = ls[v][0]; } else { fa[ls[v][1]] = 0; if (ls[v][1]) Splay(Rt = ls[v][1]); else Rt = ls[v][0]; } } long long Sum() { return sum[Rt][3]; } } BBST; int main() { int n; scanf("%d", &n); for (char s[4]; n; --n) { scanf("\n%s", s); if (s[0] == 'a') { long long x; scanf("%I64d", &x); BBST.Ins(x); } else if (s[0] == 'd') { long long x; scanf("%I64d", &x); BBST.Del(x); } else if (s[0] == 's') { long long ans = BBST.Sum(); printf("%I64d\n", ans); } } return 0; }
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; long long bigmod(long long b, long long p, long long m) { if (p == 0) return 1; long long my = bigmod(b, p / 2, m); my *= my; my %= m; if (p & 1) my *= b, my %= m; return my; } int setb(int n, int pos) { return n = n | (1 << pos); } int resb(int n, int pos) { return n = n & ~(1 << pos); } bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); } struct query { int t, x; query() {} query(int tt, int xx) { t = tt; x = xx; } }; query q[100005]; map<int, int> mp; vector<int> v; int n; struct info { long long t, a[5]; void Print1() { printf("%lld:", t); for (int i = 0; i < 5; i++) printf(" %lld", a[i]); printf("\n"); } }; info tree[400005]; info Merge(info A, info B) { info ret; ret.t = A.t + B.t; for (int i = 0; i < 5; i++) { int baki = A.t % 5; int id = 5 - baki; id = (id + i) % 5; ret.a[i] = A.a[i] + B.a[id]; } return ret; } void U(int ind, int b, int e, int i, int x) { if (b == e) { if (x) tree[ind].t = 1; else tree[ind].t = 0; for (int i = 0; i < 5; i++) tree[ind].a[i] = 0; if (x) tree[ind].a[0] = v[b - 1]; return; } int mid = (b + e) / 2, l = 2 * ind, r = l + 1; if (i <= mid) U(l, b, mid, i, x); else U(r, mid + 1, e, i, x); tree[ind] = Merge(tree[l], tree[r]); } int main() { int x, t, i, j; char ins[10]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", ins); if (ins[0] == 'a') { t = 1; scanf("%d", &x); if (mp.find(x) == mp.end()) { mp[x] = 1; v.push_back(x); } } else if (ins[0] == 'd') { t = 2; scanf("%d", &x); } else { t = 3; x = 0; } q[i] = query(t, x); } sort(v.begin(), v.end()); for (i = 0; i < v.size(); i++) mp[v[i]] = i + 1; int N = v.size(); for (i = 1; i <= n; i++) { int t = q[i].t; int x = q[i].x; x = mp[x]; if (t == 1) U(1, 1, N, x, 1); if (t == 2) U(1, 1, N, x, 0); if (t == 3) { info my = tree[1]; long long ret = my.a[2]; printf("%lld", ret); printf("\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int inf = (1 << 31) - 1; const int hinf = 0x3f3f3f3f; const long long mod = 1000000007; struct node { int l, r, cnt; long long s[5]; } sgt[100010 << 2]; int n, k; void build(int l, int r, int root) { sgt[root].l = l; sgt[root].r = r; sgt[root].cnt = 0; memset(sgt[root].s, 0, sizeof(sgt[root].s)); if (l == r) return; int mid = (sgt[root].l + sgt[root].r) >> 1; build(l, mid, root << 1); build(mid + 1, r, root << 1 | 1); } void upd(int x, int root, long long num) { sgt[root].cnt += 2 * k - 1; if (sgt[root].l == sgt[root].r) { if (k == 1) sgt[root].s[1] = num; else sgt[root].s[1] = 0; return; } int mid = (sgt[root].l + sgt[root].r) >> 1; if (x <= mid) upd(x, root << 1, num); else upd(x, root << 1 | 1, num); for (int i = 0; i < 5; i++) { sgt[root].s[i] = 1ll * sgt[root << 1].s[i] + 1ll * sgt[root << 1 | 1].s[((i - sgt[root << 1].cnt) % 5 + 5) % 5]; } } char tmp[100010][20]; int d[100010]; int aa[100010]; int tot; int main() { while (scanf("%d", &n) != EOF) { int x; tot = 0; for (int i = 1; i <= n; i++) { scanf("%s", tmp[i]); if (tmp[i][0] != 's') { scanf("%d", &d[i]); aa[tot++] = d[i]; } } sort(aa, aa + tot); tot = unique(aa, aa + tot) - aa; if (!tot) { cout << 0 << endl; continue; } build(1, tot, 1); for (int i = 1; i <= n; i++) { if (tmp[i][0] == 's') cout << sgt[1].s[3] << endl; else if (tmp[i][0] == 'a') { k = 1; int pos = lower_bound(aa, aa + tot, d[i]) - aa; upd(pos + 1, 1, 1ll * d[i]); } else if (tmp[i][0] == 'd') { k = 0; int pos = lower_bound(aa, aa + tot, d[i]) - aa; upd(pos + 1, 1, 1ll * d[i]); } } } }
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; const long long N = 200017; vector<int> v; long long get_sum() { long long sum = 0; for (int i = 2; i < v.size(); i += 5) sum += (v[i] * 1ll); return sum; } void solve() { int q; cin >> q; while (q--) { string type; cin >> type; if (type == "add") { int x; cin >> x; v.insert(lower_bound(v.begin(), v.end(), x), x); } else if (type == "del") { int x; cin >> x; v.erase(lower_bound(v.begin(), v.end(), x)); } else { cout << get_sum() << "\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t24 = 1; while (t24--) { solve(); cout << "\n"; } }
#include <bits/stdc++.h> using namespace std; vector<int> v; char s[9]; int x; int main() { int n; cin >> n; while (n--) { scanf("%s", s); if (s[0] == 'a') { int x; scanf("%d", &x); v.insert(lower_bound(v.begin(), v.end(), x), x); } else if (s[0] == 'd') { int x; scanf("%d", &x); v.erase(lower_bound(v.begin(), v.end(), x)); } else { long long sum = 0; int l = v.size(); for (int i = 2; i < l; i += 5) sum += v[i]; printf("%I64d\n", sum); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long d[300015], q, n, x[100005], y[100005], u[100005], p[6][300005], w[300005]; map<long long, long long> m; int main() { for (long long i = 1; i <= 300003; i += 5) d[i] = 1, d[i + 1] = 2, d[i + 2] = 3, d[i + 3] = 4; scanf("%I64d", &q); for (long long i = 1; i <= q; i++) { char c[10]; scanf("%s", c); if (c[0] == 's') continue; x[i] = (c[0] == 'a' ? 1 : 2); scanf("%I64d", y + i), u[i] = y[i]; } sort(u + 1, u + q + 1); for (long long i = 1; i <= q; i++) if (u[i] != u[i - 1] && u[i]) m[u[i]] = ++n; for (long long i = 1; i <= q; i++) { if (x[i]) { long long t = m[y[i]] + 131071; w[t] ^= 1; p[0][t] = (x[i] & 1) ? y[i] : 0; for (long long j = t >> 1; j >= 1; j >>= 1) { long long l = j << 1, r = l ^ 1; w[j] = w[l] + w[r]; for (long long z = 0; z <= 4; z++) p[z][j] = p[z][l] + p[d[z + 10 - d[w[l]]]][r]; } } else printf("%I64d\n", p[2][1]); } return 0; }
#include <bits/stdc++.h> using namespace std; long long sum[(1 << 17) * 2][5]; int cnt[(1 << 17) * 2]; vector<pair<int, int> > query; map<int, int> mp; void func(int x) { int l = x * 2, r = x * 2 + 1; cnt[x] = cnt[l] + cnt[r]; for (int i = 0; i < 5; i++) sum[x][i] = sum[l][i] + sum[r][(i - cnt[l] % 5 + 5) % 5]; } int main() { int N = 0, x, j, Q; scanf("%d", &Q); char buf[3]; for (int i = 0; i < Q; i++) { scanf("%s", &buf); if (buf[0] == 's') query.push_back(make_pair(2, 0)); else { scanf("%d", &x); query.push_back(make_pair((buf[0] == 'a' ? 0 : 1), x)); mp[x] = 0; } } for (__typeof((mp).begin()) itr = (mp).begin(); itr != (mp).end(); itr++) { itr->second = N; N++; } for (int i = 0; i < Q; i++) { int type = query[i].first, x = query[i].second; if (type == 2) printf("%I64d\n", sum[1][2]); else { int id = mp[x] + (1 << 17); cnt[id] = 1 - type; sum[id][0] = (1 - type) * x; for (j = id / 2; j >= 1; j /= 2) func(j); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long h[100010], dat[100010], s[100010][5]; int n, root, tot, l[100010], r[100010], sum[100010]; bool t; inline void update(int x) { if (!x) return; int ss = sum[l[x]] + 1; for (int i = 0; i < 5; i++) s[x][i] = (long long)(s[l[x]][i] + s[r[x]][(i - ss % 5 + 5) % 5]); s[x][ss % 5] += (long long)dat[x]; sum[x] = ss + sum[r[x]]; h[x] = max(h[l[x]], h[r[x]]) + 1; } inline void zig(int &x) { int y = l[x]; l[x] = r[y]; r[y] = x; update(x); update(y); x = y; } inline void zag(int &x) { int y = r[x]; r[x] = l[y]; l[y] = x; update(x); update(y); x = y; } inline void balance(int &x) { if (h[l[x]] > h[r[x]] + 1) { if (h[l[l[x]]] < h[l[r[x]]]) zag(l[x]); zig(x); t = true; } if (h[r[x]] > h[l[x]] + 1) { if (h[r[r[x]]] < h[r[l[x]]]) zig(r[x]); zag(x); t = true; } } inline void ins(int &x, long long v) { if (!x) { dat[x = ++tot] = v; l[x] = r[x] = 0; } else ins(v < dat[x] ? l[x] : r[x], v); if (!t) balance(x); update(x); } inline void del(int &x, long long v) { if ((!l[x]) && (!r[x])) { dat[x] = 0; x = 0; return; } if (dat[x] == v) { if (h[l[x]] > h[r[x]]) { zig(x); del(r[x], v); } else { zag(x); del(l[x], v); } } else del(v < dat[x] ? l[x] : r[x], v); update(x); } int main() { scanf("%d\n", &n); memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); memset(s, 0, sizeof(s)); memset(h, 0, sizeof(h)); memset(dat, 0, sizeof(dat)); memset(sum, 0, sizeof(sum)); root = 1; tot = 0; h[0] = -1; while (n--) { char x[10]; scanf("%s", &x); char y = x[0]; long long z; if (y == 'a') { scanf("%I64d", &z); t = false; ins(root, z); } else if (y == 'd') { scanf("%I64d", &z); del(root, z); } else printf("%I64d\n", s[root][3]); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, q; string s; const int maxn = 1e5 + 5; long long a[maxn]; struct opt { long long x, op; } p[maxn]; struct tree { int l, r, cnt; long long sum[5]; } tr[maxn << 2]; void push_up(int now) { tr[now].cnt = tr[now << 1].cnt + tr[now << 1 | 1].cnt; for (int i = 0; i < 5; ++i) tr[now].sum[i] = 0; for (int i = 0; i < 5; ++i) { tr[now].sum[i] += tr[now << 1].sum[i]; tr[now].sum[(i + tr[now << 1].cnt) % 5] += tr[now << 1 | 1].sum[i]; } return; } void build(int now, int l, int r) { tr[now].l = l; tr[now].r = r; if (l == r) { for (int i = 0; i < 5; ++i) tr[now].sum[i] = 0; tr[now].cnt = 0; return; } int mid = (l + r) >> 1; build(now << 1, l, mid); build(now << 1 | 1, mid + 1, r); push_up(now); return; } void update(int now, int pos) { if (tr[now].l == tr[now].r) { tr[now].cnt = (tr[now].cnt + 1) % 2; if (tr[now].cnt) tr[now].sum[1] = a[tr[now].l]; else tr[now].sum[1] = 0; return; } int mid = (tr[now].l + tr[now].r) >> 1; if (pos <= mid) update(now << 1, pos); else update(now << 1 | 1, pos); push_up(now); return; } int cnt = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> q; cnt = 1; for (int i = 0; i < q; ++i) { cin >> s; if (s[0] == 's') p[i].op = 3; else { cin >> p[i].x; if (s[0] == 'a') p[i].op = 1; else p[i].op = 2; } a[cnt++] = p[i].x; } sort(a + 1, a + cnt + 1); int m = unique(a + 1, a + cnt + 1) - (a + 1); build(1, 1, m); for (int i = 0; i < q; ++i) { if (p[i].op == 3) cout << tr[1].sum[3] << "\n"; else { int xp = upper_bound(a + 1, a + m + 1, p[i].x) - (a + 1); update(1, xp); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, x, y, rt = 0, sz, lst = 22, mod = 1e9 + 7, e = 123456789; string st; struct treap { long long sz, zhi, fix, lch, rch, sum[5]; } tr[100005]; void getsum(long long &x) { if (x == 0) return; tr[0].sz = 0; tr[x].sz = tr[tr[x].lch].sz + tr[tr[x].rch].sz + 1; long long i, j; for (i = 0; i <= 4; i++) tr[x].sum[i] = tr[tr[x].lch].sum[i]; j = tr[tr[x].lch].sz % 5; tr[x].sum[j] += tr[x].zhi; j++; j %= 5; for (i = 0; i <= 4; i++, j++, j %= 5) { tr[x].sum[j] += tr[tr[x].rch].sum[i]; } } void tor(long long &x) { long long tmp = x; x = tr[x].lch; tr[tmp].lch = tr[x].rch; tr[x].rch = tmp; getsum(tmp); getsum(x); } void tol(long long &x) { long long tmp = x; x = tr[x].rch; tr[tmp].rch = tr[x].lch; tr[x].lch = tmp; getsum(tmp); getsum(x); } void insert(long long &x, long long zhi) { if (tr[x].zhi == zhi) return; if (!x) { x = ++sz; tr[x].sz = 1; memset(tr[x].sum, 0, sizeof(tr[x].sum)); tr[x].zhi = tr[x].sum[0] = zhi; tr[x].fix = lst = lst * e % mod; tr[x].lch = tr[x].rch = 0; return; } if (zhi < tr[x].zhi) { insert(tr[x].lch, zhi); if (tr[x].fix < tr[tr[x].lch].fix) tor(x); } else { insert(tr[x].rch, zhi); if (tr[x].fix < tr[tr[x].rch].fix) tol(x); } getsum(x); } void erase(long long &x, long long zhi) { if (!x) return; if (tr[x].zhi == zhi) { if (!(tr[x].lch || tr[x].rch)) { x = 0; return; } if (!tr[x].lch) { x = tr[x].rch; return; } if (!tr[x].rch) { x = tr[x].lch; return; } if (tr[tr[x].lch].fix > tr[tr[x].rch].fix) { tor(x); erase(x, zhi); return; } else { tol(x); erase(x, zhi); return; } } if (zhi < tr[x].zhi) erase(tr[x].lch, zhi); else erase(tr[x].rch, zhi); getsum(x); } long long query(long long &x) { return tr[x].sum[2]; } int main() { ios::sync_with_stdio(0); cin >> n; for (i = 1; i <= n; i++) { cin >> st; if (st == "add") { cin >> x; insert(rt, x); } if (st == "del") { cin >> x; erase(rt, x); } if (st == "sum") { cout << query(rt) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; struct node { long long sum[5]; int cnt; } nod[maxn << 2]; struct Node { int op; int v; } p[maxn]; char cmd[5]; int a[maxn]; int cnt; int get(int x) { int l = 0, r = cnt - 1, m; while (l <= r) { m = l + r >> 1; if (a[m] == x) return m + 1; else if (a[m] > x) { r = m - 1; } else l = m + 1; } } void pushup(int o) { int lson = o << 1; int rson = o << 1 | 1; nod[o].cnt = nod[lson].cnt + nod[rson].cnt; for (int i = 0; i < 5; ++i) { nod[o].sum[i] = nod[lson].sum[i]; } for (int i = 0; i < 5; ++i) { nod[o].sum[(i + nod[lson].cnt) % 5] += nod[rson].sum[i]; } } void update(int pos, int c, int l, int r, int o) { if (l == r) { if (c == 0) { memset(nod[o].sum, 0, sizeof(nod[o].sum)); nod[o].cnt = 0; } else { nod[o].cnt = 1; memset(nod[o].sum, 0, sizeof(nod[o].sum)); nod[o].sum[1] = a[pos - 1]; } return; } int m = l + r >> 1; if (m >= pos) { update(pos, c, l, m, o << 1); } else { update(pos, c, m + 1, r, o << 1 | 1); } pushup(o); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", cmd); if (cmd[0] == 'a') { p[i].op = 0; scanf("%d", &p[i].v); a[cnt++] = p[i].v; } else if (cmd[0] == 'd') { p[i].op = 1; scanf("%d", &p[i].v); a[cnt++] = p[i].v; } else { p[i].op = 2; } } sort(a, a + cnt); cnt = unique(a, a + cnt) - a; memset(nod, 0, sizeof nod); for (int i = 0; i < n; ++i) { if (p[i].op == 2) { printf("%I64d\n", nod[1].sum[3]); } else if (p[i].op == 0) { update(get(p[i].v), 1, 1, n, 1); } else { update(get(p[i].v), 0, 1, n, 1); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct seg { seg *lc, *rc; long long sum[5]; int fi, se, num; seg(int fi_, int se_) { fi = fi_; se = se_; sum[0] = sum[1] = sum[2] = sum[3] = sum[4] = num = 0; rc = lc = NULL; } inline void fix() { num = 0; int pre = 0; if (lc != NULL) { num = lc->num; pre = num; } if (rc != NULL) num += rc->num; for (int i = 0; i < 5; i++) { sum[i] = 0; if (lc != NULL) sum[i] = lc->sum[i]; if (rc != NULL) sum[i] += rc->sum[(i + pre) % 5]; } } inline void add(int x) { if (fi == se) { sum[0] = x; num = 1; sum[1] = sum[2] = sum[3] = sum[4] = 0; return; } int mid = (fi + se) / 2; if (x <= mid) { if (lc == NULL) lc = new seg(fi, mid); lc->add(x); } else { if (rc == NULL) rc = new seg(mid + 1, se); rc->add(x); } fix(); } inline void del(int x) { if (fi == se) { num = 0; sum[0] = sum[1] = sum[2] = sum[3] = sum[4] = 0; return; } int mid = (fi + se) / 2; if (x <= mid) lc->del(x); else rc->del(x); fix(); } }; seg *mom = new seg(1, 1000000000); int main() { ios::sync_with_stdio(false); int n; cin >> n; while (n--) { string s; int x; cin >> s; if (s == "sum") cout << mom->sum[3] << "\n"; else { cin >> x; if (s == "add") mom->add(x); else mom->del(x); } } return 0; }
#include <bits/stdc++.h> using namespace std; char s[5]; int n; vector<int> a; int binary(int val, int len) { int l = 0, r = len - 1; while (l < r) { int mid = (l + r) >> 1; if (a[mid] < val) r = mid - 1; else l = mid + 1; } return l; } int main() { int len, val; vector<int>::iterator it; while (cin >> n) { len = 0; a.clear(); while (n--) { scanf("%s", s); if (s[0] == 's') { long long ans = 0; for (int i = 2; i < len; i += 5) ans += a[i]; cout << ans << endl; } else if (s[0] == 'a') { len++; scanf("%d", &val); it = lower_bound(a.begin(), a.end(), val); a.insert(it, val); } else { len--; scanf("%d", &val); it = lower_bound(a.begin(), a.end(), val); a.erase(it); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int N; char s[256]; vector<int> v; int main() { int x; long long r; while (scanf("%d", &N) != EOF) { v.clear(); while (N--) { r = 0; scanf("%s", s); if (s[0] == 's') { for (vector<int>::iterator it = v.begin() + 2; it < v.end(); it += 5) r += *it; cout << r << "\n"; } else { scanf("%d", &x); vector<int>::iterator it = lower_bound(v.begin(), v.end(), x); if (v.size() == 0) it = v.begin(); if (s[0] == 'a') v.insert(it, x); else v.erase(it); } } } return 0; }
#include <bits/stdc++.h> using namespace std; namespace io { static streambuf *inbuf = cin.rdbuf(); static streambuf *outbuf = cout.rdbuf(); char buf[1 << 21], *p1 = buf, *p2 = buf; inline char gc() { return (p1 == p2 && (p2 = (p1 = buf) + inbuf->sgetn(buf, 1 << 21), p1 == p2) ? EOF : *p1++); } inline void pc(const char x) { outbuf->sputc(x); } inline void ps(const char *x) { outbuf->sputn(x, sizeof(x)), pc(10); } inline int read() { register int _s = 0, _f = 1; register char _ch = gc(); for (; !isdigit(_ch); _ch = gc()) if (_ch == '-') _f = -1; for (; isdigit(_ch); _ch = gc()) _s = _s * 10 + _ch - '0'; return _s * _f; } template <typename T> inline void write(T _x1) { if (_x1 < 0) pc('-'); static char _sta[15]; int _p = 0; do { _sta[_p++] = _x1 % 10 ^ 48; _x1 /= 10; } while (_x1); while (_p--) pc(_sta[_p]); } string _s; inline string readstr() { _s.clear(); register char _ch = gc(); while (isspace(_ch)) _ch = gc(); for (; !isspace(_ch); _ch = gc()) _s += _ch; return _s; } inline void writestr(string _s) { for (unsigned i = 0; i < _s.size(); ++i) pc(_s[i]); } inline void writeln(int _x) { write(_x); pc(10); } inline void writesp(int _x) { write(_x); pc(' '); } } // namespace io using namespace io; int n; vector<int> a; int main() { n = read(); while (n--) { string s = readstr(); int x; if (s != "sum") x = read(); if (s == "add") a.insert((upper_bound(a.begin(), a.end(), x)), x); if (s == "del") a.erase((lower_bound(a.begin(), a.end(), x))); if (s == "sum") { long long res = 0; for (unsigned i = 2; i < a.size(); i += 5) res += 1ll * a[i]; printf("%lld\n", res); } } return 0; }
#include <bits/stdc++.h> const int N = 100010; const long long INF = 1ll << 60; const int inf = 0x3f3f3f3f; class Discretization { public: Discretization() { vec_.clear(); } void Init() { vec_.clear(); } void Add(int x) { vec_.push_back(x); } void Build() { std::sort(vec_.begin(), vec_.end()); vec_.erase(std::unique(vec_.begin(), vec_.end()), vec_.end()); } int Id(int x) { auto y = std::lower_bound(vec_.begin(), vec_.end(), x); if (y == vec_.end() || *y != x) { return -1; } else { return y - vec_.begin() + 1; } } int IdLeq(int x) { auto y = std::upper_bound(vec_.begin(), vec_.end(), x); if (y == vec_.begin()) { return 0; } else { --y; return y - vec_.begin() + 1; } } int IdGeq(int x) { auto y = std::lower_bound(vec_.begin(), vec_.end(), x); if (y == vec_.end()) { return vec_.size() + 1; } else { return y - vec_.begin() + 1; } } int Size() { return vec_.size(); } int Value(int id) { return vec_[id - 1]; } private: std::vector<int> vec_; } disc; class SegmentTree { public: void Init(int n) { n_ = n; Build(1, n, 1); } void Change(int l, int r, int rt, int id, bool flag) { if (l == r) { if (flag) { sum[rt][1] = disc.Value(l); num[rt] = 1; } else { sum[rt][1] = 0ll; num[rt] = 0; } return; } int m = (l + r) >> 1; if (id <= m) { Change(l, m, rt << 1, id, flag); } else { Change(m + 1, r, rt << 1 | 1, id, flag); } Up(rt); } long long Query() { return sum[1][3]; } private: void Build(int l, int r, int rt) { num[rt] = 0; memset(sum[rt], 0ll, sizeof(sum[rt])); if (l == r) { return; } int m = (l + r) >> 1; Build(l, m, rt << 1); Build(m + 1, r, rt << 1 | 1); } void Up(int rt) { for (int i = 0; i < 5; ++i) { int id = (num[rt << 1] + i) % 5; sum[rt][id] = sum[rt << 1][id] + sum[rt << 1 | 1][i]; } num[rt] = (num[rt << 1] + num[rt << 1 | 1]) % 5; } int n_; long long sum[N << 2 | 1][5]; int num[N << 2 | 1]; } seg; int t; int n, m; int a[N]; int b[N]; char s[N][10]; int main() { scanf("%d", &n); disc.Init(); for (int i = 1; i <= n; ++i) { scanf("%s", s[i]); if (s[i][0] != 's') { scanf("%d", &a[i]); disc.Add(a[i]); } } disc.Build(); m = disc.Size(); if (m) { seg.Init(m); } for (int i = 1; i <= n; ++i) { if (s[i][0] == 'a') { seg.Change(1, m, 1, disc.Id(a[i]), 1); } else if (s[i][0] == 'd') { seg.Change(1, m, 1, disc.Id(a[i]), 0); } else { printf("%I64d\n", m ? seg.Query() : 0ll); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int maxn = 1e5 + 10; const int maxv = 1e3 + 10; const double eps = 1e-9; struct Question { char s[5]; long long num; void read() { scanf("%s", s); if (s[0] != 's') { scanf("%lld", &num); } } } q[maxn]; vector<long long> xs; long long sum[maxn << 2][5]; long long cnt[maxn << 2]; int n; int tot; void up(int rt) { cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1]; int offset = cnt[rt << 1]; for (int i = 0; i < 5; i++) sum[rt][i] = sum[rt << 1][i]; for (int i = 0; i < 5; i++) sum[rt][(i + offset) % 5] += sum[rt << 1 | 1][i]; } void updata(int rt, int l, int r, int id, long long val) { if (l == r) { sum[rt][0] = val * xs[id]; cnt[rt] = val; return; } int mid = l + r >> 1; if (id <= mid) updata(rt << 1, l, mid, id, val); else updata(rt << 1 | 1, mid + 1, r, id, val); up(rt); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { q[i].read(); if (q[i].s[0] != 's') { xs.push_back(q[i].num); } } sort(xs.begin(), xs.end()); xs.resize(unique(xs.begin(), xs.end()) - xs.begin()); tot = xs.size() - 1; for (int i = 0; i < n; i++) { if (q[i].s[0] == 's') { printf("%lld\n", sum[1][2]); } else if (q[i].s[0] == 'a') { int id = lower_bound(xs.begin(), xs.end(), q[i].num) - xs.begin(); updata(1, 0, tot, id, 1); } else { int id = lower_bound(xs.begin(), xs.end(), q[i].num) - xs.begin(); updata(1, 0, tot, id, 0); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct SEG { int l, r, cnt; long long s[5]; SEG() {} SEG(int _l, int _r) { l = _l, r = _r, cnt = 0; memset(s, 0, sizeof s); } } SGT[555555]; void build(int id, int l, int r) { SGT[id] = SEG(l, r); if (l == r) return; int mid = (l + r) / 2; build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r); } void fresh(SEG &T, SEG &L, SEG &R) { int i, zz = L.cnt % 5; T.cnt = L.cnt + R.cnt; for (i = 0; i < 5; i++) T.s[i] = L.s[i]; for (i = 0; i < 5; i++) T.s[(i + zz) % 5] += R.s[i]; } void pushadd(int id, int x, long long add) { SEG &T = SGT[id]; int mid = (T.l + T.r) / 2; T.cnt++; if (mid == T.r) { T.s[0] = add; return; } SEG &L = SGT[id * 2]; SEG &R = SGT[id * 2 + 1]; if (x <= mid) pushadd(id * 2, x, add); else pushadd(id * 2 + 1, x, add); fresh(T, L, R); } void pushdel(int id, int x) { SEG &T = SGT[id]; int mid = (T.l + T.r) / 2; T.cnt--; if (mid == T.r) { T.s[0] = 0; return; } SEG &L = SGT[id * 2]; SEG &R = SGT[id * 2 + 1]; if (x <= mid) pushdel(id * 2, x); else pushdel(id * 2 + 1, x); fresh(T, L, R); } int main() { int i, j; int n; while (~scanf("%d", &n)) { vector<int> Z; vector<pair<int, int> > Q; for (i = 1; i <= n; i++) { char s[5]; int x; scanf("%s", s); if (s[0] == 'a') { scanf("%d", &x); Q.push_back(make_pair(1, x)); Z.push_back(x); } else if (s[0] == 'd') { scanf("%d", &x); Q.push_back(make_pair(-1, x)); Z.push_back(x); } else Q.push_back(make_pair(0, 0)); } sort(Z.begin(), Z.end()); Z.erase(unique(Z.begin(), Z.end()), Z.end()); build(1, 0, Z.size()); for (i = 0; i < Q.size(); i++) { if (Q[i].first > 0) pushadd(1, lower_bound(Z.begin(), Z.end(), Q[i].second) - Z.begin(), Q[i].second); else if (Q[i].first < 0) pushdel(1, lower_bound(Z.begin(), Z.end(), Q[i].second) - Z.begin()); else printf("%I64d\n", SGT[1].s[2]); } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, sz; vector<int> dat; inline void add(int tmp) { int pos = lower_bound(dat.begin(), dat.end(), tmp) - dat.begin(); dat.insert(dat.begin() + pos, tmp); ++sz; } inline void del(int tmp) { int pos = lower_bound(dat.begin(), dat.end(), tmp) - dat.begin(); dat.erase(dat.begin() + pos); --sz; } long long sum() { long long res = 0; for (int i = 0; i < sz; i += 5) { if (i + 2 >= sz) continue; res += dat[i + 2]; } return res; } int main() { ios_base::sync_with_stdio(0); ; cin >> n; for (int i = 0; i < n; ++i) { string cmd; cin >> cmd; if (cmd == "add") { int tmp; cin >> tmp; add(tmp); } else if (cmd == "del") { int tmp; cin >> tmp; del(tmp); } else { cout << sum() << endl; } } }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1000000007; const double pi = 3.1415926535897932, eps = 1e-6; void chmax(int &x, int y) { if (x < y) x = y; } void chmin(int &x, int y) { if (x > y) x = y; } int ran() { static int x = 1364684679; x += (x << 2) + 1; return x; } struct node { int k, w, sz; long long dat[5]; node *l, *r; static node *getnew(node *w = NULL) { static node *list = NULL; if (w) { w->r = list; list = w; return NULL; } if (!list) { node *q = new node[10000]; for (int i = 0; i < 10000; i++) { q[i].w = ran(); q[i].r = list; list = q + i; } } node *p = list; list = list->r; p->l = p->r = NULL; return p; } void down() {} void update() { for (int(i) = (0); (i) <= (4); (i)++) dat[i] = 0; sz = 1; if (l) { sz += l->sz; for (int(i) = (0); (i) <= (4); (i)++) dat[i] += l->dat[i]; } dat[(sz + 4) % 5] += k; if (r) { for (int(i) = (0); (i) <= (4); (i)++) dat[(sz + i) % 5] += r->dat[i]; sz += r->sz; } } }; struct Treap { node *root; Treap() { root = NULL; } static void merge(node *&p, node *x, node *y) { if (!x || !y) { p = x ? x : y; } else if (x->w < y->w) { x->down(); merge(x->r, x->r, y); x->update(); p = x; } else { y->down(); merge(y->l, x, y->l); y->update(); p = y; } } static void cut(node *p, node *&x, node *&y, int a) { if (!p) { x = y = NULL; return; } if (p->k >= a) { y = p; cut(p->l, x, y->l, a); y->update(); } else { x = p; cut(p->r, x->r, y, a); x->update(); } } void del(node *&p, int a) { node *q, *r, *s; cut(p, q, r, a); cut(r, s, r, a + 1); merge(p, q, r); } void del(int a) { del(root, a); } void ins(node *&p, int a) { node *q, *r, *s; s = node::getnew(); s->dat[0] = s->k = a; s->sz = 1; cut(p, q, r, a); merge(q, q, s); merge(p, q, r); } void ins(int a) { ins(root, a); } } T; int n, m, len; int main() { ios::sync_with_stdio(false); cin >> n; for (int(i) = (1); (i) <= (n); (i)++) { string s; int k; cin >> s; if (s[0] == 's') { if (T.root == NULL) cout << 0 << '\n'; else cout << T.root->dat[2] << '\n'; } else if (s[0] == 'a') { cin >> k; T.ins(k); cout << endl; } else { cin >> k; T.del(k); } } return 0; }
#include <bits/stdc++.h> using namespace std; char s[9]; vector<int> a; int n, x; long long res; int main() { cin >> n; for (int i = (1), _b = (n); i <= _b; i++) { cin >> (s + 1); if (s[1] == 's') { res = 0; for (int i = 2; i < a.size(); i += 5) res += a[i]; cout << res << "\n"; } else { cin >> x; if (s[1] == 'a') a.insert(lower_bound(a.begin(), a.end(), x), x); else a.erase(lower_bound(a.begin(), a.end(), x)); } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int pre[MAXN], ch[MAXN][2], key[MAXN], ss[MAXN], sz[MAXN]; long long dp[MAXN][5]; int tot1, tot2; int root; vector<int> buf; void push_down(int r) { if (!r) return; } void push_up(int x) { int l = ch[x][0], r = ch[x][1]; sz[x] = sz[l] + sz[r] + 1; int lsz = sz[l] + 1; for (int i = 0; i < 5; i++) { dp[x][i] = dp[l][i]; } dp[x][(sz[l]) % 5] += key[x]; if (r) { for (int i = 0; i < 5; i++) { dp[x][(i + lsz) % 5] += dp[r][i]; } } } void rotate(int x, int d) { const int y = pre[x]; ch[y][!d] = ch[x][d]; if (ch[x][d]) pre[ch[x][d]] = y; pre[x] = pre[y]; if (ch[pre[y]][0] == y) ch[pre[x]][0] = x; else if (ch[pre[y]][1] == y) ch[pre[x]][1] = x; pre[y] = x; ch[x][d] = y; push_up(y); } bool _splay_parent(int x, int &y, int goal) { return (y = pre[x]) != goal && (ch[y][0] == x || ch[y][1] == x); } void splay(int x, int goal) { if (goal == 0) root = x; if (!x) return; for (int y, z; _splay_parent(x, y, goal);) { if (_splay_parent(y, z, goal)) { int d = y == ch[z][0]; if (x == ch[y][d]) rotate(x, d ^ 1), rotate(x, d); else rotate(y, d), rotate(x, d); } else { rotate(x, x == ch[y][0]); break; } } push_up(x); } int newnode(int x, int fa) { int r; tot1++; r = tot1; ch[r][0] = ch[r][1] = 0; key[r] = dp[r][0] = x; pre[r] = fa; for (int i = 1; i < 5; i++) dp[r][i] = 0; push_up(r); return r; } void insert(int cur, int x) { if (key[cur] > x) { if (ch[cur][0]) { insert(ch[cur][0], x); } else { ch[cur][0] = newnode(x, cur); } } else { if (ch[cur][1]) { insert(ch[cur][1], x); } else { ch[cur][1] = newnode(x, cur); } } push_up(cur); } void debug(int r) { if (r) { cout << key[r] << " : "; if (ch[r][0]) { cout << "Left Child " << key[ch[r][0]] << " "; } if (ch[r][1]) { cout << "Right Child " << key[ch[r][1]]; } cout << endl; if (ch[r][0]) { debug(ch[r][0]); } if (ch[r][1]) { debug(ch[r][1]); } } } void disp(int r) { cout << r << " : " << key[r] << " "; if (ch[r][0]) { cout << "Left Child " << key[ch[r][0]] << " "; } if (ch[r][1]) { cout << "Right Child " << key[ch[r][1]]; } cout << endl; } void remove(int cur, int x) { if (key[cur] == x) { if (ch[cur][0]) { int r = ch[cur][0]; pre[r] = pre[cur]; if (pre[cur]) { int fa = pre[cur]; ch[fa][ch[fa][1] == cur] = r; } while (ch[r][1]) r = ch[r][1]; if (ch[cur][1]) { pre[ch[cur][1]] = r; ch[r][1] = ch[cur][1]; } splay(r, 0); } else if (ch[cur][1]) { int r = ch[cur][1]; if (pre[cur]) { int fa = pre[cur]; ch[fa][ch[fa][1] == cur] = r; } pre[r] = pre[cur]; splay(r, 0); } else { if (pre[cur]) { int fa = pre[cur]; ch[fa][ch[fa][1] == cur] = 0; } splay(pre[cur], 0); } return; } else if (key[cur] < x) { remove(ch[cur][1], x); } else { remove(ch[cur][0], x); } } void dfs(int r) { if (r) { dfs(ch[r][0]); buf.push_back(key[r]); dfs(ch[r][1]); } } int rebuild(int x, int L, int R) { if (R < L) return 0; int mid = (L + R) >> 1; int ret = newnode(buf[mid], x); ch[ret][0] = rebuild(ret, L, mid - 1); ch[ret][1] = rebuild(ret, mid + 1, R); push_up(ret); return ret; } int main() { ios::sync_with_stdio(0); int t; scanf("%d", &t); char op[10]; int x; int opt = 0; while (t--) { scanf("%s", op); if (op[0] == 's') { cout << dp[root][2] << endl; } else if (op[0] == 'a') { scanf("%d", &x); if (root == 0) root = newnode(x, root); else insert(root, x); opt++; if (opt % 517 == 0 && root) { buf.clear(); dfs(root); root = 0; tot1 = 0; root = rebuild(0, 0, (int)buf.size() - 1); } } else { scanf("%d", &x); remove(root, x); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> vec; int n; string Coms[] = {"add", "del", "sum"}; bool flag = true; int main() { scanf("%d", &n); string com; int k; long long ans = 0; for (int i = 0; i < n; i++) { cin >> com; if (com == Coms[0]) { scanf("%d", &k); flag = false; vec.insert(lower_bound(vec.begin(), vec.end(), k), k); } else if (com == Coms[1]) { scanf("%d", &k); flag = false; vec.erase(lower_bound(vec.begin(), vec.end(), k)); } else { ans = 0; for (int j = 2; j < (int)vec.size(); j += 5) ans += vec[j]; printf("%lld\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Treap { struct tree { int x, y, si; long long sum[5]; tree *l, *r; }; tree mem[100100]; int mpos; tree *nil, *root; mt19937 ran; Treap() { mpos = 1; nil = mem; root = nil; nil->l = nil->r = nil; } void Calc(tree *t) { if (t == nil) return; t->si = t->l->si + 1 + t->r->si; for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[i]; int sh = t->l->si; t->sum[sh % 5] += t->x; sh++; for (int i = 0; i < 5; i++) t->sum[(i + sh) % 5] += t->r->sum[i]; } tree *nn(int x) { tree *t = mem + mpos++; t->y = (ran() << 15) + ran(); t->x = x; t->l = t->r = nil; Calc(t); return t; } void split(tree *t, tree **l, tree **r, int x) { if (t == nil) *l = *r = nil; else if (t->x < x) *l = t, split(t->r, &t->r, r, x); else *r = t, split(t->l, l, &t->l, x); Calc(*l), Calc(*r); } void merge(tree **t, tree *l, tree *r) { if (l == nil || r == nil) { *t = max(l, r); return; } if (l->y < r->y) *t = l, merge(&(*t)->r, (*t)->r, r); else *t = r, merge(&(*t)->l, l, (*t)->l); Calc(*t); } void add(tree **t, int x) { tree *l, *r, *m; split(*t, &l, &r, x); merge(&m, l, nn(x)); merge(t, m, r); } void del(tree **t, int x) { tree *l, *m, *r, *tmp; split(*t, &l, &tmp, x); split(tmp, &m, &r, x + 1); merge(t, l, r); } void Add(int x) { add(&root, x); } void Del(int x) { del(&root, x); } } t; int main() { int n; scanf("%d", &n); while (n--) { char com[9]; int x; scanf("%s", com); if (!strcmp(com, "add")) { scanf("%d", &x); t.Add(x); } else if (!strcmp(com, "del")) { scanf("%d", &x); t.Del(x); } else printf("%I64d\n", t.root->sum[2]); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; struct query { string type; long long val; } q[100005]; long long a[100005]; int cnt; struct Tree { int cnt; long long sum[10]; } tree[400005]; int get(int x) { int left = 0; int right = cnt; while (left <= right) { int mid = (left + right) >> 1; if (a[mid] == x) { return mid; } if (a[mid] > x) { right = mid - 1; } else { left = mid + 1; } } } void push_up(int node) { tree[node].cnt = tree[node * 2].cnt + tree[node * 2 + 1].cnt; for (int i = 0; i < 5; i++) { tree[node].sum[i] = tree[node * 2].sum[i]; } for (int i = 0; i < 5; i++) { tree[node].sum[(i + tree[node * 2].cnt) % 5] += tree[node * 2 + 1].sum[i]; } } void Add(int x, int l, int r, int node) { if (l == r) { tree[node].cnt = 1; memset(tree[node].sum, 0, sizeof(tree[node].sum)); tree[node].sum[1] = a[x - 1]; return; } int mid = (l + r) >> 1; if (mid >= x) { Add(x, l, mid, node * 2); } else { Add(x, mid + 1, r, node * 2 + 1); } push_up(node); } void Minu(int x, int l, int r, int node) { if (l == r) { tree[node].cnt = 0; memset(tree[node].sum, 0, sizeof(tree[node].sum)); return; } int mid = (l + r) >> 1; if (mid >= x) { Minu(x, l, mid, node * 2); } else { Minu(x, mid + 1, r, node * 2 + 1); } push_up(node); } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> q[i].type; if (q[i].type == "add" || q[i].type == "del") { cin >> q[i].val; a[cnt] = q[i].val; cnt++; } } cnt--; sort(a, a + cnt + 1); cnt = unique(a, a + cnt + 1) - a; for (int i = 1; i <= n; i++) { if (q[i].type == "add") { Add(get(q[i].val) + 1, 1, n, 1); } else if (q[i].type == "del") { Minu(get(q[i].val) + 1, 1, n, 1); } else { cout << tree[1].sum[3] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, x[100005], y[100005], c[100005 * 4], p, q, m; long long t[100005 * 4][5]; char o[100005][4]; void upd(int d, int l, int r) { c[d] += m * 2 - 1; if (l == r) { t[d][0] = m * y[q]; return; } if ((l + r >> 1) < q) upd(((d << 1) | 1), (l + r >> 1) + 1, r); else upd((d << 1), l, (l + r >> 1)); for (int i = (0); i < (5); i++) t[d][i] = t[(d << 1)][i] + t[((d << 1) | 1)][(i - c[(d << 1)] % 5 + 5) % 5]; } int main() { scanf("%d", &n); for (int i = (0); i < (n); i++) { scanf("%s", o[i]); if (o[i][0] == 'a' || o[i][0] == 'd') scanf("%d", &x[i]), y[p++] = x[i]; } sort(y, y + p); p = unique(y, y + p) - y; for (int i = (0); i < (n); i++) { q = lower_bound(y, y + p, x[i]) - y; if (o[i][0] == 'a') m = 1, upd(1, 1, p); else if (o[i][0] == 'd') m = 0, upd(1, 1, p); else printf("%I64d\n", t[1][2]); } }
#include <bits/stdc++.h> using namespace std; struct node { int x, y, n; long long s[5]; } T[100005 * 4]; int n, m, t, op[100005], oc[100005], a[100005]; char s[10]; map<int, int> A; void recal(int d) { for (int i = 0; i < 5; i++) T[d].s[i] = T[d << 1].s[i]; int r = T[d << 1].n % 5; for (int i = 0; i < 5; i++) { T[d].s[(i + r) % 5] += T[(d << 1) + 1].s[i]; } T[d].n = T[d << 1].n + T[(d << 1) + 1].n; } void build(int d, int x, int y) { T[d].x = x; T[d].y = y; if (x != y) { build(d * 2, x, (x + y) / 2); build(d * 2 + 1, (x + y) / 2 + 1, y); } else { T[d].n = 0; memset(T[d].s, 0, sizeof(T[d].s)); } } void upd(int d, int x, int v) { if (T[d].x == T[d].y) { if (v == 0) { T[d].n = 1; T[d].s[0] = a[x]; } else { T[d].n = 0; memset(T[d].s, 0, sizeof(T[d].s)); } } else { if (x <= T[d * 2].y) upd(d * 2, x, v); else upd(d * 2 + 1, x, v); recal(d); } } int main() { scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%s", s); if (s[0] == 's') oc[i] = 2; else { scanf("%d", &op[i]); if (s[0] == 'a') oc[i] = 0; if (s[0] == 'd') oc[i] = 1; a[++t] = op[i]; } } sort(a + 1, a + t + 1); n = 0; for (int i = 1; i <= t; i++) { if (a[i] != a[i - 1]) { a[++n] = a[i]; A[a[n]] = n; } } if (n >= 1) build(1, 1, n); for (int i = 0; i < m; i++) { if (oc[i] == 2) { if (n == 0) printf("0\n"); else printf("%I64d\n", T[1].s[2]); } else upd(1, A[op[i]], oc[i]); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; vector<int> vec; string type; int n, num; long long res; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; while (n--) { cin >> type; if (type[0] == 'a') { cin >> num; vec.insert(lower_bound(vec.begin(), vec.end(), num), num); } if (type[0] == 'd') { cin >> num; vec.erase(lower_bound(vec.begin(), vec.end(), num)); } if (type[0] == 's') { res = 0; for (int i = 2; i < vec.size(); i += 5) res += vec[i]; cout << res << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 Rand(time(nullptr)); const int INF = 1e9 + 7; struct tnode { int key, siz; long long sum; unsigned int fix; tnode *lson, *rson; tnode(int x = 0) { key = sum = x, siz = 1, fix = Rand(); lson = rson = nullptr; } static int Siz(tnode* x) { return x ? x->siz : 0; } static long long Sum(tnode* x) { return x ? x->sum : 0; } void maintain() { siz = Siz(lson) + Siz(rson) + 1; sum = Sum(lson) + Sum(rson) + key; } }; tnode *tre[5], *ltre[5], *rtre[5]; namespace Treap { void split(tnode* cur, tnode*& x, tnode*& y, int k) { if (!cur) x = y = nullptr; else if (k <= cur->key) y = cur, split(y->lson, x, y->lson, k), y->maintain(); else x = cur, split(x->rson, x->rson, y, k), x->maintain(); } void merge(tnode*& cur, tnode* x, tnode* y) { if (!x || !y) cur = x ? x : y; else if (x->fix < y->fix) merge(x->rson, x->rson, y), x->maintain(), cur = x; else merge(y->lson, x, y->lson), y->maintain(), cur = y; } int getmin(tnode* cur) { if (!cur) return INF; while (cur->lson) cur = cur->lson; return cur->key; } } // namespace Treap int N; int main() { scanf("%d", &N); for (int i = 0; i < (int)(5); ++i) tre[i] = nullptr; for (int i = 0; i < (int)(N); ++i) { string s; int x; cin >> s; if (s[0] == 's') { printf("%lld\n", tnode::Sum(tre[2])); continue; } scanf("%d", &x); for (int i = 0; i < (int)(5); ++i) Treap::split(tre[i], ltre[i], rtre[i], x); if (s[0] == 'a') { int pos = 0; for (int i = 0; i < (int)(5); ++i) pos += tnode::Siz(ltre[i]); for (int i = 0; i < (int)(5); ++i) if (pos % 5 == i) Treap::merge(ltre[i], ltre[i], new tnode(x)); for (int i = 0; i < (int)(5); ++i) Treap::merge(tre[i], ltre[i], rtre[(i + 4) % 5]); } else { for (int i = 0; i < (int)(5); ++i) if (Treap::getmin(rtre[i]) == x) { tnode* t; Treap::split(rtre[i], t, rtre[i], x + 1); delete t; } for (int i = 0; i < (int)(5); ++i) Treap::merge(tre[i], ltre[i], rtre[(i + 1) % 5]); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int num; long long sum[5]; node() {} }; vector<int> vals; struct segtree { int N; node nodes[270270]; void init(int N_) { N = 1; while (N < N_) N *= 2; } void query(int i, bool flg) { i += N; if (flg) { nodes[i].num = 1; nodes[i].sum[0] = vals[i - N]; } else { nodes[i].num = 0; nodes[i].sum[0] = 0; } while (i > 1) { i /= 2; nodes[i].num = nodes[i * 2].num + nodes[i * 2 + 1].num; for (int r = 0; r < 5; r++) { nodes[i].sum[r] = nodes[i * 2].sum[r]; } for (int r = 0; r < 5; r++) { nodes[i].sum[(r + nodes[i * 2].num) % 5] += nodes[i * 2 + 1].sum[r]; } } } long long get() { return nodes[1].sum[2]; } }; segtree seg; char ch[100100][10]; int val[100100]; int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%s", &ch[i]); if (ch[i][0] == 's') continue; else scanf("%d", val + i); vals.push_back(val[i]); } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); seg.init(vals.size()); for (int i = 0; i < N; i++) { if (ch[i][0] == 's') { cout << seg.get() << "\n"; continue; } int id = distance(vals.begin(), lower_bound(vals.begin(), vals.end(), val[i])); if (ch[i][0] == 'a') seg.query(id, true); else seg.query(id, false); } return 0; }
#include <bits/stdc++.h> using namespace std; int Num; char CH[20]; const int inf = 0x3f3f3f3f; inline long long read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void P(int x) { Num = 0; if (!x) { putchar('0'); puts(""); return; } while (x > 0) CH[++Num] = x % 10, x /= 10; while (Num) putchar(CH[Num--] + 48); puts(""); } vector<int> s; int main() { char a[10]; int b; int n = read(); for (int i = 0; i < n; i++) { scanf("%s", a); if (a[0] == 'a') { scanf("%d", &b); s.insert(lower_bound(s.begin(), s.end(), b), b); } if (a[0] == 'd') { scanf("%d", &b); s.erase(lower_bound(s.begin(), s.end(), b)); } if (a[0] == 's') { long long sum = 0; for (int j = 2; j < s.size(); j += 5) sum += s[j]; printf("%lld\n", sum); } } }
#include <bits/stdc++.h> using namespace std; template <class T> inline void smn(T &a, const T &b) { if (b < a) a = b; } template <class T> inline void smx(T &a, const T &b) { if (b > a) a = b; } template <class T> inline T rev(const T &a) { T _ = a; reverse(_.begin(), _.end()); return _; } const double eps = 1e-9; const long double leps = 1e-14; struct node { long long s[6]; int t; node() { s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = t = 0; } }; const int MN = 1000 * 100 + 100; node seg[(1 << 18) + 100]; map<int, int> mp; set<int> mark; set<int>::iterator it; vector<pair<int, int> > op; int n; void add(int s, int e, int n, const int &k, const int &v, const int &u) { if (s == e) { seg[n].t = u; seg[n].s[0] = v; for (int i = 1; i < 5; i++) seg[n].s[i] = 0; return; } int m = (s + e) / 2; if (k <= m) add(s, m, 2 * n, k, v, u); else add(m + 1, e, 2 * n + 1, k, v, u); seg[n].t = seg[2 * n].t + seg[2 * n + 1].t; for (int i = 0; i < 5; i++) { long long now = seg[2 * n].s[i]; int t = (i - seg[2 * n].t) % 5; if (t < 0) t += 5; now += seg[2 * n + 1].s[t]; seg[n].s[i] = now; } } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "sum") { op.push_back(pair<int, int>(2, -1)); continue; } int a; cin >> a; mark.insert(a); if (s == "add") op.push_back(pair<int, int>(0, a)); else op.push_back(pair<int, int>(1, a)); } it = mark.begin(); for (int i = 0; it != mark.end(); it++, i++) mp[*it] = i; for (int i = 0; i < n; i++) { if (op[i].first == 2) { cout << seg[1].s[2] << '\n'; continue; } if (op[i].first == 0) add(0, n + 10, 1, mp[op[i].second], op[i].second, 1); else add(0, n + 10, 1, mp[op[i].second], 0, 0); } return 0; }
#include <bits/stdc++.h> using namespace std; struct info { long long a[5]; int len; info() { len = 0; memset(a, 0, sizeof a); } info(int x) { len = 1; memset(a, 0, sizeof a); a[2] = x; } }; info operator+(const info &a, const info &b) { info ans; ans.len = a.len + b.len; for (int i = 0; i < 5; i++) { ans.a[i] = a.a[i] + b.a[(i + a.len) % 5]; } return ans; } struct node { node *l, *r; int x, y; info my, all; node() { l = r = NULL; y = (rand() << 16) + rand(); x = -1; }; node(int x) : x(x) { l = r = NULL; y = (rand() << 16) + rand(); all = my = info(x); } }; typedef node *pnode; void up(pnode root) { root->all = root->my; if (root->l) { root->all = root->l->all + root->all; } if (root->r) { root->all = root->all + root->r->all; } } pnode merge(pnode l, pnode r) { if (!l) return r; if (!r) return l; if (l->y > r->y) { l->r = merge(l->r, r); up(l); return l; } else { r->l = merge(l, r->l); up(r); return r; } } void split(pnode root, pnode &l, pnode &r, int x) { if (!root) { l = r = NULL; return; } if (root->x > x) { split(root->l, l, root->l, x); up(root); r = root; } else { split(root->r, root->r, r, x); up(root); l = root; } } void dfs(pnode root) { if (!root) return; dfs(root->l); cout << root->all.len << " "; dfs(root->r); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; pnode root = NULL; for (int i = 0; i < n; i++) { string t; cin >> t; if (t == "add") { int x; cin >> x; pnode l, r; split(root, l, r, x); root = merge(merge(l, new node(x)), r); } if (t == "del") { int x; cin >> x; pnode first, second, th; split(root, first, th, x); split(first, first, second, x - 1); root = merge(first, th); } if (t == "sum") { if (root) cout << root->all.a[0] << "\n"; else cout << 0 << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int cnt; long long sum[6]; }; node seg[4 * 100000]; struct info { string str; int num; }; info data[100000 + 20]; map<int, int> m; vector<int> v; string s; void insert(int idx, int st, int ed, int pos, int val) { if (st == ed) { if (val < 0) seg[idx].cnt = 0; else seg[idx].cnt = 1; seg[idx].sum[1] += val; return; } int lft, rgt, mid, var, i; mid = (st + ed) / 2; lft = 2 * idx; rgt = lft + 1; if (pos <= mid) insert(lft, st, mid, pos, val); else if (pos > mid) insert(rgt, mid + 1, ed, pos, val); seg[idx].cnt = seg[lft].cnt + seg[rgt].cnt; for (i = 1; i <= 5; i++) seg[idx].sum[i] = 0; for (i = 1; i <= 5; i++) { var = i % 5; if (var == 0) var = 5; seg[idx].sum[var] += seg[lft].sum[i]; } for (i = 1; i <= 5; i++) { var = (seg[lft].cnt + i) % 5; if (var == 0) var = 5; seg[idx].sum[var] += seg[rgt].sum[i]; } } int main() { int t, k, i, val, pos, sz; cin >> t; m.clear(); v.clear(); for (k = 1; k <= t; k++) { s.clear(); cin >> s; data[k].str = s; if (s == "sum") { data[k].num = 0; continue; } cin >> val; if (s == "add") v.push_back(val); data[k].num = val; } sort(v.begin(), v.end()); sz = v.size(); for (i = 0; i < sz; i++) m[v[i]] = i + 1; for (i = 1; i <= t; i++) { s.clear(); s = data[i].str; val = data[i].num; pos = m[val]; if (s == "add") insert(1, 1, sz, pos, val); else if (s == "del") insert(1, 1, sz, pos, -val); else if (s == "sum") cout << seg[1].sum[3] << endl; } }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long long LINF = 1000000000000000000LL; void prepare() {} struct item { int key, prior, cnt; long long sum[5]; item *l, *r; item() {} item(int key, int prior) : key(key), prior(prior), l(0), r(0) {} }; typedef item *pitem; inline int cnt(pitem t) { if (t) return t->cnt; else return 0; } void upd_cnt(pitem t) { if (t) t->cnt = 1 + cnt(t->l) + cnt(t->r); } void upd_sum(pitem t) { if (t) { memset(t->sum, 0, sizeof(t->sum)); int mv = 0; if (t->l) { mv = t->l->cnt; for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[i]; } mv %= 5; t->sum[mv] += t->key; mv = (mv + 1) % 5; if (t->r) { for (int i = 0; i < 5; i++) t->sum[(mv + i) % 5] += t->r->sum[i]; } } } void split(pitem t, int key, pitem &l, pitem &r) { if (!t) { l = r = 0; } else if (key < t->key) { split(t->l, key, l, t->l); r = t; } else { split(t->r, key, t->r, r); l = t; } upd_cnt(l); upd_cnt(r); upd_sum(l); upd_sum(r); } void insert(pitem &t, pitem it) { if (!t) { t = it; } else if (it->prior > t->prior) { split(t, it->key, it->l, it->r); t = it; } else { if (it->key < t->key) insert(t->l, it); else insert(t->r, it); } upd_cnt(t); upd_sum(t); } void merge(pitem &t, pitem l, pitem r) { if (!l || !r) { if (l) t = l; else t = r; } else if (l->prior > r->prior) { merge(l->r, l->r, r); t = l; } else { merge(r->l, l, r->l); t = r; } upd_cnt(t); upd_sum(t); } void erase(pitem &t, int key) { if (t->key == key) { merge(t, t->l, t->r); } else { if (key < t->key) erase(t->l, key); else erase(t->r, key); } upd_cnt(t); upd_sum(t); } pitem tree = 0; char tmp[100]; bool solve() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", tmp); if (tmp[0] == 'a') { int p; scanf("%d", &p); insert(tree, new item(p, rand())); } else if (tmp[0] == 'd') { int p; scanf("%d", &p); erase(tree, p); } else { if (!tree) printf("0\n"); else printf("%I64d\n", tree->sum[2]); } } return false; } int main() { prepare(); while (solve()) ; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fdelete-null-pointer-checks") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fhoist-adjacent-loads") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-frerun-cse-after-loop") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-ftree-tail-merge") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC target("avx") using namespace std; vector<int> seq; int n, m, i, j, x; string op; long long ans; int main() { scanf("%d", &n); while (n--) { scanf(" "); { char ss[3]; scanf("%s", ss); (op) = ss; }; if (op == "add") { scanf("%d", &x); seq.insert(upper_bound(seq.begin(), seq.end(), x), x); } if (op == "del") { scanf("%d", &x); seq.erase(lower_bound(seq.begin(), seq.end(), x)); } if (op == "sum") { ans = 0; for (i = 2; i < seq.size(); i += 5) ans += seq[i]; printf("%I64d\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long n; string cmd[110000], buf; vector<long long> V; long long v[110000]; long long getid(long long x) { return lower_bound(V.begin(), V.end(), x) - V.begin() + 1; } struct A { long long lazy, val[5]; } tr[440000]; void update(long long p, long long add, long long l, long long r, long long rt) { if (l == r) { tr[rt].lazy += add; for (long long i = 0; i < 5; i++) tr[rt].val[i] = 0; if (tr[rt].lazy) tr[rt].val[0] = V[p - 1]; return; } long long m = (l + r) / 2; if (p <= m) update(p, add, l, m, rt << 1); else update(p, add, m + 1, r, rt << 1 | 1); tr[rt].lazy = tr[rt << 1].lazy + tr[rt << 1 | 1].lazy; for (long long i = 0; i < 5; i++) tr[rt].val[i] = tr[rt << 1].val[i] + tr[rt << 1 | 1].val[(5 - tr[rt << 1].lazy % 5 + i) % 5]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (long long i = 0; i < n; i++) { cin >> cmd[i]; if (cmd[i] == "sum") continue; cin >> v[i]; if (cmd[i] == "add") V.push_back(v[i]); } sort(V.begin(), V.end()); for (long long i = 0; i < n; i++) { if (cmd[i] == "add") { update(getid(v[i]), 1, 1, V.size() * 2, 1); } else if (cmd[i] == "del") { update(getid(v[i]), -1, 1, V.size() * 2, 1); } else { cout << tr[1].val[2] << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; int q, i, x; string s; vector<int> v; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> q; while (q) { q--; cin >> s; if (s == "sum") { long long ans = 0; for (i = 2; i < v.size(); i += 5) ans += v[i]; cout << ans << '\n'; } else { cin >> x; auto p = lower_bound(v.begin(), v.end(), x); if (s == "add") v.insert(p, x); else v.erase(p); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct tree { int x, y, si; long long sum[5]; tree *l, *r; }; tree mem[100100]; int mpos = 1; tree *nil = mem; void Calc(tree *t) { if (t == nil) return; t->si = 1 + t->l->si + t->r->si; for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[i]; int sh = t->l->si; t->sum[sh % 5] += t->x; sh++; for (int i = 0; i < 5; i++) t->sum[(i + sh) % 5] += t->r->sum[i]; } tree *NewT(int x) { tree *t = mem + mpos++; t->y = (rand() << 15) + rand(); t->x = x; t->l = t->r = nil; Calc(t); return t; } void Split(tree *t, tree **l, tree **r, int x) { if (t == nil) *l = *r = nil; else if (t->x < x) *l = t, Split(t->r, &t->r, r, x); else *r = t, Split(t->l, l, &t->l, x); Calc(*l), Calc(*r); } void Merge(tree **t, tree *l, tree *r) { if (l == nil || r == nil) { *t = max(l, r); return; } if (l->y < r->y) *t = l, Merge(&(*t)->r, (*t)->r, r); else *t = r, Merge(&(*t)->l, l, (*t)->l); Calc(*t); } void Add(tree **t, int x) { tree *l, *r, *m; Split(*t, &l, &r, x); Merge(&m, l, NewT(x)); Merge(t, m, r); } void Del(tree **t, int x) { tree *l, *m, *r, *tmp; Split(*t, &l, &tmp, x); Split(tmp, &m, &r, x + 1); Merge(t, l, r); } int main() { nil->l = nil->r = nil; tree *root = nil; int n; scanf("%d", &n); srand(unsigned(time(NULL))); while (n--) { char com[9]; int x; scanf("%s", com); if (!strcmp(com, "add")) { scanf("%d", &x); Add(&root, x); } else if (!strcmp(com, "del")) { scanf("%d", &x); Del(&root, x); } else printf("%I64d\n", root->sum[2]); } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; template <class T> T sqr(T a) { return a * a; } struct node { int x; long long key, sum; node *left, *right; }; node pool[110000]; int pc; node *h[5]; node *lh[5]; node *rh[5]; void update(node *t) { if (!t) return; t->sum = t->key; if (t->left) t->sum += t->left->sum; if (t->right) t->sum += t->right->sum; } node *merge(node *t1, node *t2) { if (!t1) return t2; if (!t2) return t1; if (t1->x < t2->x) { t1->right = merge(t1->right, t2); update(t1); return t1; } else { t2->left = merge(t1, t2->left); update(t2); return t2; } } void split(node *t, node **l, node **r, int key) { if (!t) *l = *r = 0; else if (key < t->key) { split(t->left, l, &(t->left), key); *r = t; } else { split(t->right, &(t->right), r, key); *l = t; } update(t); } void add(node **t, node *pp) { if (!(*t)) *t = pp; else if (pp->x > (*t)->x) add(pp->key < (*t)->key ? &((*t)->left) : &((*t)->right), pp); else { split(*t, &(pp->left), &(pp->right), pp->key); *t = pp; } update(*t); } void remove(node **t, int key) { if (!(*t)) return; if ((*t)->key != key) remove(key < (*t)->key ? &((*t)->left) : &((*t)->right), key); else *t = merge((*t)->left, (*t)->right); update(*t); } long long getmax(node *t) { if (t->right) return max(t->key, getmax(t->right)); return t->key; } int main() { int q; cin >> q; node *p; for (int i = 0; i < q; i++) { string scmd; cin >> scmd; if (scmd == "add") { int arg; cin >> arg; int mi = 4, mv = -1; for (int i = 0; i < 5; i++) { split(h[i], &lh[i], &rh[i], arg); if (lh[i]) { int cur = getmax(lh[i]); if (cur > mv) { mi = i; mv = cur; } } } for (int i = 0; i < 5; i++) h[(i + 1) % 5] = merge(lh[(i + 1) % 5], rh[i]); node *pp = &pool[pc++]; pp->key = arg; pp->x = rand(); pp->sum = arg; add(&h[(mi + 1) % 5], pp); } else if (scmd == "del") { int arg; cin >> arg; for (int i = 0; i < 5; i++) remove(&h[i], arg); for (int i = 0; i < 5; i++) split(h[i], &lh[i], &rh[i], arg); for (int i = 0; i < 5; i++) h[i] = merge(lh[i], rh[(i + 1) % 5]); } else { if (h[2]) cout << h[2]->sum << endl; else cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; char oper[100005]; int oper_num[100005]; int a[100005]; map<int, int> ma; long long val[1 << 18][5]; int size[1 << 18]; void change(int num, int l, int r, int k, int t, int x) { if (l == r - 1) { val[num][0] += x * t; size[num] += x; return; } int mid = (l + r) / 2; if (k < mid) { change(num * 2 + 1, l, mid, k, t, x); } else { change(num * 2 + 2, mid, r, k, t, x); } size[num] = size[num * 2 + 1] + size[num * 2 + 2]; int i; for (i = 0; i < 5; i++) { val[num][i] = val[num * 2 + 1][i] + val[num * 2 + 2][((i - size[num * 2 + 1]) % 5 + 5) % 5]; } } long long query(int num, int l, int r, int l0, int r0) { return val[num][2]; } int main() { ios::sync_with_stdio(false); int n; scanf("%d", &n); int i; int t = 0; for (i = 0; i < n; i++) { static char opers[1005]; scanf("%s", opers); oper[i] = opers[0]; if (oper[i] != 's') { scanf("%d", &oper_num[i]); a[t++] = oper_num[i]; } } sort(a, a + t); t = unique(a, a + t) - a; for (i = 0; i < t; i++) { ma[a[i]] = i; } for (i = 0; i < n; i++) { if (oper[i] == 's') { cout << query(0, 0, n, 0, n) << '\n'; } else if (oper[i] == 'a') { change(0, 0, n, ma[oper_num[i]], oper_num[i], 1); } else { change(0, 0, n, ma[oper_num[i]], oper_num[i], -1); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10, SQ = 350, T = MAXN / SQ + 10; pair<int, int> qw[MAXN]; long long n, a, sum[T][5], cl[T]; map<int, int> mp; bool usd[MAXN]; vector<long long> A; string qt; void addnum(int pos, bool d) { int S = pos / SQ, num = 0; if (d) { usd[pos] = true; cl[S]++; } else { usd[pos] = false; cl[S]--; } for (int i = 0; i < S; i++) num += cl[i]; for (int i = S * SQ; i < pos; i++) num += usd[i]; num++; if (d) sum[S][num % 5] += A[pos]; else sum[S][num % 5] -= A[pos], num++; for (int i = pos + 1; i < min((int)A.size(), (S + 1) * SQ); i++) if (usd[i]) { sum[S][num % 5] -= A[i]; if (d) sum[S][(num + 1) % 5] += A[i]; else sum[S][(num - 1 + 5) % 5] += A[i]; num++; } for (int i = S + 1; i * SQ < A.size(); i++) { long long stmp[5]; for (int j = 0; j < 5; j++) stmp[j] = sum[i][j]; for (int j = 0; j < 5; j++) { if (d) sum[i][j] = stmp[(j - 1 + 5) % 5]; else sum[i][j] = stmp[(j + 1) % 5]; } } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> qt; if (qt == "add") { cin >> a; A.push_back(a); qw[i] = {0, a}; } else if (qt == "del") { cin >> a; qw[i] = {1, a}; } else qw[i] = {2, 0}; } sort(A.begin(), A.end()); for (int i = 0; i < A.size(); i++) mp[A[i]] = i; for (int k = 0; k < n; k++) { if (qw[k].first == 0) addnum(mp[qw[k].second], 1); else if (qw[k].first == 1) addnum(mp[qw[k].second], 0); else { long long res = 0; for (int i = 0; i * SQ < A.size(); i++) res += sum[i][3]; cout << res << endl; } } }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 9; struct Node { Node() : cnt(0) { for (int i = 0; i < 5; ++i) { sum[i] = 0; } } int cnt; long long sum[5]; } tree[(MAX << 2)]; int n, idx; bool freq[MAX]; vector<pair<int, int> > q; vector<int> v; Node mergeNodes(const Node& a, const Node& b) { Node ret; ret.cnt = a.cnt + b.cnt; int offset = (5 - a.cnt % 5) % 5; for (int i = 0; i < 5; ++i) { ret.sum[i] = a.sum[i] + b.sum[(i + offset) % 5]; } return ret; } void upd(int low, int high, int pos) { if (low == high) { if (freq[low]) { tree[pos].cnt = 1; tree[pos].sum[0] = v[low]; } else { tree[pos].cnt = 0; tree[pos].sum[0] = 0; } return; } int mid = ((low + high) >> 1); if (idx <= mid) { upd(low, mid, (pos << 1)); } else { upd(mid + 1, high, (pos << 1 | 1)); } tree[pos] = mergeNodes(tree[(pos << 1)], tree[(pos << 1 | 1)]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s[0] == 's') { q.push_back({2, -1}); } else { int x; cin >> x; int type = (s[0] == 'a' ? 0 : 1); q.push_back({type, x}); v.push_back(x); } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); v.insert(v.begin(), -1); for (int i = 0; i < n; ++i) { int type = q[i].first; if (type == 2) { cout << tree[1].sum[2] << "\n"; } else if (type == 0) { int x = q[i].second; idx = lower_bound(v.begin(), v.end(), x) - v.begin(); freq[idx] = 1; upd(1, v.size(), 1); } else { int x = q[i].second; idx = lower_bound(v.begin(), v.end(), x) - v.begin(); freq[idx] = 0; upd(1, v.size(), 1); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct block { list<int> v; long long d[5]; }; int n, mx; void update(block& b) { for (int i = 0; i < 5; i++) b.d[i] = 0; for (int i = 0; i < 5; i++) { list<int>::iterator p = b.v.begin(); for (int j = i; p != b.v.end(); j++, p++) if (j % 5 == 2) b.d[i] += (*p); } } void add(block& b, int val) { list<int>::iterator p; int f = 0; for (p = b.v.begin(); p != b.v.end(); p++) if (*p > val) { b.v.insert(p, val); f = 1; break; } if (!f) b.v.push_back(val); } void del(block& b, int val) { list<int>::iterator p; for (p = b.v.begin(); p != b.v.end(); p++) if (*p == val) { b.v.erase(p); break; } } block norm(block& b) { block ans; for (int i = 0; i < mx; i++) { ans.v.push_back(b.v.front()); b.v.pop_front(); } update(ans); update(b); return ans; } list<block> arr; block t; void addVal(int val) { list<block>::iterator p; for (p = arr.begin(); p != arr.end(); p++) if (p->v.size() && p->v.back() > val) { add(*p, val); update(*p); if (p->v.size() > 2 * mx) arr.insert(p, norm(*p)); break; } if (p == arr.end()) { p--; add(*p, val); update(*p); if (p->v.size() > 2 * mx) arr.insert(p, norm(*p)); } } void delVal(int val) { list<block>::iterator p; for (p = arr.begin(); p != arr.end(); p++) if (p->v.back() >= val) { del(*p, val); update(*p); if (p->v.size() == 0 && arr.size() > 1) arr.erase(p); break; } } long long getSum() { int s = -1; long long ans = 0; list<block>::iterator p; for (p = arr.begin(); p != arr.end(); p++) { ans += p->d[(s + 1) % 5]; s += p->v.size(); } return ans; } int main() { arr.push_back(t); cin >> n; mx = sqrt((double)n) + 2; string com; for (int i = 0; i < n; i++) { cin >> com; if (com == "add") { int v; cin >> v; addVal(v); } if (com == "del") { int v; cin >> v; delVal(v); } if (com == "sum") cout << getSum() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; struct node { int num; long long sum[5]; node operator+(const node &a) { node c; c.num = num + a.num; for (int i = 0; i < 5; i++) { c.sum[i] = sum[i] + a.sum[((i - num) % 5 + 5) % 5]; } return c; } } tree[maxn * 4]; char op[maxn][5]; long long a[maxn], data[maxn]; void build(int root, int l, int r) { memset(tree[root].sum, 0, sizeof(tree[root].sum)); tree[root].num = 0; if (l == r) return; int mid = (l + r) / 2; build(root << 1, l, mid); build(root << 1 | 1, mid + 1, r); } void updata(int root, int l, int r, int p, long long v, int flag) { if (l == r) { tree[root].num += flag; tree[root].sum[0] = v; return; } int mid = (l + r) / 2; if (p <= mid) { updata(root << 1, l, mid, p, v, flag); } else updata(root << 1 | 1, mid + 1, r, p, v, flag); tree[root] = tree[root << 1] + tree[root << 1 | 1]; } int main() { int n, cnt = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", op[i]); if (strcmp(op[i], "sum")) { scanf("%lld", &a[i]); data[cnt++] = a[i]; } } if (cnt == 0) { for (int i = 0; i < n; i++) { if (op[i][0] == 's') printf("0\n"); } return 0; } sort(data, data + cnt); cnt = unique(data, data + cnt) - data; build(1, 1, cnt); for (int i = 0; i < n; i++) { if (!strcmp(op[i], "sum")) { printf("%lld\n", tree[1].sum[2]); continue; } int pos = lower_bound(data, data + cnt, a[i]) - data; if (!strcmp(op[i], "add")) { updata(1, 1, cnt, pos, a[i], 1); } else { updata(1, 1, cnt, pos, 0, -1); } } }
#include <bits/stdc++.h> using namespace std; template <class T> void read(T &x) { x = 0; char c = getchar(); bool p = 0; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') p = 1; for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); if (p) x = -x; } const int N = 100005; struct node { long long Hina[5]; int num; } tree[N << 2]; int n, opt[N]; char tmp[5]; map<long long, int> mp; long long x[N], xx[N]; void pushup(int k) { tree[k].num = tree[k << 1].num + tree[k << 1 | 1].num; for (int i = 0; i < 5; ++i) { tree[k].Hina[i] = tree[k << 1].Hina[i] + tree[k << 1 | 1].Hina[((i - tree[k << 1].num) % 5 + 5) % 5]; } } void add(int l, int r, int k, int pos, int opt, long long val) { if (l == r) { if (!opt) { ++tree[k].num; tree[k].Hina[1] = val; } else { --tree[k].num; tree[k].Hina[1] = 0; } return; } int mid = (l + r) >> 1; if (pos <= mid) add(l, mid, k << 1, pos, opt, val); else add(mid + 1, r, k << 1 | 1, pos, opt, val); pushup(k); } int cnt; int main() { read(n); for (int i = 1; i <= n; ++i) { scanf("%s", tmp); if (tmp[0] == 'a') opt[i] = 0; if (tmp[0] == 'd') opt[i] = 1; if (tmp[0] == 's') opt[i] = 2; if (tmp[0] != 's') { read(x[++cnt]); xx[cnt] = x[cnt]; } } sort(xx + 1, xx + cnt + 1); int temp = 0; for (int i = 1; i <= cnt; ++i) { if (!mp[xx[i]]) { mp[xx[i]] = ++temp; } } cnt = 0; for (int i = 1; i <= n; ++i) { if (opt[i] == 2) { printf("%I64d\n", tree[1].Hina[3]); } else { ++cnt; add(1, temp, 1, mp[x[cnt]], opt[i], x[cnt]); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> a; int n, x; char s[9]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (*s == 's') { long long z = 0; for (long long j = 2; j < a.size(); j += 5) z += a[j]; cout << z << endl; } else { cin >> x; if (*s == 'a') { a.insert(lower_bound(a.begin(), a.end(), x), x); } else { a.erase(lower_bound(a.begin(), a.end(), x)); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int q, n; int type[100100]; long long num[100100]; string s; vector<long long> v; long long sum[400100][5]; int cnt[400100]; int nu; void merge(int a, int b, int c) { cnt[a] = cnt[b] + cnt[c]; for (int i = 0; i < 5; i++) sum[a][i] = sum[b][i]; int g = cnt[b] % 5; for (int i = 0; i < 5; i++) sum[a][(i + g) % 5] += sum[c][i]; } void add(int id) { id += nu; cnt[id] = 1; sum[id][0] = v[id - nu]; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } void del(int id) { id += nu; cnt[id] = 0; sum[id][0] = 0; for (; id >>= 1;) merge(id, (id << 1) | 0, (id << 1) | 1); } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> q; for (int i = 0; i < q; i++) { cin >> s; if (s == "add") { type[i] = 0; cin >> num[i]; v.push_back(num[i]); } else if (s == "del") { type[i] = 1; cin >> num[i]; } else type[i] = 2; } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); n = v.size(); nu = 1; while (nu < n + 5) nu *= 2; for (int i = 0; i < q; i++) if (num[i]) num[i] = lower_bound(v.begin(), v.end(), num[i]) - v.begin(); for (int i = 0; i < q; i++) { if (type[i] == 0) add(num[i]); else if (type[i] == 1) del(num[i]); else cout << sum[1][2] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T sq(const T& x) { return x * x; } const int INF = 2147483647; struct Node { vector<int> a; long long s[5]; Node() { update(); } int size() const { return a.size(); } void insert(int x) { a.insert(upper_bound(a.begin(), a.end(), x), x); update(); } void erase(int x) { a.erase(lower_bound(a.begin(), a.end(), x)); update(); } void update() { memset(s, 0, sizeof s); for (size_t i = 0; i < a.size(); ++i) s[i % 5] += a[i]; } }; struct Kiwi { list<Node> li; int size; Kiwi() : size(0) {} void insert(int x) { ++size; auto it = li.begin(); for (; it != li.end(); ++it) { auto nx = it; ++nx; int right_bound = (nx == li.end()) ? INF : nx->a[0]; if (x <= right_bound) break; } if (it == li.end()) it = li.insert(it, Node()); it->insert(x); if (sq(it->size()) >= size * 4) { int gap = sqrt(size + 0.5); Node node; node.a.assign(it->a.begin() + gap, it->a.end()); node.update(); it->a.erase(it->a.begin() + gap, it->a.end()); it->update(); li.insert(++it, node); } } void erase(int x) { --size; auto it = li.begin(); while (x > it->a.back()) ++it; it->erase(x); if (it->size() == 0) { li.erase(it); } else if (it != li.begin()) { auto pv = it; --pv; if (sq(pv->size() + it->size()) < size * 1.4) { copy(it->a.begin(), it->a.end(), back_inserter(pv->a)); pv->update(); li.erase(it); } } } long long sum() const { long long ret = 0; int p = 2; for (auto it = li.begin(); it != li.end(); ++it) { ret += it->s[p]; p = ((p - (int)it->a.size()) % 5 + 5) % 5; } return ret; } }; void york() { int m; Kiwi b; char s[32]; scanf("%d", &m); while (m--) { scanf("%s", s); if (s[0] == 's') { printf("%I64d\n", b.sum()); } else if (s[0] == 'a') { int t; scanf("%d", &t); b.insert(t); } else { int t; scanf("%d", &t); b.erase(t); } } } int main() { york(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 5; long long n, q[maxn], cnt, q2[maxn]; pair<int, int> b[maxn]; bool below[maxn]; struct node { long long a[5], siz; } f[maxn << 3]; void pushup(long long p) { long long ls = p * 2, rs = p * 2 + 1; for (long long i = 0; i < 5; i++) f[p].a[i] = f[ls].a[i]; f[p].siz = f[ls].siz + f[rs].siz; for (long long i = 0; i < 5; i++) f[p].a[(f[ls].siz + i) % 5] += f[rs].a[i]; } long long lab(long long x) { if (x >= 0) return 1; else return 0; } void change(long long p, long long l, long long r, long long x, long long y) { if (r < x or l > x) return; if (l == r and l == x) { f[p].a[1] += y; f[p].siz = lab(y); return; } long long mid = (l + r) >> 1; if (x <= mid) change(p << 1, l, mid, x, y); else change(p << 1 | 1, mid + 1, r, x, y); pushup(p); } int main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) { char str[3]; scanf("%s", str); if (str[0] == 'a' or str[0] == 'd') { long long x; scanf("%lld", &x); q[i] = q2[i] = x; b[++cnt] = make_pair(x, i); if (str[0] == 'd') below[i] = true; } else q[i] = -1; } sort(b + 1, b + cnt + 1); long long rak = 0; for (long long i = 1; i <= cnt; i++) { if (b[i].first != b[i - 1].first) rak++; q[b[i].second] = rak; } for (long long i = 1; i <= n; i++) { if (q[i] == -1) printf("%lld\n", f[1].a[3]); else { if (below[i]) change(1, 1, rak, q[i], -q2[i]); else change(1, 1, rak, q[i], q2[i]); } } return 0; }