結果

問題 No.1245 ANDORゲーム(calc)
ユーザー KudeKude
提出日時 2020-10-03 00:15:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 1,793 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 181,920 KB
実行使用メモリ 43,128 KB
最終ジャッジ日時 2023-09-25 00:41:20
合計ジャッジ時間 10,643 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 344 ms
28,580 KB
testcase_12 AC 336 ms
28,540 KB
testcase_13 AC 356 ms
29,192 KB
testcase_14 AC 356 ms
28,312 KB
testcase_15 AC 352 ms
29,184 KB
testcase_16 AC 357 ms
28,644 KB
testcase_17 AC 462 ms
17,596 KB
testcase_18 AC 456 ms
16,952 KB
testcase_19 AC 452 ms
19,340 KB
testcase_20 AC 467 ms
21,296 KB
testcase_21 AC 454 ms
17,784 KB
testcase_22 AC 459 ms
19,652 KB
testcase_23 AC 303 ms
19,608 KB
testcase_24 AC 285 ms
16,628 KB
testcase_25 AC 192 ms
4,904 KB
testcase_26 AC 228 ms
4,904 KB
testcase_27 AC 317 ms
43,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

signed main() {
    int n, q;
    cin >> n >> q;
    VI a(n);
    rep(i, n) cin >> a[i];
    string st;
    cin >> st;
    vector<vector<P>> d(30);
    ll s = 0;
    vector<ll> dif(n);
    rep(i, n) {
        int t = 0;
        rep(k, 30) {
            if (st[i] == '0' && (a[i] >> k & 1) == 0) {
                if (d[k].empty()) {
                    t -= 1 << k;
                } else if (d[k].back().second == 1) {
                    t -= 1 << k;
                }
                d[k].emplace_back(i, 0);
            } else if (st[i] == '1' && (a[i] >> k & 1) == 1) {
                if (d[k].empty()) {
                    t += 1 << k;
                } else if (d[k].back().second == 0) {
                    t += 1 << k;
                }
                d[k].emplace_back(i, 1);
            }
        }
        s += abs(t);
        dif[i] = t;
    }
    rep(_, q) {
        int ti;
        cin >> ti;
        ll ans = s;
        map<int, int> m;
        rep(k, 30) {
            if (d[k].empty()) continue;
            int i = d[k][0].first, c = d[k][0].second;
            if (c == (ti >> k & 1)) {
                if (m.find(i) == m.end()) {
                    m[i] = dif[i];
                }
                if (c == 1) m[i] -= 1 << k;
                else m[i] += 1 << k;
            }
        }
        for(auto p: m) {
            int i, t;
            tie(i, t) = p;
            ans += -abs(dif[i]) + abs(t);
        }
        cout << ans << endl;
    }
}
0