QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356226 | #8286. Stacks | ckiseki# | TL | 1512ms | 967432kb | C++20 | 5.4kb | 2024-03-17 16:51:34 | 2024-03-17 16:51:34 |
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
using lld = int64_t;
namespace treap {
struct Node {
lld tot_copy, sum;
int val;
int copy;
int lc, rc;
int sz;
};
const int maxn = 100025, lg = 20;
constexpr int MEM = 24'000'000;
Node T[MEM];
bool inuse[MEM];
int counter;
int inc_counter() {
while (inuse[counter+1]) {
++counter;
}
return ++counter;
}
void pull(int o) {
T[o].sz = 1;
T[o].tot_copy = T[o].copy;
T[o].sum = 1LL * T[o].copy * T[o].val;
for (int c : {T[o].lc, T[o].rc})
if (c) {
T[o].sz += T[c].sz;
T[o].tot_copy += T[c].tot_copy;
T[o].sum += T[c].sum;
}
}
int new_node(int x, int y) {
int ret = inc_counter();
T[ret].val = x;
T[ret].copy = y;
T[ret].lc = T[ret].rc = 0;
pull(ret);
return ret;
}
mt19937 rng(114514);
int merge(int a, int b) {
if (!a || !b) return a ? a : b;
assert(T[a].sz + T[b].sz - 1 >= 0);
int r = uniform_int_distribution<int>(0, T[a].sz + T[b].sz - 1)(rng);
int ret = inc_counter();
if (r < T[a].sz) {
T[ret] = T[a];
T[ret].rc = merge(T[a].rc, b);
} else {
T[ret] = T[b];
T[ret].lc = merge(a, T[b].lc);
}
pull(ret);
return ret;
}
lld query(int o, lld k) {
if (!k) return 0;
const lld s = T[o].tot_copy;
assert(s >= k);
lld left = T[o].lc ? T[T[o].lc].tot_copy : 0;
if (k <= left) {
return query(T[o].lc, k);
} else if (k <= left + T[o].copy) {
return (T[o].lc ? T[T[o].lc].sum : 0) + 1LL * T[o].val * (k - left);
} else {
return (T[o].lc ? T[T[o].lc].sum : 0) + 1LL * T[o].val * T[o].copy + query(T[o].rc, k - left - T[o].copy);
}
}
int splitPrefix(int o, lld k) {
assert(o > 0);
if (!k) return 0;
lld left = T[o].lc ? T[T[o].lc].tot_copy : 0;
if (k <= left) {
return splitPrefix(T[o].lc, k);
} else if (k <= left + T[o].copy) {
int ret = inc_counter();
T[ret].val = T[o].val;
T[ret].copy = k - left;
T[ret].lc = T[o].lc;
T[ret].rc = 0;
pull(ret);
return ret;
} else {
int ret = inc_counter();
T[ret].val = T[o].val;
T[ret].copy = T[o].copy;
T[ret].lc = T[o].lc;
T[ret].rc = splitPrefix(T[o].rc, k - left - T[o].copy);
pull(ret);
return ret;
}
}
int pop_stack(int o, lld k) {
const lld s = T[o].tot_copy;
assert(s >= k);
return splitPrefix(o, s - k);
}
void dump(int o, vector<pair<lld,lld>> &v) {
if (!o) return;
dump(T[o].lc, v);
v.emplace_back(T[o].val, T[o].copy);
dump(T[o].rc, v);
}
int mark = 0;
void scan(int o) {
if (!o or inuse[o])
return;
mark++;
inuse[o] = true;
scan(T[o].lc), scan(T[o].rc);
}
}
struct Tag {
lld pop;
int push;
Tag() : pop(0), push(0) {}
Tag &operator+=(const Tag &rhs) {
lld sz = push ? treap::T[push].tot_copy : 0;
if (rhs.pop >= sz) {
pop += rhs.pop - sz;
push = rhs.push;
} else {
push = treap::pop_stack(push, rhs.pop);
push = treap::merge(push, rhs.push);
}
return *this;
}
};
namespace std {
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p) {
return o<<p.first<<','<<p.second;
}
}
struct Segtree {
int n;
vector<Tag> st;
Segtree(int n_) : n(n_), st(n * 2) {}
void upd(int p, const Tag &tg) {
st[p] += tg;
}
void push(int p) {
for (int h = __lg(n); h >= 0; h--) {
int i = p >> h;
if (i <= 1) continue;
upd(i, st[i >> 1]);
upd(i ^ 1, st[i >> 1]);
st[i >> 1] = Tag();
}
}
void apply(int l, int r, const Tag &tg) {
push(l + n), push(r - 1 + n);
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) upd(l++, tg);
if (r & 1) upd(--r, tg);
}
}
lld query(int p, lld L, lld R) {
--L;
push(p += n);
int node = st[p].push;
lld sz = node ? treap::T[node].tot_copy : 0;
L = clamp<lld>(L, 0, sz);
R = clamp<lld>(R, 0, sz);
// vector<pair<lld,lld>> v;
// treap::dump(node, v);
// orange(all(v));
return treap::query(node, R) - treap::query(node, L);
}
void scan() {
for (int i = 0; i < 2*n; ++i) {
treap::scan(st[i].push);
}
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
Segtree sgt(n);
while (m--) {
if (treap::counter > treap::MEM - 2000) {
treap::mark = 0;
memset(treap::inuse, 0, sizeof(treap::inuse));
sgt.scan();
debug("rebuild", treap::counter, treap::mark);
treap::counter = 0;
}
int t;
cin >> t;
if (t == 1) {
int l, r, x, y;
cin >> l >> r >> x >> y;
--l;
Tag tg;
tg.push = treap::new_node(y, x);
sgt.apply(l, r, tg);
} else if (t == 2) {
int l, r;
lld w;
cin >> l >> r >> w;
--l;
Tag tg;
tg.pop = w;
sgt.apply(l, r, tg);
} else {
int k;
lld p, q;
cin >> k >> p >> q;
--k;
cout << sgt.query(k, p, q) << '\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9780kb
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: 6ms
memory: 10024kb
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: 10ms
memory: 13880kb
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: 3ms
memory: 15904kb
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: 13ms
memory: 24120kb
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: 0
Accepted
time: 1512ms
memory: 967120kb
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...
result:
ok 49797 numbers
Test #7:
score: 0
Accepted
time: 1468ms
memory: 967432kb
input:
100000 99994 3 92225 1 1 3 10037 1 1 3 74283 1 1 3 2266 1 1 3 50962 1 1 3 81075 1 1 1 503 97219 60712 28632 1 20530 96581 71439 60132 3 41317 2559 104920 1 38204 95641 16185 4127 1 81150 90487 12130 78009 3 26946 49794 94793 3 30822 94956 120229 3 3139 25745 34556 1 31304 71829 59249 1581 1 34152 81...
output:
0 0 0 0 0 0 4323380784 2361991500 1519776168 252305184 5480718598 475075230 355440252 50964960 470925609 563617236 5250822628 167047570 5642890643 263790112 16650744580 9469618067 1523342134 5252794500 3397151474 44843260 15925043590 7615102649 642874296 805218390 12371730590 946262746 19013805530 1...
result:
ok 49918 numbers
Test #8:
score: -100
Time Limit Exceeded
input:
99992 100000 1 47419 74011 81562 83218 1 24365 27999 38558 68071 1 28685 99034 50336 45319 1 9957 51419 65767 96547 1 26613 48906 69394 57314 1 53501 94770 64814 21312 1 17181 66742 69740 22586 3 16409 7244 53001 1 88013 89718 83001 55044 1 37425 73824 89216 49784 1 14648 78441 67461 78275 1 9378 35...
output:
4417797626 26979560445 4524699650 21738613173 262351691 12978001923 17651184324 0 273579160 4411564116 1970670680 17453286633 14834332800 20575290994 10992737164 42531639041 7318986928 7239761756 24667868512 11295443456 33762100957 3516593780 14180850735 92220338161 448071480 72233196570 82174722698...