Codeforces Round #263 (Div. 1) c. Appleman and a Sheet of Paper tree-like array violence update

Source: Internet
Author: User

C. Appleman and a Sheet of Paper

Appleman has a very big sheet of paper. This sheet have a form of rectangle with dimensions 1x n. Your task is help Appleman with folding of such a sheet. Actually, need to perform q queries. Each query would have one of the following types:

  1. Fold the sheet of paper at position pI. After this query, the leftmost part of the paper with dimensions 1x Pi must be above the righ Tmost part of the paper with Dimensions 1x ([ current width of sheet]- C17>pi).
  2. Count What's the total width of the paper pieces, if we'll make a described later cuts and consider only the pieces B Etween the cuts. We'll make one cut at distance Li from the border of the current sheet of paper and the o ther at distance Ri from the left border of the current sheet of paper.

The explanation of the first Test example for better understanding of the problem.

Input

The first line contains the integers: n and q (1≤ n ≤105; 1≤ q< /C15>≤105)-the width of the paper and the number of queries.

Each of the following Q Lines contains one of the described queries in the following format:

    • "1 pi" (1≤ pi < [ current width  of sheet]) -the first type query.
    • "2 li ri" (0≤ lI < Ri ≤[ current width of sheet])-the Second type query.
Output

For each query of the second type, output the answer.

Input
7 4
1 3
0 S
2 0 1
2 1 2
Output
4
3
Idea: Violence update, then use Fenwicktree or segmenttree to do interval sum.
Because the value of each location will only be updated to another location once, so the violent word complexity is O (n), and then update the time in two cases, if the length of the folded past is greater than the right side of the corresponding length of the fold, otherwise the title is said from the left fold.
I use UA, UB, maintain the current interval of the left and right endpoints, each query also divided into two cases.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = 1e5 +5;4 namespaceFenwicktree {5 intARR[MAXN];6 voidModify (intXintd) {7      while(X <MAXN) {8ARR[X] + =D;9x + = x &-x;Ten     } One } A voidInitintN) { -memset (arr,0,sizeofarr); -      for(inti =1; I <= N; i++) { theModify (I,1); -     } - } - intQueryintx) { +     intres =0; -      while(X >0) { +Res + =Arr[x]; AX-= x &-x; at     } -     returnRes; - } - } - intMain () { - #ifndef Online_judge inFreopen ("In.txt","R", stdin); - #endif to     intN, Q; +      while(~ scanf ("%d%d", &n, &q)) { -         inttot =0, Direction =0; the fenwicktree::init (n); *         intUB =N; $          for(intj =0; J < Q; J + +) {Panax Notoginseng             intop, L, R, p; -             intUA = tot+1; thescanf ("%d", &op); +             if(OP = =1) { Ascanf ("%d", &p); the                 if(!direction) { +                     if(2*p <=1+ UB-UA) { -                          for(inti = UA; I <= ua+p-1; i++) { $Fenwicktree::modify (2*ua+2*p-i-1, Fenwicktree::query (i)-Fenwicktree::query (i-1)); $                         } -Tot + =p; -}Else { thep = UB-(UA + P-1); -                          for(inti = UB; I >= ub-p+1; i--) {WuyiFenwicktree::modify (2*ub-2*p+1-I, Fenwicktree::query (i)-Fenwicktree::query (i-1)); the                         } -UB = UB-p; WuDirection ^=1; -                     } About}Else{ $                     if(2*p >1+ UB-UA) { -p = UB-(UA + P-1); -                          for(inti = UA; I <= ua+p-1; i++) { -Fenwicktree::modify (2*ua+2*p-i-1, Fenwicktree::query (i)-Fenwicktree::query (i-1)); A                         } +Direction ^=1; theTot + =p; -}Else{ $                          for(inti = UB; I >= ub-p+1; i--) { theFenwicktree::modify (2*ub-2*p+1-I, Fenwicktree::query (i)-Fenwicktree::query (i-1)); the                         } theUB = UB-p; the                     } -                 } in  the}Else { thescanf ("%d%d", &l, &R); About                 if(!direction) { theprintf"%d\n", Fenwicktree::query (ua+r-1)-fenwicktree::query (ua-1+l)); the}Else{ theprintf"%d\n", Fenwicktree::query (ub-l)-fenwicktree::query (ub-R)); +                 } -             } the         }Bayi     } the     return 0; the}

Codeforces Round #263 (Div. 1) c. Appleman and a Sheet of Paper tree-like array violence update

Related Article

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.