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 <=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