小数据 WA 大数据 AC
这个暴力根据 InfOJ 的测评情况来看,应该是 多测没有清空导致的(理由:第一组测试数十万组询问一个不错,第二组测试第一个开始就错了)
#include<map>
#include<queue>
#include<cstdio>
#include<cassert>
#include<cstring>
int init(){
char c = getchar();
int x = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 1) + (x << 3) + (c & 15);
return x * f;
}
void print(int x){
if (x < 0) putchar('-'), x = -x;
if (x / 10) print(x / 10);
putchar(x % 10 + '0');
}
const int N = (int) 2e5 + 5, M = (int) 5e5 + 5;
int n, m, q;
int node(int i, int j){
return (i - 1) * m + j;
}
struct Node{
int next, to, value;
}s[M << 4]; int sLen, head[M << 4];
void add(int u, int v, int w){
s[++sLen].next = head[u];
s[sLen].to = v; s[sLen].value = w;
head[u] = sLen;
}
struct Edge{
int x, y, z, LV, COL;
friend bool operator<(const Edge&p, const Edge&q){
if (p.x != q.x) return p.x < q.x;
if (p.y != q.y) return p.y < q.y;
if (p.z != q.z) return p.z < q.z;
if (p.LV != q.LV) return p.LV < q.LV;
return p.COL < q.COL;
}
};
int col[M << 4], lv[M << 4];
int go(int x, int y){
std::map<Edge, bool>map;
map.clear();
std::map<int, bool>vis;
vis.clear();
int u = node(x, y), Ans = 0; vis[u] = 1;
std::queue<Edge>queue;
while (!queue.empty()) queue.pop();
for (int i = head[u]; i; i = s[i].next) {
int v = s[i].to, _w = s[i].value;
int nowLv = lv[u], nowCol = col[u];
int toLv = lv[v], toCol = col[v];
if (toLv == -1 && toCol == -1) {
Edge to = (Edge){v, u, _w, nowLv, nowCol};
map[to] = 1;
if (!vis.count(v)) ++Ans, vis[v] = 1;
if (_w != 1) queue.push(to);
}
else if (nowLv >= toLv && nowCol != toCol) {
map[(Edge){v, u, _w, nowLv, nowCol}] = 1;
if (!vis.count(v)) ++Ans, vis[v] = 1;
}
}
while (!queue.empty()) {
Edge p = queue.front(); queue.pop();
int _p = p.x, _q = p.y, _w = p.z, LV = p.LV, COL = p.COL;
if (_w == 1) continue;
for (int i = head[_p]; i; i = s[i].next) {
int to = s[i].to;
if (_w != s[i].value) continue;
if (_w == 2 && (_q + to) / 2 != _p) continue;
if (map.count((Edge){to, _p, _w, LV, COL})) continue;
int toLv = lv[to], toCol = col[to];
if (toLv == -1 && toCol == -1) {
map[(Edge){to, _p, _w, LV, COL}] = 1;
if (!vis.count(to)) ++Ans, vis[to] = 1;
queue.push((Edge){to, _p, _w, LV, COL});
}
else if (LV >= toLv && COL != toCol) {
map[(Edge){to, _p, _w, LV, COL}] = 1;
if (!vis.count(to)) ++Ans, vis[to] = 1;
}
}
}
return Ans;
}
int main(){
int T = init();
while (T--) {
n = init(), m = init(), q = init();
memset(head, 0, sizeof(head));
sLen = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j < m; ++j) {
int c; scanf("%1d", &c);
if (c == 1) {
add(node(i, j), node(i, j + 1), 1);
add(node(i, j + 1), node(i, j), 1);
}
else if (c == 2) {
add(node(i, j), node(i, j + 1), 2);
add(node(i, j + 1), node(i, j), 2);
}
else if (c == 3) {
add(node(i, j), node(i, j + 1), 3);
add(node(i, j + 1), node(i, j), 3);
}
}
for (int i = 1; i < n; ++i)
for (int j = 1; j <= m; ++j) {
int c; scanf("%1d", &c);
if (c == 1) {
add(node(i, j), node(i + 1, j), 1);
add(node(i + 1, j), node(i, j), 1);
}
else if (c == 2) {
add(node(i, j), node(i + 1, j), 2);
add(node(i + 1, j), node(i, j), 2);
}
else if (c == 3) {
add(node(i, j), node(i + 1, j), 3);
add(node(i + 1, j), node(i, j), 3);
}
}
memset(col, -1, sizeof(col));
memset(lv, -1, sizeof(lv));
for (int i = 1; i <= q; ++i) {
int coli = init(), lvi = init(), xi = init(), yi = init();
assert(1 <= xi && xi <= n);
assert(1 <= yi && yi <= m);
col[node(xi, yi)] = coli;
lv[node(xi, yi)] = lvi;
print(go(xi, yi)), putchar('\n');
}
}
}
可是我却没有找到多测未清空的地方,请问各位可以帮忙看看吗?