ABC331D 题解
首先把询问全部
利用容斥,把询问变成询问左上角为
考虑如何计算
接下来计算剩余部分的贡献,把
#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i++)
#define req(i, r, l) for(int i = (r); i >= (l); i--)
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define all(x) begin(x), end(x)
using namespace std;
constexpr int N = 1e5 + 10, inf = 0x3f3f3f3f, INF = 0x7f7f7f7f; constexpr double eps = 1e-9;
int n, q, cnt;
int sum[1010][1010];
ll q1(int x, int y) {
ll t1 = x / n, t2 = y / n;
ll k = 1ll * t1 * t2; ll res = k * cnt;
x %= n, y %= n;
res += sum[x][n] * t2, res += sum[n][y] * t1;
res += sum[x][y];
return res;
}
signed main() {
ios :: sync_with_stdio(0);
cin >> n >> q;
rep(i, 1, n) {
rep(j, 1, n) {
char c; cin >> c;
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (c == 'B');
if(c == 'B') cnt++;
}
}
while(q--) {
int a, b, c, d;
cin >> a >> b >> c >> d; a++, b++, c++, d++;
cout << q1(c, d) - q1(a - 1, d) - q1(c, b - 1) + q1(a - 1, b - 1) << "\n";
}
return 0;
}