QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104473 | #5098. 第一代图灵机 | bashkort | 0 | 487ms | 33620kb | C++20 | 5.0kb | 2023-05-10 19:45:59 | 2023-05-10 19:46:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
namespace SegmentTree {
vector<int> t;
int sz = 1;
void init(int n, vector<int> nxt) {
sz = 1 << __lg(n) + !!(n & (n - 1));
t.assign(sz << 1, 1e9);
for (int i = 0; i < n; ++i) {
t[i + sz] = nxt[i];
}
for (int i = sz - 1; i > 0; --i) {
t[i] = min(t[i << 1], t[i << 1 | 1]);
}
}
void modify(int x, int v) {
t[x += sz] = v;
while (x >>= 1) {
t[x] = min(t[x << 1], t[x << 1 | 1]);
}
}
int rangeMin(int l, int r) {
int ans = 1e9;
for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = min(ans, t[l++]);
if (r & 1) ans = min(ans, t[--r]);
}
return ans;
}
int findMax(int l, int r, int mn, int x = 1, int lx = 0, int rx = sz) { //max i: nxt[i] < mn
if (l >= rx || lx >= r || t[x] >= mn) {
return -1;
}
if (lx + 1 == rx) {
return lx;
}
int mid = lx + rx >> 1;
int ans = findMax(l, r, mn, x << 1 | 1, mid, rx);
if (ans != -1) {
ans = findMax(l, r, mn, x << 1, lx, mid);
}
return ans;
}
int descend(int mn) { //find max i such as nxt[i] < mn
if (t[1] >= mn) {
return -1;
}
int x = 1;
while (x < sz) {
if (t[x << 1 | 1] < mn) {
x = x << 1 | 1;
} else {
x = x << 1;
}
}
return x - sz;
}
}
constexpr int N = 2e5 + 228;
ll pref[N];
namespace ST {
struct Info {
ll ans = 0;
int mn = 0, l = -1, r = -1;
};
Info operator+(const Info &a, const Info &b) {
if (a.l == -1) {
return b;
} else if (b.l == -1) {
return a;
}
Info res{};
res.mn = min(a.mn, b.mn);
res.l = a.l, res.r = b.r;
int l = max(a.l - 1, SegmentTree::findMax(a.l, a.r, b.mn));
res.ans = max({a.ans, b.ans, pref[b.mn] - pref[l + 1]});
return res;
}
vector<Info> t;
int sz = 1;
void pull(int x) {
t[x] = t[x << 1] + t[x << 1 | 1];
}
void init(int n, vector<int> nxt) {
sz = 1 << __lg(n) + !!(n & (n - 1));
t.resize(sz << 1, Info());
for (int i = sz; i < sz + n; ++i) {
t[i].mn = nxt[i - sz];
t[i].l = i - sz, t[i].r = i - sz + 1;
}
for (int i = sz - 1; i > 0; --i) {
pull(i);
}
}
Info rangeQuery(int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) {
return {};
}
if (l <= lx && rx <= r) {
return t[x];
}
int mid = lx + rx >> 1;
return rangeQuery(l, r, x << 1, lx, mid) + rangeQuery(l, r, x << 1 | 1, mid, rx);
}
void modify(int x, int i) {
for (t[x += sz].mn = i; x >>= 1;) {
pull(x);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) {
cin >> pref[i];
pref[i] += pref[i - 1];
}
vector<int> c(n), nxt(n, n), last(m, -1);
vector<set<int>> st(m);
for (int i = 0; i < m; ++i) {
st[i].insert(-1);
st[i].insert(n);
}
for (int i = 0; i < n; ++i) {
cin >> c[i];
c[i] -= 1;
st[c[i]].insert(i);
if (last[c[i]] != -1) {
nxt[last[c[i]]] = i;
}
last[c[i]] = i;
}
SegmentTree::init(n, nxt);
ST::init(n, nxt);
auto modify = [&](int x, int to) {
nxt[x] = to;
SegmentTree::modify(x, to);
ST::modify(x, to);
};
auto replace = [&](int x, int col) {
if (c[x] == col) {
return;
}
st[c[x]].erase(x);
st[col].insert(x);
int p = *prev(st[c[x]].lower_bound(x));
int np = *prev(st[col].lower_bound(x));
int nx = *st[col].upper_bound(x);
int npx = *st[c[x]].upper_bound(p);
if (p != -1) {
modify(p, npx);
}
if (np != -1) {
modify(np, x);
}
modify(x, nx);
c[x] = col;
};
auto query = [&](int l, int r) { // [l, r)
int i = SegmentTree::descend(r);
if (i < l) {
return pref[r] - pref[l];
}
ll ans = pref[r] - pref[i + 1];
r = i;
ans = max(ans, ST::rangeQuery(l, r + 1).ans);
return ans;
};
while (q--) {
int tp, a, b;
cin >> tp >> a >> b;
a -= 1, b -= 1;
if (tp == 1) {
cout << query(a, b + 1) << '\n';
} else {
replace(a, b);
}
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 3776kb
input:
5000 200 5000 2315 3433 1793 4621 4627 4561 289 4399 3822 2392 392 4581 2643 2441 4572 4649 2981 3094 4206 2057 761 2516 2849 3509 3033 658 4965 3316 3269 4284 4961 753 1187 2515 1377 1725 4743 4761 3823 3464 4859 989 2401 953 875 1481 2181 103 2067 2625 3296 4721 61 3843 1607 997 4385 1284 4299 441...
output:
6280667 7895343 5939890 5088893 5088893 2984857 5676251 7895343 7895343 7895343 5088893 7895343 5088893 7895343 2260112 2208872 3308353 1895592 3814609 4073915 7895343 5088893 7895343 39374 6280667 6280667 6631732 6280667 7895343 6631732 7895343 6280667 7895343 7895343 7895343 7895343 8725968 628066...
result:
wrong answer 1st lines differ - expected: '118571', found: '6280667'
Subtask #2:
score: 0
Wrong Answer
Test #3:
score: 0
Wrong Answer
time: 487ms
memory: 30536kb
input:
200000 10 200000 55651 97298 108697 86619 60721 199951 10610 162267 154301 138848 39191 18605 101369 57073 34977 101576 71252 143401 89587 160521 166491 38442 150761 35579 25571 121311 38033 38483 144639 41401 179161 54872 157905 137601 46863 187656 171901 43715 41036 150741 69057 102031 130561 4772...
output:
12212373786 1635189805 2329865903 2232119686 5889089210 8585417845 7686056157 8021778906 2052223395 10543245680 130790896 409199461 11858265191 6038828457 12284289027 7569632236 4970250561 417471891 4290855630 11317377064 11142041365 3274342494 3201740565 3664608094 1640486979 1635189805 10421478905...
result:
wrong answer 1st lines differ - expected: '1232419', found: '12212373786'
Subtask #3:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 368ms
memory: 33620kb
input:
200000 20000 200000 30681 32496 35471 48191 159123 69792 120915 150673 187226 158493 36275 26856 107976 124777 145229 69745 183961 14497 144808 153612 185893 137681 66417 46802 19345 113322 168046 128149 191001 135433 13201 139214 59489 81178 42343 163158 110121 119201 97501 53079 158755 192241 1132...
output:
17292438468 15796669205 14403616875 15097718706 13035211063 15009798083 13972137601 19722396301 15881114346 16159237399 14811384793 3515659372 17085662741 18138955819 12625442167 6937288988 15251325799 7061084793 19102270816 16675382492 16100616416 14049668839 17780506716 19624904800 18284319313 150...
result:
wrong answer 1st lines differ - expected: '46702944', found: '17292438468'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%