QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103866#2831. Game Theoryyl_9#RE 0ms3332kbC++145.5kb2023-05-07 18:50:042023-05-07 18:50:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-07 18:50:08]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3332kb
  • [2023-05-07 18:50:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

template <class S, S (*op)(const S&, const S&), class F, void (*mapping)(F, S&),
          void (*composition)(F, F&)>
class SGT {
#define lc t << 1
#define rc t << 1 | 1
#define lll lc, l, mid
#define rrr rc, mid + 1, r
#define mmm int mid = (l + r) >> 1

   private:
    vector<S> node;
    S e;
    vector<F> tag;
    F id;
    int L, R;
    template <class READ>
    void build(READ read, int t, int l, int r) {
        tag[t] = id;
        // node[t] = e;
        if (l == r) return read(node[t], l, r), void(0);
        mmm;
        build(read, lll), build(read, rrr);
        node[t] = op(node[lc], node[rc]);
    }
    void push(int t, F x) { mapping(x, node[t]), composition(x, tag[t]); }
    void push_down(int t) { push(lc, tag[t]), push(rc, tag[t]), tag[t] = id; }
    S query(int ql, int qr, int t, int l, int r) {
        if (ql <= l && r <= qr) return node[t];
        push_down(t);
        mmm;
        if (ql <= mid && mid < qr)
            return op(query(ql, qr, lll), query(ql, qr, rrr));
        return ql <= mid ? query(ql, qr, lll) : query(ql, qr, rrr);
    }
    void modify(int ql, int qr, F x, int t, int l, int r) {
        if (ql <= l && r <= qr) return push(t, x);
        push_down(t);
        mmm;
        if (ql <= mid) modify(ql, qr, x, lll);
        if (mid < qr) modify(ql, qr, x, rrr);
        node[t] = op(node[lc], node[rc]);
    }
    template <class JUDGE>
    int minLeft(int ql, JUDGE judge, S& s, int t, int l, int r) {
        if (r < ql) return -1;
        if (ql <= l) {
            S ss = op(s, node[t]);
            if (!judge(ss)) return s = ss, -1;
            if (l == r) return l;
        }
        push_down(t);
        mmm;
        int pos = minLeft(ql, judge, s, lll);
        if (~pos) return pos;
        return minLeft(ql, judge, s, rrr);
    }
    template <class JUDGE>
    int maxRight(int qr, JUDGE judge, S& s, int t, int l, int r) {
        if (qr < l) return -1;
        if (r <= qr) {
            S ss = op(node[t], s);
            if (!judge(ss)) return s = ss, -1;
            if (l == r) return l;
        }
        push_down(t);
        mmm;
        int pos = maxRight(qr, judge, s, rrr);
        if (~pos) return pos;
        return maxRight(qr, judge, s, lll);
    }

   public:
    /**
     * read(idx) :  读取idx的值,返回区间信息
     * l,r :        范围左右区间
     * e :          空区间信息
     * id :         空标记
     */
    SGT(S e, F id) : e(e), id(id) {}
    template <class READ>
    void build(READ read, int l, int r) {
        node = vector<S>(4 * (r - l + 1), e);
        tag = vector<F>(4 * (r - l + 1), id);
        build(read, 1, L = l, R = r);
    }
    S query(int l, int r) {
        assert(L <= l && r <= R);
        assert(l <= r);
        return query(l, r, 1, L, R);
    }
    void modify(int l, int r, F x) {
        assert(L <= l && r <= R);
        assert(l <= r);
        modify(l, r, x, 1, L, R);
    }
    template <class JUDGE>  // judge(node) -> bool : 判断node是否是合法区间
    int minLeft(int ql, JUDGE judge) {  // 找出最短合法区间[ql...res]
        S s = e;
        return minLeft(ql, judge, s, 1, L, R);  // 未找到返回-1,否则返回res
    }
    template <class JUDGE>  // judge(node) -> bool : 判断node是否是合法区间
    int maxRight(int qr, JUDGE judge) {  // 找出最短合法区间[res...qr]
        S s = e;
        return maxRight(qr, judge, s, 1, L, R);  // 未找到返回-1,否则返回res
    }
};

/**
 * S :              区间信息
 * op :             区间信息合并
 * F :              懒标记类型
 * mapping :        将映射x作用于区间s
 * composition :    对懒标记复合
 */
// typedef ? S;
// typedef ? F;
// S e{?};
// F id{?};
// inline S op(const S &a, const S &b) { return {?}; }
// 注意判断标记是否为空
// inline void mapping(F f, S &s) { }
// inline void composition(F f, F &g) { }
// SGT<S, op, F, mapping, composition> sgt(e,id);

#define rep(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define per(i, b, a) for (int i = a, i##end = b; i >= i##end; --i)
#define endl '\n'
const int MOD = 998244353;
int add(int a, int b) { return (a + b) % MOD; }
struct S {
    int n;
    int cnt1;
    int sum[2];
} e{};
inline S op(const S& a, const S& b) {
    return {a.n + b.n,
            a.cnt1 + b.cnt1,
            {add(a.sum[0], b.sum[0]), add(a.sum[1], b.sum[1])}};
    ;
}
typedef int F;
F id = 0;
inline void mapping(F f, S& s) {
    if (f) {
        s.cnt1 = s.n - s.cnt1;
        swap(s.sum[0], s.sum[1]);
    }
}
inline void composition(F f, F& g) { g ^= f; }
SGT<S, op, F, mapping, composition> sgt(e, id);
int n, q;
void solve() {
    string s;
    cin >> s;
    sgt.build(
        [&](S& node, int l, int r) {
            node = {1, s[l - 1] == '1', {0, 0}};
            node.sum[s[l - 1] - '0'] = l;
        },
        1, n);
    while (q--) {
        int l, r;
        cin >> l >> r;
        sgt.modify(l, r, 1);
        int k = sgt.query(1, n).cnt1;
        int flag = sgt.query(k, k).cnt1;
        long long ans =
            sgt.query(k, n).sum[1] * 2ll - sgt.query(1, k).sum[0] * 2ll;
        if (flag)
            ans -= sgt.query(k, n).sum[1];
        else
            ans += sgt.query(1, k).sum[0];
        cout << (ans % MOD + MOD) % MOD << "\n";
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while (cin >> n >> q) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
010
1 2
2 3
5 1
00000
1 5

output:

1
3
5

result:

ok 3 lines

Test #2:

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

input:

1 1
0
1 1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Dangerous Syscalls

input:

2 2
01
2 2
2 2
2 2
01
1 2
1 2
1 2
1
1 1
1 1
1 2
1
1 1
1 1
2 2
00
1 2
1 2
2 2
11
1 2
1 2
2 2
01
2 2
1 1
2 2
10
2 2
1 2
2 2
01
1 2
1 2
1 2
0
1 1
1 1
2 2
01
1 2
2 2
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 2
1 1
1 2
0
1 1
1 1
2 2
01
1 2
1 2
2 2
10
1 2
1 1
1 2
0
1 1
1 1
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 ...

output:


result: