Each time only the short part to the long part of the folding, directly with a tree-like array to blow it up.
Every length shrinks some of the complexity of the violence is not too high, heuristic violence????
C. Appleman and a Sheet of Papertime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputsta Ndard output
Appleman has a very big sheet of paper. This sheet have a form of rectangle with dimensions 1?x? 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:
- Fold the sheet of paper at position Pi . After this query, the leftmost part of the paper with dimensions1?x? P I Must is above the rightmost part of the paper with dimensions1?x? ([Currentwidth of sheet]?-? P i).
- count What's the total width of The paper pieces, if we'll make both described later cuts and consider only the pieces between the cuts. We'll make one cut at Distance l i from The border of the current sheet of paper and the other at distance R i from The 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 Integers: n and 5 ; 1?≤? q ? ≤?105 )-the width of the paper and the number of queries.
Eac H of the Following
- "1 pi" (1?≤? Pi? <? [Currentwidth of sheet]) -the first type query.
- "2 li Ri "(0?≤? ) Li? <? R i? ≤? [Currentwidth of sheet])-the second type query.
Output
For each query of the second type, output the answer.
Sample Test (s) input
7 41 31 22 0 12 1 2
Output
43
Input
10 92 2 91 12 0 11 82 0 81 22 1 31 42 2 4
Output
721045
Note
The pictures below show the shapes of the paper during the queries of the first example:
After the first fold operation the sheet have width equal to 4, after the second one the width of the sheet equals To 2.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=101000;int a[maxn],n,q;inline int lowbit (int x) {return x& (-X);} void Add (int p,int v) {for (int i=p;i<=n;i+=lowbit (i)) a[i]+=v;} int sum (int p) {int sum=0;for (int i=p;i;i-=lowbit (i)) Sum+=a[i];return sum;} int main () {scanf ("%d%d", &n,&q), int flag=0;for (int i=1;i<=n;i++) Add (i,1); int Beg=1,end=n;while (q--) {int k , p,l,r;scanf ("%d", &k), if (k==1) {scanf ("%d", &p), if (p<= (end-beg+1)/2) {if (flag==0) {for (int i=p+beg-1,j=p +beg;i>=beg;i--, J + +) {Add (J,sum (i)-sum (i-1));} Beg=beg+p;} else{for (int i=end-p,j=end-p+1;j<=end;i--, J + +) {Add (I,sum (j)-sum (J-1));} End=end-p;}} Else{if (flag==0) {for (int i=beg+p-1,j=p+beg;j<=end;j++,i--) {Add (I,sum (j)-sum (J-1));} End=beg+p-1;} else{for (int i=end-p,j=end-p+1;i>=beg;i--, J + +) {Add (J,sum (i)-sum (i-1));} beg=end-p+1;} flag=flag^1;}} else if (k==2) {scanf ("%d%d", &l,&r), L++;if (flag==0) {l=beg+l-1; r=beg+r-1;printf ("%d\n ", sum (r)-sum (L-1));} else{l=end-l+1; r=end-r+1;printf ("%d\n", SUM (L)-sum (r-1));}} return 0;}
Codeforces 461C. Appleman and a Sheet of Paper