• 【洛谷P5300】【GXOI/GZOI2019】—与或和(单调栈)


    传送门

    每一位O(n2)O(n^2)做一下就完了

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    #define poly vector<int>  
    cs int mod=1e9+7;
    inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
    inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
    inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
    inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
    inline int mul(int a,int b){return 1ll*a*b%mod;}
    inline void Mul(int &a,int b){a=1ll*a*b%mod;}
    inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
    inline int Inv(int x){return ksm(x,mod-2);}
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    cs int N=1005;
    int n,a[N][N],f[N][N],up[N][N],ans1,ans2,tmp;
    int q[N],top;
    inline int calc(){
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++)
    		up[i][j]=(f[i][j]==0)?0:(up[i-1][j]+1);
    	}
    	int res=0;
    	for(int i=1;i<=n;i++){
    		int tot=0,top=0,*p=up[i];
    		for(int j=1;j<=n;j++){
    			while(top&&p[j]<=p[q[top]])Dec(tot,mul(dec(q[top],q[top-1]),p[q[top]])),top--;
    			q[++top]=j,Add(tot,mul(dec(q[top],q[top-1]),p[j]));
    			Add(res,tot);
    		}
    	}
    	return res;
    }
    int main(){
    	#ifdef Stargazer
    	freopen("lx.cpp","r",stdin);
    	#endif
    	n=read();int tot=n*(n+1)/2;tot=mul(tot,tot);
    	for(int i=1;i<=n;i++)
    	for(int j=1;j<=n;j++)
    	a[i][j]=read();
    	for(int t=30;~t;t--){
    		for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    		f[i][j]=(bool)(a[i][j]&(1<<t));
    		Add(ans1,mul(calc(),(1<<t)%mod));
    		for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    		f[i][j]^=1;
    		Add(ans2,mul(dec(tot,calc()),(1<<t)%mod));
    	}
    	cout<<ans1<<" "<<ans2<<'
    ';
    }
    
  • 相关阅读:
    NLog.Config的配置示例
    极限定义新讲:动态定义与静态定义
    rclone命令
    linux用户管理命令
    vim常用命令
    Python解二元二次方程组
    英雄联盟为什么这么难玩?——认识所有英雄
    snap安装、运行ss
    阿里云邮件推送服务Python API发送邮件
    Visual Studio #include<bits/stdc++.h>万能头文件
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328490.html
Copyright © 2020-2023  润新知