SplayPOJ3468 (new for old questions)

Source: Internet
Author: User
ASimpleProblemwithIntegersTimeLimit: 5000 MS & #160; & #160; & #160; & #160; & #160; MemoryLimit: 131072KB & #160; & #160; & #160; & #160; & #160; 64 bitIOFormat: % I64d & amp; % I64

A Simple Problem with Integers Time Limit:5000 MS Memory Limit:131072KB 64bit IO Format:% I64d & % I64uSubmit Status Practice POJ 3468 Appoint description: System Crawler)

Description

You haveNIntegers,A1,A2 ,...,AN. You need to deal with two kinds of operations. one type of operation is to add some given number to each number in a given interval. the other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbersNAndQ. 1 ≤N,Q≤ 100000.
The second line containsNNumbers, the initial valuesA1,A2 ,...,AN.-1000000000 ≤Ai≤ 1000000000.
Each of the nextQLines represents an operation.
"CAbc"Means addingCTo eachAa,Aa+ 1 ,...,AB.-10000 ≤C≤ 10000.
"QAB"Means querying the sumAa,Aa+ 1 ,...,AB.

Output

You need to answer allQCommands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.
This question should be regarded as a line segment tree entry question, but it can also be done using Splay to maintain the sequence, an important property of the balanced binary tree is that the sequential traversal is ordered. The first Splay in life (touching TAT, QAQ)

The code is as follows:

/*************************************** * *********************************> File Name: spaly. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: ******************************** **************************************** /# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     # Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              Using namespace std; typedef long LL; const int maxn = 1e5 + 100; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backLL add [maxn], sum [maxn]; int ch [maxn] [2], siz [maxn], key [maxn], pre [maxn], A [maxn]; int root, tot; void newnode (int & x, int fa, int Key) // Create a node {x = ++ tot; pre [x] = fa; siz [x] = 1; key [x] = sum [x] = Key; ch [x] [0] = ch [x] [1] = add [x] = 0;} void Modify (int x, int val) {// interval update if (! X) return; add [x] + = val; key [x] + = val; sum [x] + = (LL) val * siz [x];} void push_down (int x) {// Mark if (! Add [x]) return; Modify (ch [x] [0], add [x]); Modify (ch [x] [1], add [x]); add [x] = 0;} void push_up (int x) {// update node siz [x] = siz [ch [x] [0] + siz [ch [x] [1] + 1; sum [x] = sum [ch [x] [0] + sum [ch [x] [1] + key [x];} void built (int & x, int L, int R, int fa) {if (L> R) return; int M = (L + R)> 1; newnode (x, fa, A [M]); built (ch [x] [0], L, M-1, x); built (ch [x] [1], M + 1, R, x); push_up (x);} void Init (int n) // initialize Spaly and add two virtual nodes to facilitate interval extraction, avoiding discussion {root = tot = 0; newnode (root, 0, 0 ); Newnode (ch [root] [1], root, 0); for (int I = 1; I <= n; I ++) scanf ("% d ", A + I); built (ch [ch [root] [1] [0], 1, n, ch [root] [1]); push_up (ch [root] [1]); push_up (root);} void print (int x) {if (! X) return; print (ch [x] [0]); printf ("% d", key [x]); print (ch [x] [1]);} void Rotate (int x, bool kind) {// Rotate, true right-hand, false left-hand int y = pre [x]; push_down (y ); // flag push_down (x); ch [y] [! Kind] = ch [x] [kind]; pre [ch [x] [kind] = y; ch [x] [kind] = y; if (pre [y]) ch [pre [y] [ch [pre [y] [1] = y] = x; // if the parent node of y points its child pointer to xpre [x] = pre [y]; pre [y] = x; push_up (y); // update back, note that you must update the child push_up (x);} void Spaly (int x, int goal) {// stretch operation, rotate x to push_down (x) under goal; while (pre [x]! = Goal) {if (pre [pre [x] = goal) Rotate (x, ch [pre [x] [0] = x ); else {int y = pre [x]; bool kind = (ch [pre [y] [0] = y ); if (ch [y] [kind] = x) {Rotate (x ,! Kind); Rotate (x, kind);} else {Rotate (y, kind); Rotate (x, kind) ;}} push_up (x ); if (goal = 0) root = x; // if goal is 0, x has been rotated to the root, so update root} int Get_kth (int x, int k) {// The k value in the sequence int t = siz [ch [x] [0] + 1; if (t = k) return x; if (t> k) return Get_kth (ch [x] [0], k); return Get_kth (ch [x] [1], k-t );} int main () {ios_base: sync_with_stdio (false); cin. tie (0); siz [0] = sum [0] = 0; // non-existent nodes are initialized to 0 to avoid discussing int n, q, l, r, x; scanf ("% d", & n, & q); Init (n); char cmd [5]; while (q --) {scanf ("% s % d", cmd, & l, & r); Spaly (Get_kth (root, l), 0); Spaly (Get_kth (root, r + 2), root); if (cmd [0] = 'Q') {printf ("% lld \ n ", sum [ch [ch [root] [1] [0]);} else {int Add; scanf ("% d", & Add ); modify (ch [ch [root] [1] [0], Add); push_up (ch [root] [1]); push_up (root );}} 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.