洛谷10月月赛R2·浴谷八连测R3 -Chtholly- 1、2题题解

先上成绩:(神犇不要嘲笑蒟蒻)
这里写图片描述

第1题题解:

挺简单的吧,但我还是写了很久,预处理几个前缀和就可以每次询问 O(1) 得到答案了,看看代码就好了。这个模数似乎有点暴力。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int Maxn=200010;
const LL mod=19260817;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
LL s1[Maxn],s2[Maxn],s3[Maxn],a[Maxn],b[Maxn];
LL ss1[Maxn],ss2[Maxn],ss3[Maxn];
LL g1(int x,int l,int r)
{
    LL ans;
    ans=(s3[r]-s3[l-1]+mod)%mod;
    ans=(ans-((s2[r]-s2[l-1]+mod)%mod)*s1[x]%mod+mod)%mod;
    return ans;
}
LL g2(int x,int l,int r)
{
    LL ans;
    ans=(ss3[l]-ss3[r+1]+mod)%mod;
    ans=(ans-((ss2[l]-ss2[r+1]+mod)%mod)*ss1[x]%mod+mod)%mod;
    return ans;
}
int main()
{
    n=read();m=read();
    s1[0]=s1[1]=s2[0]=s3[0]=0;
    ss1[n]=ss1[n+1]=ss2[n]=ss3[n]=0;
    for(int i=2;i<=n;i++)s1[i]=(s1[i-1]+(LL)(a[i]=read()))%mod;
    for(int i=1;i<=n;i++)
    {
        b[i]=(LL)(read());
        s2[i]=(s2[i-1]+b[i])%mod;
        s3[i]=(s3[i-1]+s1[i]*b[i])%mod;
    }
    for(int i=n-1;i;i--)ss1[i]=(ss1[i+1]+a[i+1])%mod;
    for(int i=n;i;i--)
    {
        ss2[i]=(ss2[i+1]+b[i])%mod;
        ss3[i]=(ss3[i+1]+ss1[i]*b[i])%mod;
    }
    while(m--)
    {
        int x=read(),l=read(),r=read();
        LL ans;
        if(l>=x)ans=g1(x,l,r);
        else if(r<=x)ans=g2(x,l,r);
        else ans=(g1(x,x,r)+g2(x,l,x-1))%mod;
        printf("%lld\n",ans);
    }
}

第二题题解:

考试的时候想到了二分,但是写了一个错误的check方法,但还是水了40分,正确的check应该是这样的:先找出矩阵的最大最小值,然后对于二分出来的值 x ,我们采取这样的方法做标记(以样例为例,假设当前二分到的x就是答案11):
对于每一行,从左到右标记所有小于等于 min+x 的数,有不满足的就到下一行,然后每行结束的位置一定要比上一行左。如样例:

     1 12 6 1111 4 210 1 94        
这些数就被标记了。
然后再反过来,从最后一行开始,对所有大于等于 maxx 的数,采取类似的方法标记。如样例:
          11          14       9 2017 13 10
这些数就被标记了。
然后,若整个矩阵都被标记了,那么这个 x 可行(正确性显然)。但是仅仅这样做是不行的,还要第一次先标记maxx,然后再标记 min+x 来一次,除此之外还要交换左右再来两次,就行了。(讲的不清楚,最好看代码)

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int Maxn=2010;
const int inf=1e9;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,a[Maxn][Maxn],mx=-inf,mn=inf,mark[Maxn][Maxn],tot;
bool check(int x)
{
    memset(mark,-1,sizeof(mark));tot=n*m;
    int last=m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=last;j++)
        if(a[i][j]<=mn+x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=0;
        }
        else{last=j-1;break;}
    }
    last=1;
    for(int i=n;i;i--)
    {
        for(int j=m;j>=last;j--)
        if(a[i][j]>=mx-x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=1;
        }
        else{last=j+1;break;}
    }
    if(!tot)return true;
    /******************************/
    memset(mark,-1,sizeof(mark));tot=n*m;
    last=m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=last;j++)
        if(a[i][j]>=mx-x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=0;
        }
        else{last=j-1;break;}
    }
    last=1;
    for(int i=n;i;i--)
    {
        for(int j=m;j>=last;j--)
        if(a[i][j]<=mn+x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=1;
        }
        else{last=j+1;break;}
    }
    if(!tot)return true;
    /******************************/
    memset(mark,-1,sizeof(mark));tot=n*m;
    last=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=last;j--)
        if(a[i][j]<=mn+x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=0;
        }
        else{last=j+1;break;}
    }
    last=m;
    for(int i=n;i;i--)
    {
        for(int j=1;j<=last;j++)
        if(a[i][j]>=mx-x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=1;
        }
        else{last=j-1;break;}
    }
    if(!tot)return true;
    /******************************/
    memset(mark,-1,sizeof(mark));tot=n*m;
    last=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=last;j--)
        if(a[i][j]>=mx-x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=0;
        }
        else{last=j+1;break;}
    }
    last=m;
    for(int i=n;i;i--)
    {
        for(int j=1;j<=last;j++)
        if(a[i][j]<=mn+x)
        {
            if(mark[i][j]==-1)tot--;
            mark[i][j]=1;
        }
        else{last=j-1;break;}
    }
    if(!tot)return true;
    return false;
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    a[i][j]=read(),mx=max(mx,a[i][j]),mn=min(mn,a[i][j]);
    int l=0,r=inf;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(check(mid))r=mid-1;
        else l=mid+1;
    }printf("%d",r+1);
}

第三题:

不会,附上60分暴力代码(过了2、3、4、5、7、8)。讲道理写高精度可以过第一个点。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int Maxn=500010;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
LL a[Maxn],s[Maxn];
void add(int x,LL y){for(;x<=n;x+=(x&-x))s[x]+=y;}
LL getsum(int x,LL mod)
{
    LL re=0;
    if(mod){for(;x;x-=(x&-x))re=(re+s[x])%mod;}
    else {for(;x;x-=(x&-x))re+=s[x];}
    return re;
}
LL Pow(LL x,LL y)
{
    if(y==0)return 1;
    if(y==1)return x;
    LL t=Pow(x,y>>1),ans=t*t;
    if(y&1)ans=ans*x;
    return ans;
}
LL Pow_mod(LL x,LL y,LL mod)
{
    if(y==0)return 1%mod;
    if(y==1)return x%mod;
    LL t=Pow_mod(x,y>>1,mod),ans=t*t%mod;
    if(y&1)ans=ans*x%mod;
    return ans;
}
LL work(int l,int r,LL mod)
{
    if(l==r)return a[l]%mod;
    LL ans=Pow(a[r-1],a[r]);
    for(int i=r-2;i>=l;i--)
    {
        if(i>l)ans=Pow(a[i],ans);
        else ans=Pow_mod(a[i],ans,mod);
    }
    return ans%mod;
}
struct Ask{int op,l,r,x;}q[Maxn];
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)a[i]=(LL)(read());
    if(n==5&&m==5)
    {
        while(m--)
        {
            int op=read(),l=read(),r=read(),x=read();
            if(op==1)for(int i=l;i<=r;i++)a[i]+=(LL)(x);
            else printf("%lld\n",work(l,r,(LL)(x)));
        }
    }
    else
    {
        int mx=0;bool all=true;
        for(int i=1;i<=m;i++)
        {
            q[i].op=read(),q[i].l=read(),q[i].r=read(),q[i].x=read();
            if(q[i].op==2&&q[i].x!=2)all=false;
            if(q[i].op==2)mx=max(mx,q[i].r-q[i].l+1);
        }
        if(mx==1)
        {
            a[0]=0;
            for(int i=1;i<=n;i++)add(i,a[i]-a[i-1]);
            for(int i=1;i<=m;i++)
            {
                if(q[i].op==1)add(q[i].l,(LL)(q[i].x)),add(q[i].r+1,(LL)(-q[i].x));
                else printf("%lld\n",getsum(q[i].l,(LL)(q[i].x)));
            }
        }
        else if(mx==2)
        {
            a[0]=0;
            for(int i=1;i<=n;i++)add(i,a[i]-a[i-1]);
            for(int i=1;i<=m;i++)
            {
                if(q[i].op==1)add(q[i].l,(LL)(q[i].x)),add(q[i].r+1,(LL)(-q[i].x));
                else 
                {
                    if(q[i].r==q[i].l)printf("%lld\n",getsum(q[i].l,(LL)(q[i].x)));
                    else printf("%lld\n",Pow_mod(getsum(q[i].l,q[i].x),getsum(q[i].r,0),q[i].x));
                }
            }
        }
        else if(all)
        {
            a[0]=0;
            for(int i=1;i<=n;i++)add(i,a[i]-a[i-1]);
            for(int i=1;i<=m;i++)
            {
                if(q[i].op==1)add(q[i].l,(LL)(q[i].x)),add(q[i].r+1,(LL)(-q[i].x));
                else printf("%lld\n",getsum(q[i].l,2LL));
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值