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

本文提供了一种解决CodeForces平台C题的有效算法——启发式暴力法,并附带详细的C++实现代码。该方法通过将较短的纸带折叠到较长的纸带中来解决问题,同时使用了一个全局变量rev来跟踪状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://codeforces.com/contest/461/problem/C

题目大意:见原题。

算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记。注意细节。

Code:

#include <cstdio>
#include <algorithm>

#define N 100000

using 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(2*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);
			printf("%d\n",query(r)-query(l));
		}
		//<debug>
			/*puts("");
			for (int j=beg;j<=end;++j) printf("--");puts("");
			for (int j=beg;j<=end;++j) printf("%d ",query(j)-query(j-1));puts("");
			for (int j=beg;j<=end;++j) printf("--");puts("");
			printf("BEGIN @ %d END @ %d REV %s\n",beg,end,rev?"Y":"N");
			puts("");*/
		//</debug>
	}
	return 0;
}

By Charlie Pan

Aug 27,2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值