http://acm.hdu.edu.cn/showproblem.php?pid=4288
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 would replace many other employees. An added benefit to the employer was that the coder would also become redundant once their work was done. 1
You is now the Signle coder, and has been assigned a new task writing code, since your boss would like to replace many O Ther employees (and you are become redundant once your task is complete).
Your code should is able to complete a task to replace these employees who does nothing all day but eating:make the digest Sum.
By saying "Digest sum" we study some properties of data. For the sake of simplicity, we 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 is understood by
Where the set S is written as {A1, A2, ..., AK} satisfying A1 < A2 < A3 < ... < AK
Can you complete the This task (and is then fired)?
------------------------------------------------------------------------------
1 See Http://uncyclopedia.wikia.com/wiki/Algorithm
Input there ' re several test cases.
The first line contains one integer n (1 <= n <=), and the number of operations to process.
Then following are n lines, each one containing one of the three operations: "Add X" or "Del x" or "sum".
Assume that 1 <= x <= 109.
Please see the sample for detailed format.
For any "add X" It's guaranteed that's currently in the set just before this operation.
For any "Del X" it's guaranteed that x must currently are in the set just before this operation.
Please process until EOF (End of File).
Output for each operation "sum" tab print one line containing exactly one integer denoting the digest sum of the Curre NT set. Print 0 If the set is empty.
Sample Input
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
Sample Output
345HintC + + maybe run faster than g++ in this problem.
/**hdu4288 Offline line segment Tree topic: Three operations on an array that was initially empty: Add x: Insert X:del x: Delete x:sum: Query the sum of the 5 modulo 3 in the current series. Problem solving: Violence will time out. Can be done by the line segment tree, because the segment tree does not support the deletion and increase of points, the normal line segment tree can not meet the requirements of the topic. We can save all the points added in the input, then go to the weight, make a sequence, for the element if the current segment tree has, then the mark is 1, otherwise 0, maintain the number of marks in the line tree cnt, and 5 modulo five case value. When adding or deleting an operation, simply change the value of that node and then treat it with the effect of the subscript. So how do you deal with the impact of these operations on the subscript? Now let's consider a parent interval, assuming that its left and right sub-range has been updated. Obviously, the condition of the subscript%5 in the left interval is exactly the same as for%5 in the parent interval, but the right interval is not necessarily the same because the subscript in the right interval starts with mid as 1. So, as long as we know the number of active elements in the left interval cnt, we can know that the subscript i in the right interval corresponds to the subscript i+cnt in the parent interval. So, although we ultimately want only the sum of the total interval subscript%5 = 3. But in the update we need to know all the conditions of the right zone%5. So we are going to open a sum[5] and a CNT on each node of the segment tree, and record the number of the 5 cases and the valid elements of this node, respectively, of the subscript%5. When querying, direct access to the total interval of sum[3] can be. * * #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream>using Namespace Std;typedef long Long ll;const int maxn=500000+10;int N,len,flag;char str[maxn][10];int NUM[MAXN],S[MAXN]; struct node{int l,r,cnt; LL sum[5];} Tree[maxn*4];void init (int l,int r,int i) {treE[i].l=l; Tree[i].r=r; tree[i].cnt=0; memset (tree[i].sum,0,sizeof (tree[i].sum)); if (l!=r) {int mid= (L+R) >>1; Init (l,mid,i<<1); Init (mid+1,r,2*i+1); }}void Add (int x) {for (int i=0;i<5;i++) {int j= (i+tree[x<<1].cnt)%5; tree[x].sum[j]=tree[x<<1].sum[j]+tree[x<<1|1].sum[i]; }}void Insert (int i,int pos,int m) {flag?tree[i].cnt++:tree[i].cnt--; if (TREE[I].L==TREE[I].R) {tree[i].sum[1]=flag*m; Return } int mid= (TREE[I].L+TREE[I].R) >>1; if (pos<=mid) insert (I<<1,POS,M); else insert (I<<1|1,POS,M); Add (i);} int main () {while (~SCANF ("%d", &n)) {len=0; for (int i=0;i<n;i++) {scanf ("%s", Str[i]); if (str[i][0]!= ' s ') {scanf ("%d", &num[i]); S[len++]=num[i]; }} sort (S,s+len); Len=unique (S,s+len)-S; if (len==0) {memset (tree[1].sum,0,sizeof (tree[1].sum)); } else {init (1,len,1); } for (int i=0;i<n;i++) {if (str[i][0]== ' a ') {flag=1; int Pos=lower_bound (s,s+len,num[i])-s+1; Insert (1,pos,num[i]); } else if (str[i][0]== ' d ') {flag=0; int Pos=lower_bound (s,s+len,num[i])-s+1; Insert (1,pos,num[i]); } else {/*for (int j=0;j<5;j++) printf ("%i64d", Tree[1].sum[j]); printf ("\ n"); */printf ("%i64d\n", tree[1].sum[3]); }}} return 0;} /**18add 1add 2add 3add 4add 5sumadd 6add 7add 8add 9add 10add 11add 12del 3add 13add 14del 9sumanswer:14*/
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 would replace many other employees. An added benefit to the employer was that the coder would also become redundant once their work was done. 1
You is now the Signle coder, and has been assigned a new task writing code, since your boss would like to replace many O Ther employees (and you are become redundant once your task is complete).
Your code should is able to complete a task to replace these employees who does nothing all day but eating:make the digest Sum.
By saying "Digest sum" we study some properties of data. For the sake of simplicity, we 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 is understood by
Where the set S is written as {A1, A2, ..., AK} satisfying A1 < A2 < A3 < ... < AK
Can you complete the This task (and is then fired)?
------------------------------------------------------------------------------
1 See Http://uncyclopedia.wikia.com/wiki/Algorithm
Input there ' re several test cases.
The first line contains one integer n (1 <= n <=), and the number of operations to process.
Then following are n lines, each one containing one of the three operations: "Add X" or "Del x" or "sum".
Assume that 1 <= x <= 109.
Please see the sample for detailed format.
For any "add X" It's guaranteed that's currently in the set just before this operation.
For any "Del X" it's guaranteed that x must currently are in the set just before this operation.
Please process until EOF (End of File).
Output for each operation "sum" tab print one line containing exactly one integer denoting the digest sum of the Curre NT set. Print 0 If the set is empty.
Sample Input
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
Sample Output
345HintC + + maybe run faster than g++ in this problem.
hdu4288 Offline processing segment tree