QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#372518 | #2831. Game Theory | ckiseki# | WA | 37ms | 3588kb | C++20 | 3.8kb | 2024-03-31 14:32:06 | 2024-03-31 14:32:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
constexpr int mod = 998244353;
constexpr int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
constexpr int sub(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
constexpr int mul(int64_t a, int64_t b) {
return static_cast<int>(a * b % mod);
}
class Segtree {
struct node {
int sm[2], cnt[2];
bool flag;
};
int n;
vector<node> nodes;
static int lc(int x) { return 2 * x + 1; }
static int rc(int x) { return 2 * x + 2; }
void push(int id) {
if (not nodes[id].flag)
return;
nodes[lc(id)].flag ^= 1;
swap(nodes[lc(id)].sm[0], nodes[lc(id)].sm[1]);
swap(nodes[lc(id)].cnt[0], nodes[lc(id)].cnt[1]);
nodes[rc(id)].flag ^= 1;
swap(nodes[rc(id)].sm[0], nodes[rc(id)].sm[1]);
swap(nodes[rc(id)].cnt[0], nodes[rc(id)].cnt[1]);
}
void pull(int id) {
for (int i = 0; i < 2; ++i) {
nodes[id].sm[i] = add(nodes[lc(id)].sm[i], nodes[rc(id)].sm[i]);
nodes[id].cnt[i] = add(nodes[lc(id)].cnt[i], nodes[rc(id)].cnt[i]);
}
}
void build(int l, int r, int id, const vector<bool> &a) {
nodes[id].sm[0] = nodes[id].sm[1] = 0;
nodes[id].cnt[0] = nodes[id].cnt[1] = 0;
nodes[id].flag = false;
if (r - l == 1) {
nodes[id].sm[a[l]] += l + 1;
nodes[id].cnt[a[l]]++;
return;
}
int m = (l + r) >> 1;
build(l, m, lc(id), a);
build(m, r, rc(id), a);
pull(id);
}
void flip(int ql, int qr, int l, int r, int id) {
if (qr <= l or r <= ql)
return;
if (ql <= l and r <= qr) {
nodes[id].flag ^= 1;
swap(nodes[id].sm[0], nodes[id].sm[1]);
swap(nodes[id].cnt[0], nodes[id].cnt[1]);
return;
}
push(id);
int m = (l + r) >> 1;
flip(ql, qr, l, m, lc(id));
flip(ql, qr, m, r, rc(id));
pull(id);
}
pair<int, int> query(int ql, int qr, int l, int r, int id, bool o) {
if (qr <= l or r <= ql)
return {0, 0};
if (ql <= l and r <= qr) {
return {nodes[id].sm[o], nodes[id].cnt[o]};
}
push(id);
int m = (l + r) >> 1;
auto [lsm, lcnt] = query(ql, qr, l, m, lc(id), o);
auto [rsm, rcnt] = query(ql, qr, m, r, rc(id), o);
return {add(lsm, rsm), add(lcnt, rcnt)};
}
public:
Segtree(const vector<bool> &a) : n(int(a.size())), nodes(n * 4) {
build(0, n, 0, a);
}
void flip(int l, int r) {
flip(l, r, 0, n, 0);
}
pair<int, int> query(int l, int r, bool o) {
if (l >= r)
return {0, 0};
return query(l, r, 0, n, 0, o);
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
while (cin >> n >> q) {
string s;
cin >> s;
vector<bool> a(n);
for (int i = 0; i < n; ++i)
a[i] = s[i] - '0';
Segtree sgt(a);
while (q--) {
int l, r;
cin >> l >> r;
sgt.flip(l - 1, r);
int k = sgt.query(0, n, 1).second;
bool is1 = sgt.query(k - 1, k, 1).second;
int k1_k0 = sub(sgt.query(k + 1, n, 1).first, sgt.query(0, k, 0).first);
k1_k0 = add(k1_k0, k1_k0);
if (is1) {
k1_k0 = add(k1_k0, k);
} else {
k1_k0 = sub(k1_k0, k);
}
cout << k1_k0 << '\n';
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3476kb
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: 1ms
memory: 3548kb
input:
1 1 0 1 1
output:
1
result:
ok single line: '1'
Test #3:
score: -100
Wrong Answer
time: 37ms
memory: 3588kb
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:
0 998244350 998244352 998244350 0 1 0 1 2 4 0 998244351 0 1 2 0 998244352 998244350 1 0 998244352 998244351 1 0 0 1 998244352 998244351 1 0 998244352 998244350 998244352 998244351 1 0 1 0 0 1 0 1 0 2 2 1 0 1 2 998244352 1 0 2 4 2 998244352 1 0 0 1 2 0 0 1 0 1 0 1 1 0 998244352 998244351 2 0 0 2 0 1 ...
result:
wrong answer 2nd lines differ - expected: '3', found: '998244350'