QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592156 | #2831. Game Theory | lonelywolf# | WA | 35ms | 3828kb | C++20 | 4.8kb | 2024-09-26 20:59:06 | 2024-09-26 20:59:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector(n_ + 1, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size() - 1;
info.assign(4 << __lg(n) + 1, Info());
tag.assign(4 << __lg(n) + 1, Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (l == r) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m + 1, r);
pull(p);
};
build(1, 1, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (l == r) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x <= m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 1, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l > y || r < x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m + 1, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 1, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l > y || r < x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m + 1, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 1, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F pred) {
if (l > y || r < x || !pred(info[p])) {
return -1;
}
if (l == r) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m + 1, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F pred) {
return findFirst(1, 1, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F pred) {
if (l > y || r < x || !pred(info[p])) {
return -1;
}
if (l == r) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m + 1, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F pred) {
return findLast(1, 1, n, l, r, pred);
}
};
struct Tag {
int rev = false;
void apply(Tag t) {
rev ^= t.rev;
}
};
struct Info {
array<int, 2> cnt{0, 0};
array<int, 2> sum{0, 0};
void apply(Tag t) {
if (t.rev) {
swap(cnt[0], cnt[1]);
swap(sum[0], sum[1]);
}
}
};
Info operator+(Info a, Info b) {
Info c;
for (int i = 0; i < 2; i++) {
c.cnt[i] = a.cnt[i] + b.cnt[i];
c.sum[i] = a.sum[i] + b.sum[i];
}
return c;
}
constexpr bool DEBUG = false;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
while (cin >> n >> q) {
LazySegmentTree<Info, Tag> tr(n);
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
if (c == '1') {
tr.modify(i, {{0, 1}, {0, i}});
} else {
tr.modify(i, {{1, 0}, {i, 0}});
}
}
while (q--) {
int l, r;
cin >> l >> r;
tr.rangeApply(l, r, {1});
auto v = tr.rangeQuery(1, n);
int a = v.sum[1], b = v.cnt[1];
cout << a - b * (b - 1) / 2 << "\n";
}
if (DEBUG) {
return 0;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3496kb
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: 3828kb
input:
1 1 0 1 1
output:
1
result:
ok single line: '1'
Test #3:
score: -100
Wrong Answer
time: 35ms
memory: 3556kb
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 2 1 2 0 1 0 1 2 0 0 2 0 1 2 0 1 2 1 0 1 2 1 0 0 1 2 2 1 0 1 2 2 2 1 0 1 0 0 1 0 1 0 2 2 1 0 1 2 1 1 0 2 0 2 2 1 0 0 1 2 0 0 1 0 1 0 1 1 0 1 2 2 0 0 2 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 2 0 2 0 1 0 0 1 1 0 1 0 1 2 0 2 1 0 0 2 1 2 0 1 2 2 1 0 0 1 2 0 2 0 0 1 0 1 1 0 1 0 1 0 1 0 1 2 1 0 2 1 0 2 0 1 0 1 ...
result:
wrong answer 2nd lines differ - expected: '3', found: '2'