QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270282#6836. A Plus B Problem1722087564WA 245ms3852kbC++205.8kb2023-11-30 18:01:382023-11-30 18:01:39

Judging History

你现在查看的是最新测评结果

  • [2023-11-30 18:01:39]
  • 评测
  • 测评结果:WA
  • 用时:245ms
  • 内存:3852kb
  • [2023-11-30 18:01:38]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

struct Tag {
    int val = -1;
    void apply(Tag v) {
        val = v.val;
    }
};

struct Info {
    int len = 1, pre = 0;
    Info(int x = 0) {
        pre = x;
    }
    friend Info operator+ (const Info &lhs, const Info &rhs) {
        Info res;
        res.len = lhs.len + rhs.len;
        res.pre = lhs.pre;
        if (lhs.pre == lhs.len) {
            res.pre += rhs.pre;
        }
        return res;
    }
    void apply(Tag v) {
        if (v.val == 0) {
            pre = 0;
        }
        if (v.val == 1) {
            pre = len;
        }
    }
};

struct SegTr {
    int n;
    std::vector<Tag> tag;
    std::vector<Info> info;
    SegTr(int n) {
        this->n = n;
        tag.resize(n * 4);
        info.resize(n * 4);
    }

    void pushup(int p) {
        info[p] = info[p * 2] + info[p * 2 + 1];;
    }

    void pushdown(int p) {
        if (tag[p].val != -1) {
            info[p * 2].apply(tag[p]);
            info[p * 2 + 1].apply(tag[p]);
            tag[p * 2].apply(tag[p]);
            tag[p * 2 + 1].apply(tag[p]);
            tag[p].val = -1;
        }
    }

    void build(int p, int l, int r, std::vector<int> &a) {
        if (l == r) {
            info[p] = Info(a[l]);
            return;
        }
        int m = (l + r) / 2;
        build(p * 2, l, m, a);
        build(p * 2 + 1, m + 1, r, a);
        pushup(p);
    };

    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= x && r <= y) {
            return info[p];
        }
        pushdown(p);
        int m = (l + r) / 2;
        if (y <= m) {
            return rangeQuery(p * 2, l, m, x, y);
        }
        if (x > m) {
            return rangeQuery(p * 2 + 1, m + 1, r, x, y);
        }
        return rangeQuery(p * 2, l, m, x, y) + rangeQuery(p * 2 + 1, m + 1, r, x, y);
    }

    void rangeModify(int p, int l, int r, int x, int y, Tag v) {
        if (l >= x && r <= y) {
            info[p].apply(v);
            tag[p].apply(v);
            return;
        }
        int m = (l + r) / 2;
        pushdown(p);
        if (x <= m) {
            rangeModify(p * 2, l, m, x, y, v);
        }
        if (y > m) {
            rangeModify(p * 2 + 1, m + 1, r, x, y, v);
        }
        pushup(p);
    };
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, q;
    std::string a[2];
    std::cin >> n >> q >> a[0] >> a[1];

    std::reverse(a[0].begin(), a[0].end());
    std::reverse(a[1].begin(), a[1].end());
    a[0] = ' ' + a[0];
    a[1] = ' ' + a[1];

    std::vector<int> ans(n + 1);
    std::vector<int> is0(n + 1), is9(n + 1);

    for (int i = 1, flg = 0; i <= n; i++) {
        int c1 = a[0][i] - '0';
        int c2 = a[1][i] - '0';
        int now = c1 + c2 + flg;
        if (now >= 10) {
            now -= 10;
            flg = 1;
        } else {
            flg = 0;
        }
        ans[i] = now;
        if (ans[i] == 0) {
            is0[i] = 1;
        }
        if (ans[i] == 9) {
            is9[i] = 1;
        }
    }

    SegTr sgt0(n + 1), sgt9(n + 1);
    sgt0.build(1, 1, n, is0);
    sgt9.build(1, 1, n, is9);

    auto check = [&](int i) {
        assert(ans[i] >= 0 && ans[i] < 10);
    };

    auto get = [&](int i) -> int {
        if ((sgt0.rangeQuery(1, 1, n, i, i).pre) == 1) {
            ans[i] = 0;
        }
        if ((sgt9.rangeQuery(1, 1, n, i, i).pre) == 1) {
            ans[i] = 9;
        }
        return ans[i];
    };

    while (q--) {
        int r, c, d;
        std::cin >> r >> c >> d;

        // r = 1;
        // c = std::rand() % n + 1;
        // d = std::rand() % 10;

        c = n - c + 1;
        r -= 1;
        int now = get(c);
        int inc = d - (a[r][c] - '0');
        if (inc == 0) {
            std::cout << now << " " << 0 << "\n";
            continue;
        }
        int ans2 = 2;
        a[r][c] = '0' + d;
        if (now + inc >= 10 && c < n) {
            auto res = sgt9.rangeQuery(1, 1, n, c + 1, n);
            int len = res.pre;
            ans2 += len;
            if (len > 0) {
                sgt9.rangeModify(1, 1, n, c + 1, c + len, {0});
                sgt0.rangeModify(1, 1, n, c + 1, c + len, {1});
            }
            if (c + len + 1 <= n) {
                ans[c + len + 1] = get(c + len + 1) + 1;
                if (ans[c + len + 1] == 9) {
                    sgt9.rangeModify(1, 1, n, c + len + 1, c + len + 1, {1});
                }
                check(c + len + 1);
                ans2 += 1;
            }
        } else if (now + inc < 0 && c < n) {
            auto res = sgt0.rangeQuery(1, 1, n, c + 1, n);
            int len = res.pre;
            ans2 += len;
            if (len > 0) {
                sgt9.rangeModify(1, 1, n, c + 1, c + len, {1});
                sgt0.rangeModify(1, 1, n, c + 1, c + len, {0});
            }
            if (c + len + 1 <= n) {
                ans[c + len + 1] = get(c + len + 1) - 1;
                if (ans[c + len + 1] == 0) {
                    sgt0.rangeModify(1, 1, n, c + len + 1, c + len + 1, {1});
                }
                // check(c + len + 1);
                ans2 += 1;
            }
        }
        ans[c] = ((now + inc) % 10 + 10) % 10;
        // check(c);
        if (ans[c] == 0) {
            sgt0.rangeModify(1, 1, n, c, c, {1});
        }
        if (ans[c] != 0) {
            sgt0.rangeModify(1, 1, n, c, c, {0});
        }
        if (ans[c] == 9) {
            sgt9.rangeModify(1, 1, n, c, c, {1});
        }
        if (ans[c] != 9) {
            sgt9.rangeModify(1, 1, n, c, c, {0});
        }
        std::cout << ans[c] << " " << ans2 << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3852kb

input:

5 5
01234
56789
2 1 0
2 2 1
2 3 2
2 4 3
2 5 4

output:

0 2
3 2
5 3
7 3
8 3

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

1 1
1
1
1 1 9

output:

0 2

result:

ok single line: '0 2'

Test #3:

score: -100
Wrong Answer
time: 245ms
memory: 3660kb

input:

10 1000000
6869373857
3130626142
1 9 2
1 10 0
2 7 6
1 1 0
1 7 6
2 10 4
2 3 9
2 4 2
2 4 4
2 7 0
1 2 4
1 9 8
1 3 7
1 7 1
1 1 5
2 1 6
1 3 5
2 5 8
2 6 5
1 6 3
1 3 8
2 4 2
2 6 3
2 2 6
1 10 9
2 1 1
2 5 4
1 1 8
2 4 0
1 9 1
1 1 8
2 4 2
2 9 2
1 10 3
1 8 9
1 4 6
2 3 0
1 1 6
1 7 1
1 10 9
2 4 4
2 5 9
2 1 8
1 9 ...

output:

6 2
2 2
9 0
3 2
2 8
4 2
6 2
2 2
4 2
6 5
6 3
2 4
7 2
2 2
8 2
1 2
5 2
1 3
2 3
8 3
8 2
2 2
6 2
1 3
3 3
7 2
7 3
0 2
9 3
6 4
0 0
1 3
4 2
7 3
0 3
8 3
8 3
8 2
2 0
3 3
0 3
2 3
5 2
9 2
4 2
8 2
3 3
5 3
3 2
5 0
4 2
2 2
1 2
3 2
7 4
9 3
4 2
5 3
0 4
4 2
3 2
3 2
5 3
6 4
3 0
8 2
9 3
9 3
0 2
1 4
7 2
5 2
5 2
3 0
0 2
...

result:

wrong answer 52nd lines differ - expected: '3 2', found: '2 2'