Codeforces 461C. Appleman and a Sheet of Paper

Source: Internet
Author: User


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:

  1. 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).
  2. 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

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.