제출 #559200

#제출 시각아이디문제언어결과실행 시간메모리
559200SweezyPlus Minus (BOI17_plusminus)C++17
100 / 100
141 ms8980 KiB
#include <bits/stdc++.h>
 
using namespace std;
#define int long long

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, s, n) for (int i = s; i < (n); ++i)
#define pb push_back
#define sz(a) (int) (a.size())

const int mod = 1e9 + 7;

int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2) {
      (res *= a) %= mod;
    }
    (a *= a) %= mod;
    n /= 2;
  }
  return res;
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> x(k), y(k), t(k);
  rep(i, k) {
    char c;
    cin >> c >> x[i] >> y[i];
    t[i] = (c == '+');
    --x[i], --y[i];
  }

  if (k == 0){
    cout << (power(2, n) + power(2, m) - 2 + 5 * mod) % mod;
    return;
  }
  
  if (n == 1 || m == 1) {
    cout << power(2, n * m - k);
    return;
  }

  bool need = 0;
  int res = 0;
  auto add = [&] () -> void {
    map<int, int> mp;
    rep(i, k) {
      int type = (t[i] == 1 ? (x[i] & 1) : (x[i] & 1 ^ 1));
      if (mp.find(y[i]) != mp.end() && mp[y[i]] != type) {
        return;
      }
      mp[y[i]] = type;
    }
    (res += power(2, m - sz(mp))) %= mod;
    bool del = 1;
    int prv = -1, prv_type = -1;
    for (auto &[y, type] : mp) {
      if (prv != -1) {
        del &= (y - prv) % 2 == ((type == prv_type) ^ 1);
      }
      prv = y;
      prv_type = type;
    }
    need |= del;
  };
  add();
  rep(i, k) {
    int xx = x[i];
    int yy = y[i];
    y[i] = n - 1 - xx;
    x[i] = yy;
  }
  swap(n, m);
  add();
  if (need) {
    res -= 1;
    if (res < 0) {
      res += mod;
    } 
  }
  cout << res;
}
 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

plusminus.cpp: In lambda function:
plusminus.cpp:58:50: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   58 |       int type = (t[i] == 1 ? (x[i] & 1) : (x[i] & 1 ^ 1));
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...