CF 461C 模拟

Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:

Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper.
Please look at the explanation of the first test example for better understanding of the problem.

这里写图片描述

题意:有一根长为n的线,每次操作选左侧pi处折叠,每次查询以左侧顶点为起点查询[l,r]区间内总线的长度。

做法:显然折叠的操作使长度减少,这样就可以用1e5的空间存储线段,这里有一个trick:如果pi至左端点距离不到长度一半,直接翻转,否则从右往左翻转,这样做相当于将向右翻转的结果做一个镜像变换,以后操作时左右反置即可。
我一开始是改变记录位置的值,每次查询o(n),显然会T。只需记录前缀和每次查询就是o(1)了。
而每次翻转的更新逐个进行而不会T的原因是,每次的更新次数等于长度减小量,总更新次数是o(n)的。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int n,q;
int l,r;
int rec[105000];
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=0;i<=n;i++)
    {
        rec[i]=i;
    }
    l=0;r=n;
    bool ok=false;
    for(int i=1;i<=q;i++)
    {
        int jj;
        scanf("%d",&jj);
        if(jj==1)
        {
            int p;
            scanf("%d",&p);
            if(!ok)
            {
                if(p*2>(r-l))
                {
                    ok=true;
                    int f=p+l;
                    int sum=0;
                    for(int j=r;j>f;j--)
                    {
                        sum+=rec[j]-rec[j-1];
                        rec[2*f-j+1]+=sum;
                    }
                    r=f;
                }
                else{
                    int f=p+l;
                    int sum=0;
                    for(int j=f;j>=l+1;j--)
                    {
                        sum+=rec[j]-rec[j-1];
                        rec[2*f-j+1]+=(sum-rec[f]);
                    }
                    rec[f]=0;
                    l=f;
                }
            }
            else{
                if(p*2>(r-l))
                {
                    ok=false;
                    int f=r-p;
                    int sum=0;
                    for(int j=f;j>=l+1;j--)
                    {
                        sum+=rec[j]-rec[j-1];
                        rec[2*f-j+1]+=(sum-rec[f]);
                    }
                    rec[f]=0;
                    l=f;
                }
                else
                {
                    int f=r-p;
                    int sum=0;
                    for(int j=r;j>f;j--)
                    {
                        sum+=rec[j]-rec[j-1];
                        rec[2*f-j+1]+=sum;
                    }
                    r=f;
                }
            }
        }
        else {
            int ll,rr;
            scanf("%d%d",&ll,&rr);
            if(!ok)
            {
                int sum=0;
                sum=rec[l+rr]-rec[l+ll];
                printf("%d\n",sum);
            }

            else{
                int sum=0;
                sum=rec[r-ll]-rec[r-rr];
                printf("%d\n",sum);
            }
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值