Appleman and a Sheet of Paper
题解
由于一张纸条翻折掉的部分会消失,所以如果每次操作都将小的一半折到大的一部分上,时间复杂度应该是趋近的。
由于从右边往左边折时需要一个翻折操作,需要用个标记记录下来,如果暴力翻会T。
但询问操作要求一段区间的和,所以需要先拿一个树状数组去维护每一位,这样修改与查询的时间复杂度都成了。
不过其实暴力翻也可以不T,一些玄学优化可以卡过去。
源码
正解
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
typedef long long LL;
typedef pair<LL,LL> pii;
int n,q,sum[MAXN];
void Add(int pos,int w){while(pos<=n)sum[pos]+=w,pos+=lowbit(pos);}
int Sum(int pos){int res=0;while(pos>0)res+=sum[pos],pos-=lowbit(pos);return res;}
signed main(){
scanf("%d %d",&n,&q);int s=1,t=n,flip=0;
for(int i=1;i<=n;i++)Add(i,1);
for(int i=1;i<=q;i++){
int opt;scanf("%d",&opt);
if(opt==1){
int id;scanf("%d",&id);
if(flip){
if(id*2>t-s+1){flip^=1;id=t-s-id+1;s+=id;for(int j=1;j<=id;j++)Add(s+j-1,Sum(s-j)-Sum(s-j-1));}
else{for(int j=1;j<=id;j++)Add(t-2*id+j,Sum(t-j+1)-Sum(t-j));t-=id;}
}
else{
if(id*2>t-s+1){flip^=1;id=t-s+1-id;for(int j=1;j<=id;j++)Add(t-2*id+j,Sum(t-j+1)-Sum(t-j));t-=id;}
else{s+=id;for(int j=1;j<=id;j++)Add(s+j-1,Sum(s-j)-Sum(s-j-1));}
}
}
else{
int l,r,tmp;scanf("%d %d",&l,&r);
tmp=!flip?(Sum(r+s-1)-Sum(l+s-1)):(Sum(t-l)-Sum(t-r));
printf("%d\n",tmp);
}
}
return 0;
}
卡过去的代码,1980ms还是很险恶的
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
typedef long long LL;
typedef pair<LL,LL> pii;
int n,q,a[MAXN],ll,rr,val[MAXN<<2];
void read(int &x){
int f=1;x=0;char s=getchar();
while('0'>s||s>'9'){if(s=='-')f=-1;s=getchar();}
while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
x*=f;
}
void build(int rt,int l,int r){
if(l==r){val[rt]=a[l+ll-1];return ;}
int mid=l+r>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
val[rt]=val[rt<<1]+val[rt<<1|1];
//printf("%d %d %d:%lld\n",rt,l,r,val[rt]);
}
int query(int rt,int l,int r,int al,int ar){
if(l>r||l>ar||r<al)return 0;
if(al<=l&&r<=ar)return val[rt];
int mid=l+r>>1;LL sum=0;
if(al<=mid)sum+=query(rt<<1,l,mid,al,ar);
if(ar>mid)sum+=query(rt<<1|1,mid+1,r,al,ar);
return sum;
}
signed main(){
read(n);read(q);
for(int i=1;i<=n;i++)a[i]=1;ll=1;rr=n;bool flag=false;
for(int i=1;i<=q;i++){
int opt;read(opt);
if(opt==1){
int p,mid;read(p);flag=false;mid=ll+p;
if(p<=(rr-ll+1)/2){for(int j=1;j<=p;j++)a[mid+j-1]+=a[mid-j];ll=mid;}
else{for(int j=1;j<=rr-mid+1;j++)a[mid-j]+=a[mid+j-1];reverse(a+ll,a+mid);rr=mid-1;}
}
else{
if(!flag){build(1,1,rr-ll+1);flag=true;}
int L,R;read(L);read(R);
printf("%d\n",query(1,1,rr-ll+1,L+1,R));
}
//for(int i=ll;i<=rr;i++)printf("%d ",a[i]);puts("");
}
return 0;
}