码迷,mamicode.com
首页 > 其他好文 > 详细

[GXOI/GZOI2019]与或和

时间:2019-04-23 17:17:13      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:pre   read   ios   cout   i++   queue   string   getch   lse   

/*
显然位之间会互不影响, 然后分位来统计, 显然&只有全1才有贡献, 显然|只有全0才没贡献
分别n^2处理即可


*/

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define ll long long
#define M 1010
using namespace std;
int read() {
    int nm = 0, f = 1;
    char c = getchar();
    for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    return nm * f;
}
const int mod = 1000000007;
void add(int &a, int b) {
    a += b;
    a -= a >= mod ? mod : 0;
    a += a < 0 ? mod : 0;
}
int mul(int a, int b) {
    return 1ll * a * b % mod;
}
int a[M][M], ans, ans2, b[M][M];
int que[M], l, r, n;
int work() {
#define h b
    int tmp = 0, sum = 0;
    for(int i = 1; i <= n; i++) {
        sum = 0;
        l = 1, r = 0;
        for(int j = 1; j <= n; j++) {
            while(l <= r && h[i][que[r]] >= h[i][j]) {
                sum -= (que[r] - que[r - 1]) * h[i][que[r]];
                r--;
            }
            que[++r] = j;
            sum += (j - que[r - 1]) * h[i][j];
            add(tmp, sum);
        }
    }
    return tmp;
}


int main() {
    n = read();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            a[i][j] = read();
            add(ans2, mul(i, mul(j, (1ll << 31) - 1ll)));
        }
    }
    for(int k = 0; k <= 30; k++) {
        int tmp = 0, tmd = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(a[i][j] & (1 << k)) b[i][j] = 1;
                else b[i][j] = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(b[i][j] == 1) b[i][j] += b[i - 1][j];
            }
        }
        add(tmp, work());
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(a[i][j] & (1 << k)) b[i][j] = 0;
                else b[i][j] = 1;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(b[i][j] == 1) b[i][j] += b[i - 1][j];
            }
        }
        add(tmd, work());
        add(ans, mul(1 << k, tmp));
        add(ans2, -mul(1 << k, tmd));
    }
    cout << ans << " " << ans2 << "\n";
    return 0;
}
/*
3
0 0 0
0 0 0
0 0 0

1 
0

2
0 0 
0 0
*/

[GXOI/GZOI2019]与或和

标签:pre   read   ios   cout   i++   queue   string   getch   lse   

原文地址:https://www.cnblogs.com/luoyibujue/p/10757412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!