Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

Source: Internet
Author: User

Title Address: Http://codeforces.com/contest/461/problem/C

Main topic: See the original question.

Algorithm analysis: Heuristic violence, each time the short paper tape folded into a long paper tape, in the global record a Rev mark. Attention to detail.

Code:

#include <cstdio> #include <algorithm> #define N 100000using namespace Std;bool rev;int n,q,beg=1,end,typ,p, l,r,bit[n+10];inline void Add (int x,int y) {for (; x<=n;x+=x&-x) bit[x]+=y;} inline int query (int x) {int res=0;for (; x;x-=x&-x) Res+=bit[x];return Res;} int main () {scanf ("%d%d", &n,&q), for (int i=1;i<=n;++i) Add (i,1), end=n;for (int i=1;i<=q;++i) {scanf ("%d" , &typ), if (typ==1) {scanf ("%d", &p), if (p<= (end-beg+1)/2) if (rev) {p=end-p;for (int j=end;j>p;--j) Add ( P-j+1,query (j)-query (j-1)); end=p;} else{p+=beg-1;for (int j=beg;j<=p;++j) Add (2*p-j+1,query (j)-query (j-1)); beg=p+1;} Else{if (rev) {p=end-p;for (int j=beg;j<=p;++j) Add (2*p-j+1,query (j)-query (j-1)); beg=p+1;} else{p+=beg-1;for (int j=end;j>p;--j) Add (2*p-j+1,query (j)-query (j-1)); end=p;} Rev^=1;}} ELSE{SCANF ("%d%d", &l,&r), if (rev) L=end-l,r=end-r;else L+=beg-1,r+=beg-1;if (l>r) Swap (l,r);p rintf ("%d\n ", query (R)-query (L));} <debug>/*puts (""); for (int j=beg;j<=end;++j) printf ("--");p UTS (""); for (int. j=beg;j<=end;++j) printf ("%d", query (j)-query (j-1));p UTS (""); for (int j=beg;j &LT;=END;++J) printf ("--");p UTS ("");p rintf ("BEGIN @%d end @%d REV%s\n", Beg,end,rev? " Y ":" N ");p UTS (" "); *///</debug>}return 0;}

by Charlie Pan

27,2014

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

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.