HDU 4288 coder 37th ACM/ICPC Chengdu division Network Competition 1001 question (line segment tree)

Source: Internet
Author: User
Coder

Time Limit: 20000/10000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 120 accepted submission (s): 52

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 input9 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 output3 4 5

Hint

C ++ maybe run faster than G ++ in this problem.

 

Source2012 ACM/ICPC Asia Regional Chengdu online

 

Recommendliuyiding The question of the Line Segment tree. It is said to be the original question of CF. There are too many questions about this online competition. Http://www.codeforces.com/contest/85/problem/D Create a line segment tree. Use offline processing. Each interval s [5] and num represent five sums and numbers respectively.
# Include <stdio. h> # Include <Iostream> # Include <Map> # Include <Algorithm> # Include <String . H> Using   Namespace  STD;  Const   Int Maxn = 100010  ;  Struct  Node {  Int  L, R;  Long   Long S [ 5  ]; Int  Num;} segtree [maxn * 3  ]; Map < Long   Long , Int > MP; Map < Long   Long , Int > MP2;  Long   Long  A [maxn]; Void Build ( Int I, Int L, Int  R) {segtree [I]. L = L; segtree [I]. r = R; segtree [I]. Num = 0  ;  For ( Int J = 0 ; J < 5 ; J ++) Segtree [I]. s [J] = 0  ;  If (L = R) {MP [A [l] = I;  Return  ;}  Int Mid = (L + r)> 1  ); Build (I < 1  , L, mid); Build (I < 1 ) |1 , Mid + 1  , R );}  Void Add ( Long   Long  X ){  Int Tt = MP [X]; segtree [TT]. Num = 1  ; Segtree [TT]. s [  0 ] = X; TT >>= 1  ; While  (Tt) {segtree [TT]. Num ++ ;  Int Temp = segtree [TT < 1  ]. Num;  For ( Int I = 0 ; I < 5 ; I ++ ) Segtree [TT]. s [I] = Segtree [TT < 1  ]. S [I];  For ( Int I = 0 ; I < 5 ; I ++ ) Segtree [TT]. s [(temp + I) % 5 ] + = Segtree [(TT < 1 ) | 1  ]. S [I]; TT >>= 1  ;}}  Void Del ( Long   Long  X ){ Int Tt = MP [X]; segtree [TT]. Num = 0  ;  For ( Int I = 0 ; I < 5 ; I ++) segtree [TT]. s [I] = 0  ; TT >>= 1  ;  While  (Tt ){  If (Segtree [TT]. Num> 0 ) Segtree [TT]. Num -- ;  Int Temp = segtree [TT < 1  ]. Num;  For ( Int I = 0 ; I < 5 ; I ++ ) Segtree [TT]. s [I] = Segtree [TT < 1  ]. S [I];  For (Int I = 0 ; I < 5 ; I ++ ) Segtree [TT]. s [(temp + I) % 5 ] + = Segtree [(TT < 1 ) | 1  ]. S [I]; TT >>= 1  ;}}  Struct  Action {  Int  ID; Long   Long  X;} node [maxn];  Char STR [ 20  ];  Int  Main (){  Int  N;  Int  M;  While (Scanf ( "  % D  " , & N )! =EOF) {m = 0  ;  For ( Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % S  " ,& Str );  If (Strcmp (STR, "  Add  " ) =0  ) {Node [I]. ID = 0  ; Scanf (  "  % I64d  " ,& Node [I]. X); [ ++ M] = Node [I]. X ;}  Else   If (Strcmp (STR, "  Sum  " ) =0  ) {Node [I]. ID = 1  ;}  Else  {Node [I]. ID = 2  ; Scanf (  "  % I64d  " ,& Node [I]. X);} [ ++ M] = 0  ; Sort ( + 1 , A + M + 1  ); MP. Clear (); Build (  1 , 1  , M); mp2.clear (); add (  0  ); MP2 [  0 ] = 1  ;  For ( Int I = 1 ; I <= N; I ++){  If (Node [I]. ID = 0  ){  If (MP2 [node [I]. x] = 0  ) {MP2 [node [I]. x] = 1  ; Add (node [I]. X );}}  Else   If (Node [I]. ID = 1  ) {Printf ( "  % I64d \ n  " , Segtree [ 1 ]. S [ 3  ]);}  Else  {  If (MP2 [node [I]. x] = 1  ) {MP2 [node [I]. x] = 0  ; Del (node [I]. X );}}}} Return   0  ;} 

 

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.