Hdu 4288 Coder

Source: Internet
Author: User

Hdu 4288 Coder

 

Initially, there is an empty set with N operations. 1 add x adds an element x to the set, and 2 del x deletes an element x in the set, sum queries the sum of the elements whose subscript mod 5 is 3.

 

A line segment tree can be created to delete and add elements in the array and maintain the sum of the intervals.

The key point is how to calculate the sum of the number where mod 5 is 3 at any time when the element changes at any time. First, the node has a message cnt indicating the number of elements in the interval, sum [5]

It indicates the sum of the elements in the lower and lower tables with the remainder of 5 in this interval. Note that if an element exists in the current interval, it is saved to sum [1. Then there is the maintenance of sum [] During push_up. This is hard to think about. I read the problem and discussed it with my classmates for a long time.

 

 

# Include
 
  
# Include
  
   
# Include
   # Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              # Include // # define LL long # define LL _ int64 # define eps 1e-12 # define PI acos (-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100010; struct Info {char str [5]; LL num;} info [maxn]; LL x [maxn]; struct node {int l, r; int cnt; LL sum [6];} tree [maxn * 4]; void build (int v, int l, int r) {tree [v]. l = l; tree [v]. r = r; tree [v]. cnt = 0; tree [v]. sum [0] = tree [v]. sum [1] = tree [v]. sum [2] = t Ree [v]. sum [3] = tree [v]. sum [4] = 0; if (l = r) return; int mid = (l + r)> 1; build (v * 2, l, mid ); build (v * 2 + 1, mid + 1, r);} int Binsearch (int l, int r, int key) {int mid, low = l, high = r; while (high> = low) {mid = (low + high)> 1; if (x [mid] = key) return mid; if (x [mid]> key) high = mid-1; else low = mid + 1;} return-1;} void update (int v, int pos, LL num, int f) {tree [v]. cnt + = f; if (tree [v]. l = tree [V]. r) {tree [v]. sum [1] + = num; return;} int mid = (tree [v]. l + tree [v]. r)> 1; if (pos <= mid) update (v * 2, pos, num, f); else update (v * 2 + 1, pos, num, f); // focuses on the sum of maintenance intervals. For (int I = 0; I <5; I ++) tree [v]. sum [I] = tree [v * 2]. sum [I] + tree [v * 2 + 1]. sum [(I-tree [v * 2]. cnt) % 5 + 5) % 5];} int main () {int n; int cnt; while (~ Scanf (% d, & n) {cnt = 0; for (int I = 1; I <= n; I ++) {scanf (% s, info [I]. str); if (strcmp (info [I]. str, sum )! = 0) {scanf (% I64d, & info [I]. num); x [++ cnt] = info [I]. num ;}// discretization sort (x + 1, x + 1 + cnt); cnt = unique (x + 1, x + 1 + cnt) -(x + 1); build (1, 1, cnt); int pos; for (int I = 1; I <= n; I ++) {if (info [I]. str [0]! ='S ') pos = Binsearch (1, cnt, info [I]. num); // returns the subscript if (info [I]. str [0] = 'A') {update (1, pos, info [I]. num, 1);} else if (info [I]. str [0] = 'D') {update (1, pos,-info [I]. num,-1);} else printf (% I64d, tree [1]. sum [3]) ;}} 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.