# Luogu P5300 [GXOI/GZOI2019]与或和

CODE

#include<cstdio>
#include<cctype>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int N=1005,mod=1e9+7;
int n,a[N][N],and_ans,or_ans,mx,h[N][N],st[N],sum[N],val[N],top,cur;
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));

}
#undef tc
}F;
inline void inc(int& x,CI y)
{
if ((x+=y)>=mod) x-=mod;
}
{
int t=x+y; return t>=mod?t-mod:t;
}
inline int solve_and(CI bs,int ret=0)
{
RI i,j; for (i=1;i<=n;++i) for (j=1;j<=n;++j)
if (a[i][j]&bs) h[i][j]=h[i-1][j]+1; else h[i][j]=0;
for (i=1;i<=n;++i) for (top=0,j=1;j<=n;++j) if (!h[i][j]) top=0; else
{
int high=1; while (top&&st[top]>h[i][j]) high+=val[top--];
inc(ret,1LL*h[i][j]*high%mod); inc(ret,sum[top]); st[++top]=h[i][j];
}
return 1LL*ret*bs%mod;
}
inline int solve_or(CI bs,int ret=0)
{
RI i,j; for (i=1;i<=n;++i) for (j=1;j<=n;++j)
if (!(a[i][j]&bs)) h[i][j]=h[i-1][j]+1; else h[i][j]=0;
for (i=1;i<=n;++i) for (top=0,j=1;j<=n;++j) if (!h[i][j]) top=0; else
{
int high=1; while (top&&st[top]>h[i][j]) high+=val[top--];
inc(ret,1LL*h[i][j]*high%mod); inc(ret,sum[top]); st[++top]=h[i][j];
}