QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#636038 | #8286. Stacks | DBsoleil | TL | 20ms | 13684kb | C++20 | 5.6kb | 2024-10-12 21:56:52 | 2024-10-12 21:56:52 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
static constexpr int Maxn = 1e5 + 5, SqrN = 505;
int n, qn, bs, bn, bel[Maxn], bL[Maxn], bR[Maxn];
struct data_t {
int64_t length, w;
data_t() = default;
data_t(int64_t length, int64_t w) : length(length), w(w) { }
int64_t calc(int64_t len) const { return len * w; }
friend bool operator < (const data_t &lhs, const data_t &rhs)
{ return pair(lhs.length, lhs.w) < pair(rhs.length, rhs.w); }
};
struct tag_t {
vector<data_t> a, s;
tag_t() = default;
void insert(int64_t x, int64_t w) {
int64_t px = a.empty() ? 0 : s.back().length;
int64_t pw = a.empty() ? 0 : s.back().w;
s.push_back(data_t(px + x, pw + x * w));
a.push_back(data_t(x, w));
}
int64_t calc(int64_t len) const {
if (s.empty()) return 0;
if (len >= s.back().length) return s.back().w;
auto it = lower_bound(s.begin(), s.end(), data_t(len, -1)) - s.begin();
if (it == 0) return a[0].calc(len);
else return a[it].calc(len - s[it - 1].length) + s[it - 1].w;
}
void erase(int64_t w) {
while (!s.empty() && w) {
if (a.back().length <= w)
s.pop_back(), w -= a.back().length, a.pop_back();
else {
s.pop_back(); a.back().length -= w; w = 0;
int px = s.empty() ? 0 : s.back().length;
int64_t pw = s.empty() ? 0 : s.back().w;
s.push_back(data_t(a.back().length + px, pw + a.back().calc(a.back().length)));
}
}
}
int length(void) const {
return s.empty() ? 0 : s.back().length;
}
void clear(void) {
a.clear(), s.clear();
}
};
vector<tag_t> mem;
tag_t ta[Maxn];
int td[Maxn];
struct lazy {
int64_t i, length, w;
lazy() = default;
lazy(int64_t i, int64_t len) : i(i), length(len) { }
lazy(data_t d) : i(-1), length(d.length), w(d.w) { }
int64_t calc(int64_t x) const {
// fprintf(stderr, "--- i = %d, x = %d, length = %d\n", i, x, length);
if (i != -1) return mem[i].calc(min(length, x));
else return w * min(length, x);
}
};
struct Stack {
vector<lazy> a;
vector<data_t> s;
Stack() = default;
void insert(int64_t x, int64_t w) {
int64_t px = a.empty() ? 0 : s.back().length;
int64_t pw = a.empty() ? 0 : s.back().w;
a.push_back(lazy(data_t(x, w)));
s.push_back(data_t(px + x, pw + a.back().calc(x)));
}
void insert_lazy(int64_t i, int64_t len) {
int64_t px = a.empty() ? 0 : s.back().length;
int64_t pw = a.empty() ? 0 : s.back().w;
int64_t x = mem[i].length();
a.push_back(lazy(i, len));
s.push_back(data_t(px + x, pw + a.back().calc(x)));
}
void erase(int64_t w) {
while (!a.empty() && w) {
if (a.back().length <= w)
s.pop_back(), w -= a.back().length, a.pop_back();
else {
s.pop_back(); a.back().length -= w; w = 0;
int px = s.empty() ? 0 : s.back().length;
int64_t pw = s.empty() ? 0 : s.back().w;
s.push_back(data_t(a.back().length + px, pw + a.back().calc(a.back().length)));
}
}
}
int64_t calc(int64_t len) {
if (s.empty() || len <= 0) return 0;
if (len >= s.back().length) return s.back().w;
auto it = lower_bound(s.begin(), s.end(), data_t(len, -1)) - s.begin();
// fprintf(stderr, "len = %lld, it = %d\n", len, it);
if (it == 0) return a[0].calc(len);
else return a[it].calc(len - s[it - 1].length) + s[it - 1].w;
}
} a[Maxn];
void pushtag(int b) {
if (td[b] != 0) {
for (int i = bL[b]; i <= bR[b]; ++i)
a[i].erase(td[b]);
td[b] = 0;
}
if (!ta[b].a.empty()) {
mem.push_back(ta[b]);
int j = mem.size() - 1;
for (int i = bL[b]; i <= bR[b]; ++i) {
// fprintf(stderr, "### i = %d, j = %d\n", i, j);
a[i].insert_lazy(j, mem[j].length());
}
ta[b].clear();
}
}
void add_tag(int b, int64_t len, int64_t w) {
ta[b].insert(len, w);
} // add_tag
void del_tag(int b, int64_t len) {
if (len >= ta[b].length()) td[b] += len - ta[b].length(), ta[b].clear();
else ta[b].erase(len);
} // del_tag
int main(void) {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> qn; bs = (int)sqrt(n);
// fprintf(stderr, "bs = %d\n", bs); fflush(stderr);
for (int i = 1; i <= n; ++i)
bel[i] = (i - 1) / bs + 1;
bn = bel[n];
for (int i = 1; i <= bn; ++i) {
bL[i] = (i - 1) * bs + 1;
bR[i] = min(i * bs, n);
}
while (qn--) {
int op, l, r; cin >> op;
if (op == 1) {
cin >> l >> r;
int64_t x, y; cin >> x >> y;
if (bel[l] == bel[r]) {
pushtag(bel[l]);
for (int i = l; i <= r; ++i) a[i].insert(x, y);
} else {
pushtag(bel[l]), pushtag(bel[r]);
for (int i = l; i <= bR[bel[l]]; ++i) a[i].insert(x, y);
for (int i = bL[bel[r]]; i <= r; ++i) a[i].insert(x, y);
for (int i = bel[l] + 1; i <= bel[r] - 1; ++i)
add_tag(i, x, y);
}
} else if (op == 2) {
int64_t w; cin >> l >> r >> w;
if (bel[l] == bel[r]) {
pushtag(bel[l]);
for (int i = l; i <= r; ++i) a[i].erase(w);
} else {
pushtag(bel[l]), pushtag(bel[r]);
for (int i = l; i <= bR[bel[l]]; ++i) a[i].erase(w);
for (int i = bL[bel[r]]; i <= r; ++i) a[i].erase(w);
for (int i = bel[l] + 1; i <= bel[r] - 1; ++i)
del_tag(i, w);
}
} else if (op == 3) {
int64_t k, p, q;
cin >> k >> p >> q;
pushtag(bel[k]);
int64_t aq = a[k].calc(q), ap = a[k].calc(p - 1);
// cerr << "aq = " << aq << ", ap = " << ap << endl;
cout << aq - ap << endl;
}
}
return 0;
} // main
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 6188kb
input:
4907 4910 2 763 3330 1 3 307 1 1 1 2262 3430 22699 89397 1 1915 4000 51541 67587 2 212 2990 9763 2 1086 2162 1 2 1813 4496 16760 1 51 2796 68005 99390 1 1267 1519 74236 66178 3 1768 23808 54314 2 900 4122 27758 3 3287 17350 28989 2 3277 4024 3633 2 444 4866 1 2 353 4219 1061 1 987 3141 99906 17320 2...
output:
0 3032090730 903396180 471569175 200648623 98486697 647114751 123945 50793012 61782451 0 0 0 762429740 321140700 871619914 536311874 5361094892 0 1792521566 6640518748 2415375780 249435711 225987900 5250788038 1145132507 140071334 0 118545795 3086405469 5646099271 84280112 1232466642 4992966775 7968...
result:
ok 1622 numbers
Test #2:
score: 0
Accepted
time: 10ms
memory: 8276kb
input:
4992 4958 2 2965 3892 1 3 2141 1 1 3 4963 1 1 3 2298 1 1 3 2236 1 1 1 3393 4668 65533 8224 1 884 2343 86158 41289 3 4617 12174 63763 2 898 4236 44143 2 2860 4246 1 2 2696 3014 1 2 496 1635 15779 3 2230 8262 39805 2 3287 3627 5350 2 3443 4900 19874 1 535 1622 26926 88406 1 3272 3747 94290 19081 2 812...
output:
0 0 0 0 424276160 1302420216 0 393525459 0 188112684 0 38587680 696225296 717180100 2193537294 297696966 0 0 0 124461621 26876032 1609925499 0 3681040200 51602516 1502016 0 8636592 1138256753 196684293 0 16126264 959145423 58640451 1945754097 2949696960 0 3577791360 2029416288 2361290004 5882833609 ...
result:
ok 1597 numbers
Test #3:
score: 0
Accepted
time: 20ms
memory: 10304kb
input:
4980 4984 1 183 4891 75896 45281 2 767 3528 1367 3 2313 45535 49112 2 529 4006 1568 2 2114 2971 3819 3 3237 30655 31381 1 2074 2176 51631 35339 3 1602 95 16082 2 1340 3727 9249 2 105 1907 11928 3 2461 15189 33999 2 1450 1956 4721 1 700 4760 3043 92859 2 329 2992 6515 3 1295 10832 40486 2 3477 4447 8...
output:
162015418 32919287 723952628 851780891 1342808055 38307726 4701651115 903944603 240532672 652952020 1168430924 2253203546 3542990917 5872603595 305017015 657095398 25321688 1834305604 0 256832266 2732654054 1757936801 1158797383 656866283 3470700279 694675745 1042863834 76284096 6705704850 475899629...
result:
ok 1645 numbers
Test #4:
score: 0
Accepted
time: 20ms
memory: 10120kb
input:
4976 4948 2 858 1218 1 1 780 1528 70910 12344 1 681 4398 25997 59182 1 4564 4692 72420 96925 1 1124 2604 98159 98651 3 4734 1 1 2 1921 3430 3373 1 3805 3909 56118 23383 2 1471 2065 23679 2 1052 1154 30740 1 1098 2180 13716 29728 1 1094 3585 2073 93894 1 2024 4201 39023 1713 3 1571 21453 96893 3 1297...
output:
0 7262943486 185110624 53327400 957813600 383014415 1539405803 896522316 1454164560 7158196459 479198625 1943839360 1189657450 23355822139 2684778350 183742084 6400082784 2310401225 2082631008 5644811789 1875949890 3185562597 7185156304 3147144197 1588457333 676240200 1122598010 8758314557 100699296...
result:
ok 1663 numbers
Test #5:
score: 0
Accepted
time: 18ms
memory: 13684kb
input:
4944 4934 1 468 4845 87517 63656 3 4756 22899 79177 1 761 1054 45331 86403 1 24 2806 46189 11490 1 2602 4446 12528 14317 3 2601 51537 65051 1 1502 3573 79699 84830 3 1998 35405 151264 1 2400 4041 95071 83748 1 2050 3772 23643 53614 3 2261 51072 236192 2 1317 1908 6197 2 949 2543 30190 1 1457 4573 33...
output:
3582496024 860310840 5337461878 10833286574 1397502876 3735482073 4207877274 17671620 10854427218 1484917319 5462491276 1497165465 1453546510 1672688712 1158344316 1014734250 3797802047 15668090927 14634073116 32337553147 2159971110 12088416736 90924880 1410366456 13829776128 12126485158 18393654569...
result:
ok 829 numbers
Test #6:
score: -100
Time Limit Exceeded
input:
99999 99998 1 5026 18575 27178 90423 3 30623 1 1 3 76936 1 1 1 77021 95683 84664 24734 1 46085 74886 40512 11266 3 5048 8594 22468 1 53318 77721 97151 70784 1 70645 91192 37556 13013 1 56752 56940 91812 62887 1 7928 34576 87339 69404 3 74875 32807 100970 3 22338 17221 25771 3 21421 20602 57957 3 717...
output:
0 0 1254619125 4366274868 593473604 2592655824 3657975552 5652513833 110091352 1226646296 1989326852 763582808 8205318671 1659086055 3012598941 20085582585 3242801176 17381308704 24555397019 4722824224 20308857160 899316516 38935050954 988382364 13341823621 11397759491 2449683584 5875277101 80572355...