Coder
Question: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4288
Question: There are three types of operations: (1). "add X", which indicates adding? Number X. (2). "del X" indicates to delete the number X in the set. (3). "sum" is used to obtain the sum of the numbers in the subscripts of the Set in ascending order. The number in the set is unique.
Ideas: The clever part of this question is that the input is offline and then discretization. The numbers are sorted in ascending order and then used as the leaf node of the Line Segment tree. Each node consists of two parts: one is the number of numbers contained in the node, and the other is the sum of the remainder of the internal model 5 in the interval. When pushup is required, the interval of the Left subtree remains unchanged, and the position of each number changes in the interval of the right subtree, starting with I, then it changes to I + CNT (CNT indicates the number of digits in the left subtree range)
Code:
#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<fstream>#include<cstring>#include<ctype.h>#include<iostream>#include<algorithm>#define INF (1<<30)#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))#define rep(i, n) for (int i = 0; i < n; i++)#define debug puts("===============")typedef long long ll;using namespace std;const int maxn = 100200;ll sum[maxn << 2][5];int cnt[maxn << 2];#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1int n, tot, op[maxn], a[maxn];char str[maxn][10];void pushup(int rt) { cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1]; int p = cnt[rt << 1]; for (int i = 0; i < 5; i++) { sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][((i - p) % 5 + 5) % 5]; } //cout<<rt<<" "<<cnt[rt]<<endl;}void update(int pos, int x, int l, int r, int rt) { if (l == r) { if (x == 1) { sum[rt][1] = a[pos - 1]; cnt[rt] = 1; } else { sum[rt][1] = 0; cnt[rt] = 0; } return ; } int m = (l + r) >> 1; if (pos <= m) update(pos, x, lson); else update(pos, x, rson); pushup(rt);}void build(int l, int r, int rt) { for (int i = 0; i < 5; i++) sum[rt][i] = 0; cnt[rt] = 0; if (l == r) return ; int m = (l + r) >> 1; build(lson); build(rson);}int main () { while(~scanf("%d", &n)) { tot = 0; for (int i = 0; i < n; i++) { scanf("%s", str[i]); if (str[i][0] != 's') { scanf("%d", op + i); a[tot++] = op[i]; } } sort(a, a + tot); tot = unique(a, a + tot) - a; build(1, tot, 1); for (int i = 0; i < n; i++) { if (str[i][0] == 's') printf("%I64d\n", sum[1][3]); else { int pos = lower_bound(a, a + tot, op[i]) - a + 1; if (str[i][0] == 'a') update(pos, 1, 1, tot, 1); else update(pos, -1, 1, tot, 1); } } } return 0;}<strong></strong>
HDU 4288 coder (line segment tree)