P9198题解

· · 题解

题目大意

给定一个 n \times m 的矩阵,每个点有三个值,每次操作可将一个点的一个值改变,在每次操作过后,请你输出图片是否左右对称?

题目分析

我们可以按照题目要求每次更改一个数,再看是否对称。

时间复杂度 \mathcal O(q \times n \times m)

我们发现,每回只改一个值,所以我们可以记一个 cnt 表示矩阵共有 cnt 对点左右不对称,每次更改一个数后有如下四种情况:

  1. 原来这个点对称,后来这个点依旧对称(改了跟没改一样):cnt 不变。
  2. 原来这个点对称,后来这个点不对称(改不对称了):cnt + 1
  3. 原来这个点不对称,后来这个点依不旧对称(好像改了,但是没完全改):cnt 不变。
  4. 原来这个点不对称,后来这个点对称(改对称了):cnt-1

每次改完后,如果 cnt = 0 即没有点不对称,输出 Yes

否则说明有点不对称,输出 No

code

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 105, MOD = 256;
struct node{
    int a[4];
}p[N][N];
int n, m, q, x, y, t, c, cnt;

bool operator == (node u, node v)
{
    for(int i = 1;i <= 3;i++)
        if(u.a[i] != v.a[i])
            return false;
    return true;
}

int main()
{
    scanf("%d %d %d", &n, &m, &q);
    for(int i = 1;i <= q;i++)
    {
        scanf("%d %d %d %d", &x, &y, &t, &c);
        if(p[x][y].a[t] == p[x][m - y + 1].a[t])
        {
            (p[x][y].a[t] += c) %= MOD;
            cnt += (p[x][y].a[t] == p[x][m - y + 1].a[t]) ? 0 : 1;
        }
        else
        {
            (p[x][y].a[t] += c) %= MOD;
            cnt += (p[x][y].a[t] == p[x][m - y + 1].a[t]) ? -1 : 0;
        }
        (cnt == 0) ? printf("Yes\n") : printf("No\n");
    }
    return 0;
}