某人写 ABC331D 实录
DE_aemmprty · · 个人记录
奇文共赏!!!
发现可以把正方形剖成 9 个部分,然后随便前缀和一下就做完了!!!
出题人好低能。开写。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
char c = getchar();
int x = 0, p = 1;
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') p = -1, c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * p;
}
const int N = 1007;
int n, q;
long long p[N][N], s[N][N];
long long col[N];
long long row[N];
long long gets(int ax, int ay, int bx, int by) {
return s[bx + 1][by + 1] - s[ax][by + 1] - s[bx + 1][ay] + s[ax][ay];
}
long long getcol(int x, int y) {
return col[x + 1] - col[y];
}
long long getrow(int x, int y) {
return row[y + 1] - row[x];
}
long long pcal(int ax, int ay, int bx, int by) {
// printf("output %d %d\n", ax, bx);
int pay = ay / n, pby = by / n;
if (pay == pby) return gets(ax % n, ay % n, bx % n, by % n);
int res = gets(ax % n, ay % n, bx % n, n - 1) + gets(ax % n, 0, bx % n, by % n);
// printf("res = %lld\n", res);
res += getrow(ax % n, bx % n) * (pby - pay - 1);
printf("res = %lld\n", res);
return res;
}
long long cal(int ax, int ay, int bx, int by) {
long long res = 0;
int pax = ax / n, pay = ay / n, pbx = bx / n, pby = by / n;
// printf("p : %d %d %d %d\n", pax, pbx, pay, pby);
if (pax == pbx && pay == pby) return gets(ax % n, ay % n, bx % n, by % n);
res += pcal(ax % n, ay, (pbx > pax ? n - 1 : bx % n), by);
if (pbx > pax) res += pcal(0, ay, n - 1, by) * (pbx - pax - 1);
if (pbx > pax) res += pcal(0, ay, bx % n, by);
return res;
}
void solve() {
n = read(), q = read();
for (int i = 0; i < n; i ++) {
string ss; cin >> ss;
for (int j = 0; j < n; j ++) {
p[i][j] = (ss[j] == 'B' ? 1 : 0);
s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + p[i][j];
}
}
for (int i = 0; i < n; i ++) {
long long sum = 0;
for (int j = 0; j < n; j ++)
sum += p[i][j];
col[i + 1] = col[i] + sum;
}
for (int i = 0; i < n; i ++) {
long long sum = 0;
for (int j = 0; j < n; j ++)
sum += p[j][i];
row[i + 1] = row[i] + sum;
}
while (q --) {
int ax = read(), ay = read(), bx = read(), by = read();
printf("%lld\n", cal(ax, ay, bx, by));
}
}
signed main() {
int t = 1;
while (t --) solve();
return 0;
}
踏马的怎么过不了样例。扔了。
出题人煞笔。