[GXOI/GZOI2019]与或和
Posted luoyibujue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GXOI/GZOI2019]与或和相关的知识,希望对你有一定的参考价值。
/*
显然位之间会互不影响, 然后分位来统计, 显然&只有全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]与或和的主要内容,如果未能解决你的问题,请参考以下文章
luogu P5300 [GXOI/GZOI2019]与或和
[LuoguP5305][GXOI/GZOI2019]旧词 (树链剖分)