【LOJ】#3083. 「GXOI / GZOI2019」与或和

LOJ#3083. 「GXOI / GZOI2019」与或和

显然是先拆位,AND的答案是所有数字为1的子矩阵的个数

OR是所有的子矩阵个数减去所有数字为0的子矩阵的个数

子矩阵怎么求可以记录每个位置能向上延伸的高度\(h[i][j]\)

枚举左下角的端点,用一个单调栈维护即可

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,all;
int a[1005][1005],b[1005][1005],ans[2],h[1005],sta[1005],top;
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &a,int b) {
    a = inc(a,b);
}
int getall(int x) {
    memset(h,0,sizeof(h));
    int res = 0,sum = 0;
    for(int i = 1 ; i <= N ; ++i) {
    sum = 0;top = 0;sta[0] = N + 1;
    for(int j = N ; j >= 1 ; --j) {
        if(b[i][j] == x) h[j]++;
        else h[j] = 0;
        while(top && h[sta[top]] >= h[j]) {
        update(sum,MOD - mul(sta[top - 1] - sta[top],h[sta[top]]));
        --top;
        }
        sta[++top] = j;
        update(sum,mul(sta[top - 1] - sta[top],h[j]));
        update(res,sum);
    }
    }
    return res;
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
    for(int j = 1 ; j <= N ; ++j) {
        read(a[i][j]);
        update(all,mul(N - i + 1,N - j + 1));
    }
    }
    for(int i = 30 ; i >= 0 ; --i) {
    for(int j = 1 ; j <= N ; ++j) {
        for(int h = 1 ; h <= N ; ++h) {
        b[j][h] = a[j][h] >> i & 1;
        }
    }
    int t = (1 << i) % MOD;
    update(ans[0],mul(t,getall(1)));
    update(ans[1],mul(t,inc(all,MOD - getall(0))));
    }
    out(ans[0]);space;out(ans[1]);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

转载于:https://www.cnblogs.com/ivorysi/p/10976403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值