QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#61825 | #2560. Streetlights | alpha1022 | TL | 4ms | 9984kb | C++17 | 8.1kb | 2022-11-15 08:03:16 | 2022-11-15 08:03:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5;
const int Q = 2.5e5;
int n, q;
int a[N + 5], las[N + 5];
pair<int, int> upd[N + Q + 5];
int pre[N + Q + 5];
struct SegmentTree {
#define ls (u << 1)
#define rs (ls | 1)
int seg[N * 4 + 5];
void insert(int x, int k, int u, int tl, int tr) {
if (tl == tr) { seg[u] = k; return ; }
int mid = (tl + tr) >> 1;
if (x <= mid) insert(x, k, ls, tl, mid);
else insert(x, k, rs, mid + 1, tr);
seg[u] = max(seg[ls], seg[rs]);
}
void insert(int x, int k) { insert(x, k, 1, 1, n); }
pair<int, int> queryL(int x, int k, int u, int tl, int tr) {
if (seg[u] < k) return {0, inf};
if (tl == tr) return {tl, seg[u]};
int mid = (tl + tr) >> 1;
if (x <= mid) return queryL(x, k, ls, tl, mid);
auto ret = queryL(x, k, rs, mid + 1, tr);
if (!ret.first) ret = queryL(x, k, ls, tl, mid);
return ret;
}
int queryL(int x, int k) {
if (!x) return 0;
auto res = queryL(x, k, 1, 1, n);
return res.second == k ? res.first : 0;
}
pair<int, int> queryR(int x, int k, int u, int tl, int tr) {
if (seg[u] < k) return {n + 1, inf};
if (tl == tr) return {tl, seg[u]};
int mid = (tl + tr) >> 1;
if (x > mid) return queryR(x, k, rs, mid + 1, tr);
auto ret = queryR(x, k, ls, tl, mid);
if (ret.first > n) ret = queryR(x, k, rs, mid + 1, tr);
return ret;
}
int queryR(int x, int k) {
if (x > n) return n + 1;
auto res = queryR(x, k, 1, 1, n);
return res.second == k ? res.first : n + 1;
}
#undef ls
#undef rs
} seg;
struct node {
int l, r, h, w;
node(int l, int r, int h, int w): l(l), r(r), h(h), w(w) {}
bool operator<(const node &o) const { return l < o.l; }
bool operator>(const node &o) const { return l > o.l; }
bool operator==(const node &o) const { return l == o.l; }
};
bool FLAG;
pair<vector<node>, int> build(set<int> newStatic, vector<pair<int, int>> upd, vector<node> tr) {
if (FLAG) {
printf("newStatic = { ");
for (int i: newStatic) printf("%d ", i);
puts("}");
}
if (FLAG) {
puts("BEGIN");
for (int i = 0; i < tr.size(); ++i) printf("%d %d %d %d\n", tr[i].l, tr[i].r, tr[i].h, tr[i].w);
puts("END");
}
for (auto [i, h]: upd)
if (FLAG)
printf("%d %d\n", i, h);
static int anc[N + 5];
int top = 0;
vector<node> newTr;
int j = 0;
for (auto [i, _]: upd) seg.insert(i, 0);
for (int i: newStatic) {
seg.insert(i, a[i]);
for (; j < tr.size() && tr[j].l <= i; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top) newTr.push_back(tr[anc[top]]);
anc[++top] = j;
}
for (; top && tr[anc[top]].r < i; --top) newTr.push_back(tr[anc[top]]);
//if (FLAG) printf("%d\n", top);
for (; top && tr[anc[top]].h <= a[i]; --top) tr[anc[top]].w = -1;
//if (FLAG) printf("%d\n", top);
}
for (; top; --top) newTr.push_back(tr[anc[top]]);
for (; j < tr.size(); ++j) newTr.push_back(tr[j]);
for (int i: newStatic) {
int l = seg.queryL(i - 1, a[i]);
/*if (newStatic.size() == 1 && newStatic.count(4) && upd.size() == 1)
printf("%d %d %d\n", i, a[i], l);*/
if (l && !newStatic.count(l)) newTr.emplace_back(l, i, a[i], 1);
int r = seg.queryR(i + 1, a[i]);
/*if (newStatic.size() == 1 && newStatic.count(4) && upd.size() == 1)
printf("%d %d %d\n", i, a[i], r);*/
if (r <= n) newTr.emplace_back(i, r, a[i], 1);
}
// for (auto [i, _]: upd) newTr.emplace_back(i, i, 0, 0);
tr = newTr, sort(tr.begin(), tr.end());
if (FLAG) {
puts("BEGIN");
for (int i = 0; i < tr.size(); ++i) printf("%d %d %d %d\n", tr[i].l, tr[i].r, tr[i].h, tr[i].w);
puts("END");
}
vector<int> sum(tr.size());
int cnt = 0;
for (int i = 0; i < tr.size(); ++i) {
auto [l, r, h, w] = tr[i];
for (; top && tr[anc[top]].r <= r; --top);
if (top) sum[i] = sum[anc[top]];
sum[i] += w, cnt += w;
anc[++top] = i;
}
//if (FLAG) printf("CNT %d\n", cnt);
vector<int> crit(1, 0);
sort(upd.begin(), upd.end());
j = top = 0;
for (auto [i, h]: upd) {
if (FLAG)
printf("%d %d\n", i, h);
for (; j < tr.size() && tr[j].l <= i; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top);
anc[++top] = j;
}
for (; top && tr[anc[top]].r < i; --top);
crit.push_back(anc[top]);
if (FLAG)
printf("%d %d, %d %d %d %d\n", i, h, tr[anc[top]].l, tr[anc[top]].r, tr[anc[top]].h, tr[anc[top]].w);
int l = 1, r = top, mid, res = 1;
while (l <= r) {
mid = (l + r) >> 1;
if (tr[anc[mid]].h > h) l = mid + 1, res = mid;
else r = mid - 1;
}
crit.push_back(anc[res]);
}
sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
//for (int i: crit) printf("%d %d %d %d\n", tr[i].l, tr[i].r, tr[i].h, tr[i].w);
//puts("FUCK");
j = top = 0;
for (int i = 0, siz = crit.size(); i + 1 < siz; ++i) {
for (; j <= crit[i]; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top);
anc[++top] = j;
}
int l = 1, r = top, mid, res = 1;
while (l <= r) {
mid = (l + r) >> 1;
if (tr[anc[mid]].r > tr[crit[i + 1]].r) l = mid + 1, res = mid;
else r = mid - 1;
}
crit.push_back(anc[res]);
}
sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
vector<node>().swap(newTr), top = 0;
for (int i = 0; i < crit.size(); ++i) {
//if (newStatic.empty()) printf("%d, %d\n", crit[i], tr.size());
auto [l, r, h, w] = tr[crit[i]];
for (; top && tr[anc[top]].r <= r; --top);
w = sum[crit[i]];
if (top) w -= sum[anc[top]];
cnt -= w;
anc[++top] = crit[i];
newTr.emplace_back(l, r, h, w);
}
/*if (FLAG) {
puts("BEGIN");
for (int i = 0; i < newTr.size(); ++i) printf("%d %d %d %d\n", newTr[i].l, newTr[i].r, newTr[i].h, newTr[i].w);
puts("END");
}*/
//if (FLAG) printf("CNT %d\n", cnt);
//if (newStatic.empty()) puts("fuck here");
FLAG = 0;
return {newTr, cnt};
}
int ans[N + Q + 5];
void solve(int l, int r, vector<node> tr) {
//printf("D&C [%d, %d]\n", l, r);
//for (auto i: tr) printf("%d %d %d %d\n", i.l, i.r, i.h, i.w);
if (l == r) {
//if (l == 3) printf("%d\n", ans[l]);
a[upd[l].first] = upd[l].second,
ans[l] += build({upd[l].first}, {}, tr).second;
return ;
}
int mid = (l + r) >> 1;
set<int> s, tmp;
vector<pair<int, int>> updTmp;
for (int i = l; i <= r; ++i) s.insert(upd[i].first);
tmp = s;
for (int i = l; i <= mid; ++i) tmp.erase(upd[i].first);
updTmp = vector<pair<int, int>>(upd + l, upd + mid + 1);
for (int i = l; i <= mid; ++i) if (pre[i] && pre[i] < l && !tmp.count(upd[pre[i]].first)) updTmp.push_back(upd[pre[i]]);
//if (l == 7 && mid == 11) FLAG = 1;
auto [L, lTag] = build(tmp, updTmp, tr);
//printf("{ [%d, %d] } [%d, %d], with tag %d\n", l, mid, mid + 1, r, lTag);
for (int i = l; i <= mid; ++i) ans[i] += lTag;
solve(l, mid, L);
//for (int i: tmp) seg.insert(i, 0);
tmp = s;
for (int i = mid + 1; i <= r; ++i) tmp.erase(upd[i].first);
updTmp = vector<pair<int, int>>(upd + mid + 1, upd + r + 1);
for (int i = mid + 1; i <= r; ++i) if (pre[i] && pre[i] <= mid && !tmp.count(upd[pre[i]].first)) updTmp.push_back(upd[pre[i]]);
//printf("ready to build [%d, %d]\n", mid + 1, r);
//if (mid + 1 == 6 && r == 6) FLAG = 1;
auto [R, rTag] = build(tmp, updTmp, tr);
//printf("[%d, %d] { [%d, %d] }, with tag %d\n", l, mid, mid + 1, r, rTag);
for (int i = mid + 1; i <= r; ++i) ans[i] += rTag;
solve(mid + 1, r, R);
//for (int i: tmp) seg.insert(i, 0);
}
int main() {
scanf("%d%d", &n, &q);
a[0] = a[n + 1] = inf;
for (int i = 1; i <= n; ++i) upd[i].first = i, scanf("%d", &upd[i].second), las[i] = i;
for (int i = n + 1; i <= n + q; ++i)
scanf("%d%d", &upd[i].first, &upd[i].second),
pre[i] = las[upd[i].first], las[upd[i].first] = i;
solve(1, n + q, {node(0, n + 1, inf, 0)});
for (int i = n; i <= n + q; ++i) printf("%d\n", ans[i]);
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7744kb
input:
6 2 4 2 2 2 4 6 4 6 6 4
output:
3 2 2
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 9808kb
input:
50 100 310081863 722273055 654741011 310081863 654741011 722273055 654741011 722273055 654741011 654741011 654741011 310081863 310081863 722273055 654741011 654741011 654741011 722273055 310081863 654741011 310081863 310081863 310081863 722273055 310081863 654741011 654741011 310081863 722273055 722...
output:
28 28 28 29 30 31 31 31 31 31 31 31 31 32 33 34 34 33 33 33 33 32 32 31 31 31 32 32 31 31 31 31 30 30 30 31 31 31 31 31 31 30 30 29 30 31 32 32 32 32 32 31 32 33 33 33 33 32 32 31 32 33 31 31 32 31 32 31 31 31 30 31 30 29 29 28 28 29 28 28 27 27 27 27 27 27 26 27 28 27 28 29 28 28 28 28 29 29 28 29 28
result:
ok 101 lines
Test #3:
score: 0
Accepted
time: 4ms
memory: 9984kb
input:
50 100 93308794 275481889 130830018 675774101 130830018 93308794 275481889 999873895 275481889 104418887 130830018 275481889 675774101 999873895 130830018 841188804 360486542 104418887 140762403 275481889 275481889 770511267 104418887 140762403 93308794 675774101 104418887 770511267 130830018 933087...
output:
12 12 11 11 11 11 11 11 10 10 10 10 10 10 10 11 11 11 11 12 12 12 12 12 11 10 11 11 11 11 11 12 13 12 12 13 13 14 12 11 11 10 10 10 10 10 9 9 9 9 9 9 8 9 10 10 9 10 9 9 9 10 10 11 12 12 13 13 13 13 14 14 13 13 13 12 12 12 12 13 12 12 12 12 12 12 13 13 13 13 15 15 15 17 18 18 17 17 16 16 15
result:
ok 101 lines
Test #4:
score: 0
Accepted
time: 3ms
memory: 7680kb
input:
50 100 195248019 905127308 129122336 764519854 338556860 795943323 554412442 338556860 217191782 140699690 654772489 386182517 217191782 37485244 795943323 924638428 795943323 820028162 855279832 795943323 129122336 554412442 195248019 764519854 810525122 554412442 201706134 661330059 129122336 2090...
output:
5 5 6 5 5 5 4 4 3 3 3 3 3 2 4 3 3 3 4 5 5 5 5 5 5 5 5 4 4 4 5 6 6 5 5 4 4 4 3 3 3 3 3 3 4 4 4 4 3 3 4 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 6 5 5 5 5 5 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 7
result:
ok 101 lines
Test #5:
score: 0
Accepted
time: 4ms
memory: 5776kb
input:
50 100 772094573 19576803 263817454 873867094 557813690 952336439 500513802 392057352 305209480 199018938 206776586 514630037 466387810 403552086 50423285 658534934 19576803 404488754 179660945 591777562 262850065 817419372 680762089 591777562 424021147 403552086 718896141 456431927 680762089 595426...
output:
1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 2 2 2 3 3 3 3 3 3 2 2 2 2 2 2 2 1 1 1 0 0 0 0 0 0 0 0 0 1 2 2 3 3 3 3 3 3 2 2 2 3 3 3 3 3 3 3
result:
ok 101 lines
Test #6:
score: 0
Accepted
time: 0ms
memory: 9812kb
input:
50 100 5096114 61078240 254964021 318250156 571031769 256037951 208426954 833646260 732869624 746606948 226729785 151221431 611264696 351005299 205027954 706057630 453231547 874058912 462474957 366832522 823051853 289489922 109072951 103985450 269915659 377686154 809672410 12123621 732787174 9017273...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 101 lines
Test #7:
score: 0
Accepted
time: 0ms
memory: 9868kb
input:
50 100 976187983 976187983 879080743 976187983 827737130 827737130 827737130 827737130 815905933 811453113 789018592 789018592 681089922 675640665 659464656 635119734 635119734 633485638 633485638 567930339 552957008 484438465 484438465 484438465 387753272 377659696 376161946 976187983 367642977 376...
output:
16 15 14 14 15 16 17 17 18 18 31 30 29 28 27 26 25 24 23 22 21 20 20 19 20 19 18 18 18 18 18 17 17 16 16 16 16 15 15 15 15 14 13 12 12 12 12 11 11 10 9 9 9 8 8 7 7 6 7 6 7 7 7 8 8 7 8 10 10 10 10 10 11 10 10 12 14 15 15 16 16 15 16 18 19 20 26 26 27 28 29 28 28 27 26 25 24 23 23 22 21
result:
ok 101 lines
Test #8:
score: 0
Accepted
time: 3ms
memory: 5892kb
input:
50 100 843864537 245114944 227661173 137675097 918583745 80278395 44678681 37169219 37007425 27167524 4382795 4043558 3655016 3624538 2994987 1979195 1407769 819862 771067 665903 137891 137891 665903 771067 819862 1407769 1979195 2994987 3624538 3655016 4043558 4382795 27167524 37007425 37142735 305...
output:
17 17 16 15 7 7 8 5 5 5 5 5 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 0 1 1 2 3 3 4 4 4 4 5 6 6 7 8 9 10 11 12 13 14 14 14 14 14 15 16 16 16 16 17 18 19 19 20 21 20 19 18 15 14 14 14 13 7 6 6 6 6 6 6 6 6 6 6 5 5 4 4 4 3 3 3 3 3 3 2 2 2 2
result:
ok 101 lines
Test #9:
score: 0
Accepted
time: 4ms
memory: 9968kb
input:
50 100 920202355 768392166 755066475 630812635 617367313 601334965 450742259 367726734 265094786 151773018 77676966 53524889 53524889 77676966 151773018 265094786 205222950 154745305 57476426 57476426 154745305 294856628 367726734 450742259 601334965 617367313 630812635 481253037 481253037 755066475...
output:
19 20 20 19 18 17 16 15 14 13 12 11 10 10 10 10 9 8 7 7 7 8 7 7 7 6 6 5 5 5 5 4 4 3 3 4 3 3 3 3 3 3 3 3 2 2 3 3 3 3 3 4 4 5 5 5 6 6 6 6 8 10 10 11 12 13 13 14 14 15 15 15 20 21 22 21 19 17 17 17 15 14 13 13 13 13 13 12 12 11 10 8 8 8 8 7 7 7 7 7 7
result:
ok 101 lines
Test #10:
score: -100
Time Limit Exceeded
input:
100000 250000 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1...