HDU-4288-Coder (line tree)

Source: Internet
Author: User

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. compile companies now employ a single coder to write an algorithm that will replace into 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 wocould like to replace into other employees (and you when you become redundant once your task is complete ).
Your code shocould 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 shoshould 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 shocould be understood

Where the set S is written as {A1, A2,..., AK} satisfying A1 <A2 <A3 <... <AK
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 <= 105), 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 <= 109.
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
 
9add 1add 2Add 3add 4add 5 sumadd 6del 3sum6add 1add 3add 5add 7add 9sum

Sample output
345HintC ++ maybe run faster than G ++ in this problem. 

Source2012 ACM/ICPC Asia Regional Chengdu online
Idea: Use sum [400005] [5] to save the sum of the subscripts of the corresponding range modulo 5 as the number of I, the rest is the single-point update and sum maintenance based on the number of left and right son nodes.
# Include <cstdio> # include <cstring> # include <map> # include <algorithm> using namespace STD; struct s {char op [5]; int X, ID ;} E [100005]; bool cmpval (s a, s B) {if (. X = B. x) return. OP [0] <B. OP [0]; return. x <B. x;} bool cmpid (s a, s B) {return. ID <B. ID;} int N, CNT, Val [100005], num [400005]; long sum [400005] [5]; void build (INT idx, int S, int e) {If (s! = E) {int mid = (S + E)> 1; build (idx <1, S, mid); Build (idx <1 | 1, mid + 1, e);} num [idx] = 0; For (INT I = 0; I <5; I ++) sum [idx] [I] = 0;} void Update (INT idx, int S, int e, int POs, int flag) {num [idx] + = flag; if (S = e) {sum [idx] [1] + = Val [s] * flag; return;} int mid = (S + E)> 1; if (Pos <= mid) Update (idx <1, S, mid, POs, flag); else Update (idx <1 | 1, Mid + 1, E, POs, flag); For (INT I = 0; I <5; I ++) sum [idx] [I] = sum [idx <1] [I] + sum [idx <1 | 1] [I-n Um [idx <1] % 5> = 0? I-num [idx <1] % 5: I-num [idx <1] % 5 + 5]; // update the long query (INT idx, int S, int e, int mod) where the subscript in the corresponding interval is the sum of Modulo 5 and equal to I) {return sum [idx <1] [mod] + sum [idx <1 | 1] [mod-num [idx <1] % 5> = 0? Mod-num [idx <1] % 5: mod-num [idx <1] % 5 + 5];} int main () {int I; long ans; while (~ Scanf ("% d", & N) {Map <int, int> MP; // used to determine the weight during discretization. It is possible that add a first, then del, add a for (I = 0; I <n; I ++) {scanf ("% s", E [I]. OP); If (E [I]. OP [0] ='s ') E [I]. X = 0; else scanf ("% d", & E [I]. x); e [I]. id = I;} Sort (E, E + N, cmpval); CNT = 1; for (I = 0; I <n; I ++) // discretization {If (E [I]. OP [0] = 'A') {If (! MP [E [I]. x]) MP [E [I]. x] = CNT ++; Val [MP [E [I]. x] = E [I]. x; E [I]. X = MP [E [I]. x];} else if (E [I]. OP [0] = 'D') {e [I]. X = MP [E [I]. x] ;}} sort (E, E + N, cmpid); CNT --; If (! CNT) // Special Command {for (I = 0; I <n; I ++) {If (E [I]. OP [0] ='s ') {printf ("0 \ n") ;}} continue;} build (, CNT); for (I = 0; I <n; I ++) {If (E [I]. OP [0] = 'A') Update (1, 1, CNT, E [I]. x, 1); else if (E [I]. OP [0] = 'D') Update (1,1, CNT, E [I]. x,-1); else printf ("% i64d \ n", query (, N, 3 ));}}}


HDU-4288-Coder (line tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.