QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#649935#6836. A Plus B ProblemljljljljAC ✓1078ms73288kbC++207.2kb2024-10-18 11:37:282024-10-18 11:37:28

Judging History

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

  • [2024-10-18 11:37:28]
  • 评测
  • 测评结果:AC
  • 用时:1078ms
  • 内存:73288kb
  • [2024-10-18 11:37:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
const int N = 1e6 + 7;
const int mod = 998244353;
struct Tree
{
    // 有懒惰标记的线段树,只要需要往子节点继续搜索就需要pushdown,只要发生修改就一定需要pushup
    struct node
    {
        int l, r, len;
        int val;      // val只对区间长度为1的区间起作用,用来表示单点的值
        int r0;       // 区间右端开始有几个连续的零
        int r9;       // 区间右端开始有几个连续的九
        int lz0, lz9; // lz0:区间全部置0的懒惰标记,lz9:区间全部置1的懒惰标记
    } root[N << 2];
#define ls (p << 1)
#define rs (p << 1 | 1)
    void pushup(int p)
    {
        root[p].r0 = root[rs].r0 + ((root[rs].r0 == root[rs].len) ? root[ls].r0 : 0); // 右区间若全是9,那么需要加上左区间的r9
        root[p].r9 = root[rs].r9 + ((root[rs].r9 == root[rs].len) ? root[ls].r9 : 0); // 右区间若全是0,那么需要加上左区间的r0
    }
    void change(int p)
    { // 单点的值修改后,对应的r0和r9也需要变动
        root[p].r0 = root[p].r9 = 0;
        if (root[p].val == 0)
            root[p].r0 = 1;
        else if (root[p].val == 9)
            root[p].r9 = 1;
    }
    void change0(int p)
    { // 将p区间全部置0
        root[p].lz0 = 1;
        root[p].lz9 = 0; // 要将之前的懒惰标记覆盖掉,用最新的
        root[p].val = 0;
        root[p].r0 = root[p].len;
        root[p].r9 = 0;
    }
    void change9(int p)
    { // 将p区间全部置9
        root[p].lz9 = 1;
        root[p].lz0 = 0; // 要将之前的懒惰标记覆盖掉,用最新的
        root[p].val = 9;
        root[p].r9 = root[p].len;
        root[p].r0 = 0;
    }
    void pushdown(int p)
    { // 下传懒惰标记
        if (root[p].lz0)
        {
            change0(ls);
            change0(rs); // 修改子树
            root[p].lz0 = 0;
        }
        else if (root[p].lz9)
        {
            change9(ls);
            change9(rs); // 修改子树
            root[p].lz9 = 0;
        }
    }
    void build(int p, int l, int r, string &s)
    { // 初始化
        root[p] = {l, r, r - l + 1, 0, 0, 0, 0, 0};
        if (l == r)
        {
            root[p].val = s[l] - '0';
            change(p);
            return;
        }
        int mid = (l + r) >> 1;
        build(ls, l, mid, s);
        build(rs, mid + 1, r, s);
        pushup(p);
    }
    int find(int p, int r, int w)
    { // 查找[1,r]区间内从由至左有几个连续的w
        if (!r)
            return 0;
        if (root[p].r <= r)
        { // 区间被完全覆盖,只用返回右端点有几个连续的w
            if (w == 0)
                return root[p].r0;
            return root[p].r9;
        }
        pushdown(p);
        int mid = (root[p].l + root[p].r) >> 1;
        if (r <= mid)
            return find(ls, r, w);
        else
        {
            int x = find(rs, r, w);
            if (x == r - mid)
                x += find(ls, r, w); // 右区间内全是w,那么就往左区间继续走,反之则已经结束
            return x;
        }
    }
    void change_to(int p, int l, int r, int w)
    { // 区间赋值为w
        if (root[p].l >= l && root[p].r <= r)
        {
            if (w == 0)
                change0(p);
            else
                change9(p);
            return;
        }
        pushdown(p);
        int mid = (root[p].l + root[p].r) >> 1;
        if (l <= mid)
            change_to(ls, l, r, w);
        if (r > mid)
            change_to(rs, l, r, w);
        pushup(p);
    }
    void update(int p, int pos, int val)
    { // 单点修改
        if (root[p].l == root[p].r)
        {
            root[p].val = (root[p].val + val + 10) % 10; // 产生进位或借位的值不用管,在外部会处理
            change(p);
            return;
        }
        pushdown(p);
        int mid = (root[p].l + root[p].r) >> 1;
        if (pos <= mid)
            update(ls, pos, val);
        else
            update(rs, pos, val);
        pushup(p);
    }
    int fval(int p, int pos)
    { // 单点查找
        if (root[p].l == root[p].r)
            return root[p].val;
        int mid = (root[p].l + root[p].r) >> 1;
        pushdown(p);
        if (pos <= mid)
            return fval(ls, pos);
        return fval(rs, pos);
    }
} tree;
void solve()
{
    int n, q;
    cin >> n >> q;
    string s, t;
    cin >> s >> t;
    s = "+" + s;
    t = "+" + t;
    string tmp(n + 1, '0');
    // 预处理出s+t
    dF(i, n, 1)
    {
        tmp[i] += s[i] + t[i] - '0' - '0';
        int x = tmp[i] - '0';
        tmp[i - 1] += x / 10;
        tmp[i] = (x % 10) + '0';
    }
    tree.build(1, 1, n, tmp); // 建树
    while (q--)
    {
        int r, c, d;
        cin >> r >> c >> d;
        int del; // 差值
        if (r == 1)
        {
            del = d - s[c] + '0';
            s[c] = d + '0';
        }
        else
        {
            del = d - t[c] + '0';
            t[c] = d + '0';
        }
        int x = tree.fval(1, c); // 查找当前位的值
        if (!del)
            cout << x << " 0\n"; // 无修改,直接返回
        else if (del > 0)
        {
            tree.update(1, c, del); // 先修改
            if (x + del >= 10)
            {                                   // 产生进位
                int p = tree.find(1, c - 1, 9); //[1,c-1]有p个连续的9
                int y = c - 1 - p;              // 第一个不是9的数的位置
                int ans = c - y + 1;
                if (!y)
                    ans--;
                if (y + 1 <= c - 1)
                    tree.change_to(1, y + 1, c - 1, 0); // 连续为9的位置区间赋值为0
                if (y)
                    tree.update(1, y, 1); // 单点修改第一个不是9的数
                cout << x + del - 10 << " " << ans + 1 << "\n";
            }
            else
                cout << x + del << " " << 2 << "\n";
        }
        else if (del < 0)
        {
            del = -del;
            tree.update(1, c, -del); // 先修改
            if (x - del < 0)
            {                                   // 产生借位
                int p = tree.find(1, c - 1, 0); //[1,c-1]有p个连续的0
                int y = c - 1 - p;              // 第一个不是0的数的位置
                int ans = c - y + 1;
                if (!y)
                    ans--;
                if (y + 1 <= c - 1)
                    tree.change_to(1, y + 1, c - 1, 9); // 连续为0的位置区间赋值为9
                if (y)
                    tree.update(1, y, -1); // 单点修改第一个不是0的数
                cout << x - del + 10 << " " << ans + 1 << "\n";
            }
            else
                cout << x - del << " " << 2 << "\n";
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--)
        solve();
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

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: 3812kb

input:

1 1
1
1
1 1 9

output:

0 2

result:

ok single line: '0 2'

Test #3:

score: 0
Accepted
time: 226ms
memory: 3860kb

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
3 2
1 2
4 2
7 3
0 2
5 2
6 2
0 3
4 2
4 2
3 2
5 3
6 3
3 0
8 2
9 3
9 3
1 2
1 4
7 2
5 2
5 2
4 0
0 2
...

result:

ok 1000000 lines

Test #4:

score: 0
Accepted
time: 221ms
memory: 3648kb

input:

10 1000000
8702774998
9088637390
1 3 3
2 4 7
1 4 0
1 6 7
1 1 1
1 4 0
2 3 8
1 7 7
2 4 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2
9 2
0 5
9 8
5 2
3 5
5 2
9 2
9 5
9 2
8 2
9 2
7 2
9 2
3 10
8 3
0 3
9 10
8 2
5 2
2 4
6 2
4 4
6 3
3 3
9 3
3 2
5 4
7 2
6 2
8 2
1 3
9 ...

result:

ok 1000000 lines

Test #6:

score: 0
Accepted
time: 241ms
memory: 3660kb

input:

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

output:

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

result:

ok 1000000 lines

Test #7:

score: 0
Accepted
time: 1056ms
memory: 73288kb

input:

1000000 1000000
68693738574822907668000669943297325347608140886272616051068251483556534289323531160993017440087302814083329820936792365202060610991343493080865626095241885616863256382251749215319751373247876361270911203617554820406029584474249635378527788208607403822974202545637490373196507887743784...

output:

4 2
6 2
4 2
5 2
7 97772
8 2
0 140233
6 2
5 2
1 2
1 70008
8 2
1 987
0 138405
5 2
6 138113
1 121002
2 35285
4 2
0 2
3 25467
5 2
8 2
4 5543
6 2
1 3862
8 2
3 107304
6 81700
0 0
2 2
0 0
0 15551
5 14120
3 4872
7 2
0 0
6 58933
6 2
9 7954
4 2
4 2
3 63166
0 0
8 38562
1 349
6 10624
3 2
3 2
4 2
8 65159
4 21435...

result:

ok 1000000 lines

Test #8:

score: 0
Accepted
time: 1078ms
memory: 72420kb

input:

1000000 1000000
61693798575862907668150369943297325385708140884272416052068257423550554279326571150943024493087202814853321120702792765522060610138341594081829639894344885616853227782222149213319781393275876306231911209117574815406667384452247691376587753208747407802994802745837490373194507888042646...

output:

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

result:

ok 1000000 lines

Test #9:

score: 0
Accepted
time: 950ms
memory: 72032kb

input:

1000000 1000000
68693738574822907668000669943297325347608140886272616051068251483556534289323531160993017440087302814083329820936792365202060610991343493080865626095241885616863256382251749215319751373247876361270911203617554820406029584474249635378527788208607403822974202545637490373196507887743784...

output:

3 958194
6 349300
6 259789
4 2
1 77093
6 2
9 2
8 2
9 2
5 133811
5 2
0 8254
9 0
9 0
9 8254
9 2
4 5608
4 2
3 328452
6 97720
7 8629
0 0
7 13938
9 113207
0 113207
4 2
0 0
4 2
9 2
1 2
6 2
9 2
0 2
0 0
1 2
0 2
0 2
4 2808
9 2808
0 13938
2 206652
0 206652
9 115835
0 115835
0 0
2 2
9 2
2 2
3 2
0 2
0 2
9 8629
...

result:

ok 1000000 lines

Test #10:

score: 0
Accepted
time: 857ms
memory: 71836kb

input:

1000000 1000000
68693738574822907668000669943297325347608140886272616051068251483556534289323531160993017440087302814083329820936792365202060610991343493080865626095241885616863256382251749215319751373247876361270911203617554820406029584474249635378527788208607403822974202545637490373196507887743784...

output:

0 832279
9 832279
9 0
9 0
0 754544
9 754544
1 609087
9 609087
4 930049
9 930049
0 809411
9 809411
0 749318
9 749318
0 582458
9 582458
9 0
9 0
0 868657
9 868657
9 0
9 0
2 707359
9 707359
1 869642
9 869642
0 568452
9 568452
0 735732
9 735732
2 568160
9 568160
0 551049
9 551049
1 535285
9 535285
0 8651...

result:

ok 1000000 lines