题意:
给你3种操作:
1. 向集合中加入数x。
2.在集合中删除数x。
3.求和sum: 将集合中的数组排好序,将下标(从1开始) 对5取模为3 的位置的数 求和。
思路:
线段树结点
sum[i] 表示 对5取模为i 的数的和。
cnt 表示这个区间内存在的数的个数。
那么每次1,2 操作更新单个结点, 然后pushup:
左子树的位置和父结点的下标位置是一样的, 右子树需要加上左子树的个数 才是父结点的下标。 这样更新sum数组即可。
每次查询输出1号结点的sum[3]即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
typedef long long LL;
struct node{
LL sum[5];
int cnt;
}nod[maxn<<2];
struct Node{
int op; /// add is 0 , del is 1 , sumi is 2;
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;
while(~scanf("%d",&n)){
cnt = 0;
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;
}
Coder
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5664 Accepted Submission(s): 2132
Problem Description
In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done.
1
You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
1. add x – add the element x to the set;
2. del x – remove the element x from the set;
3. sum – find the digest sum of the set. The digest sum should be understood by
where the set S is written as {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
1. add x – add the element x to the set;
2. del x – remove the element x from the set;
3. sum – find the digest sum of the set. The digest sum should be understood by
![](https://i-blog.csdnimg.cn/blog_migrate/5c7cf6f55433f9c0922c86baf44dab6a.jpeg)
where the set S is written as {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
Input
There’re several test cases.
In each test case, the first line contains one integer N ( 1 <= N <= 10 5 ), the number of operations to process.
Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
You may assume that 1 <= x <= 10 9.
Please see the sample for detailed format.
For any “add x” it is guaranteed that x is not currently in the set just before this operation.
For any “del x” it is guaranteed that x must currently be in the set just before this operation.
Please process until EOF (End Of File).
In each test case, the first line contains one integer N ( 1 <= N <= 10 5 ), the number of operations to process.
Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
You may assume that 1 <= x <= 10 9.
Please see the sample for detailed format.
For any “add x” it is guaranteed that x is not currently in the set just before this operation.
For any “del x” it is guaranteed that x must currently be in the set just before this operation.
Please process until EOF (End Of File).
Output
For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.
Sample Input
9 add 1 add 2 add 3 add 4 add 5 sum add 6 del 3 sum 6 add 1 add 3 add 5 add 7 add 9 sum
Sample Output
3 4 5HintC++ maybe run faster than G++ in this problem.
Source
Recommend
liuyiding