voidadd(int x1, int y1, int x2, int y2){ a[x1][y1]++; a[x1][y2 + 1]--; a[x2 + 1][y1]--; a[x2 + 1][y2 + 1]++; }
intsolve(){ memset(a, 0, sizeof a); for (int i = 1; i <= n; ++i) { int L = INF, R = -INF; for (int j = 1; j <= n; ++j) { if (s[i][j] == 'B') { L = min(L, j); R = max(R, j); } } if (L != INF) { if (R - L + 1 <= k) { add(max(1, i - k + 1), max(1, R - k + 1), i, L); } } else { add(1, 1, n, n); } } for (int j = 1; j <= n; ++j) { int L = INF, R = -INF; for (int i = 1; i <= n; ++i) { if (s[i][j] == 'B') { L = min(L, i); R = max(R, i); } } if (L != INF) { if (R - L + 1 <= k) { add(max(1, R - k + 1), max(1, j - k + 1), L, j); } } else { add(1, 1, n, n); } } int res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; res = max(res, a[i][j]); } } return res;
}
intmain(){ #ifdef LOCAL_JUDGE freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); while (cin >> n >> k) { for (int i = 1; i <= n; ++i) { cin >> (s[i] + 1); } cout << solve() << endl; } return0; }
#define N 1010 #define M 2560 #define INF 0x3f3f3f3f
constint p = 2520;
int n, m, k[N], q; vector<vector<int> > G; int f[N * M];
intgao(int x, int y){ int now = x * p + y; if (f[now] != -1) { return f[now]; } set<int> se; stack<int> st; se.insert(now); st.push(now); while (true) { x = now / p, y = now % p; int ny = ((y + k[x]) % p + p) % p; int nx = G[x][ny % G[x].size()]; int nxt = nx * p + ny; if (f[nxt] != -1) { while (!st.empty()) { f[st.top()] = f[nxt]; st.pop(); } return f[nxt]; } if (se.find(nxt) != se.end()) { set<int> tmp; while (true) { int Top = st.top(); tmp.insert(Top / p); st.pop(); if (Top == nxt) { break; } } for (auto it : se) { f[it] = tmp.size(); } return tmp.size(); } now = nxt; st.push(now); se.insert(now); } return0; }
intmain(){ #ifdef LOCAL_JUDGE freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); while (cin >> n) { G.clear(); G.resize(n + 1); for (int i = 1; i <= n; ++i) { cin >> k[i]; } for (int i = 1; i <= n; ++i) { cin >> m; G[i].resize(m); for (auto &it : G[i]) { cin >> it; } } memset(f, -1, sizeof f); cin >> q; while (q--) { int x, y; cin >> x >> y; y = (y % p + p) % p; cout << gao(x, y) << "\n"; } } return0; }