QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#723922 | #4429. Gebyte's Grind | maspy | AC ✓ | 2367ms | 148472kb | C++23 | 10.1kb | 2024-11-08 03:38:13 | 2024-11-08 03:38:14 |
Judging History
answer
#line 2 "/home/maspy/library/my_template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ll8 = __int128;
using ld = long double;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using vvvvvc = vector<vvvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define FOR3(i, m, n) for (ll i = (m); (i) < (ll)(n); ++(i))
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
#define FOR3_R(i, m, n) for (ll i = (ll)(n)-1; (i) >= (ll)(m); --(i))
#define FORIN(x, A) for (auto&& x : A)
#define FOR_nCk(s, n, k) \
for (ll s = (1 << k) - 1, tmp_var = 0; s < (1 << n); \
tmp_var = s | (s - 1), s = (tmp_var + 1) | (((~tmp_var & -~tmp_var) - 1) >> (__builtin_ctz(s) + 1)))
#define FOR_SUB(t, s) for(ll t = s; t >= 0; t = (t==0 ? -1 : (t - 1) & s))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
using ull = unsigned long long;
using uint = unsigned int;
int popcnt(uint x) {
return __builtin_popcount(x);
}
int popcnt(int x) {
return __builtin_popcount(x);
}
int popcnt(ull x) {
return __builtin_popcountll(x);
}
int popcnt(ll x) {
return __builtin_popcountll(x);
}
int bsr(uint x) {
return 31 - __builtin_clz(x);
}
int bsr(ull x) {
return 63 - __builtin_clzll(x);
}
int ctz(int x) {
return __builtin_ctz(x);
}
int ctz(ll x) {
return __builtin_ctzll(x);
}
int ctz(ull x) {
return __builtin_ctzll(x);
}
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <class T> T ceil(T x, T y) {
assert(y >= 1);
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
assert(y >= 1);
return (x > 0 ? x / y : (x - y + 1) / y);
}
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DBL(...) \
long double __VA_ARGS__; \
IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &a) {for(auto &i : a) scan(i);}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
ll isqrt(ll n) {
ll x = n, y = (n + 1) / 2;
while (y < x) { tie(x, y) = mp(y, (y + n / y) / 2); }
return x;
}
vi s_to_vi(string S, char first_char='a'){
vi A(S.size());
FOR(i, S.size()){
A[i] = S[i] - first_char;
}
return A;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
os << A.fi << " " << A.se;
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
for (size_t i = 0; i < A.size(); i++) {
if(i) os << " ";
os << A[i];
}
return os;
}
void print() {
cout << "\n";
}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(Tail)) cout << " ";
print(forward<Tail>(tail)...);
}
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
template <typename T>
vector<T> cumsum(vector<T> A) {
ll N = A.size();
vector<T> B(N + 1);
B[0] = T(0);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
return B;
}
vi bin_count(vi& A, ll size) {
vi C(size);
for (auto&& x : A) {
++C[x];
}
return C;
}
template <typename T>
vi argsort(vector<T>& A){
vi ids(A.size());
iota(all(ids), 0);
sort(all(ids), [&](ll i, ll j){
return A[i] < A[j] || (A[i] == A[j] && i < j);
});
return ids;
}
ll binary_search(function<bool(ll)> check, ll ok, ll ng) {
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
if (check(x))
ok = x;
else
ng = x;
}
return ok;
}
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
template <typename T>
vc<T> merge_sort(vc<T>& A, vc<T>& B) {
vc<T> C;
C.reserve(A.size() + B.size());
merge(all(A), all(B), back_inserter(C));
return C;
}
#line 2 "main.cpp"
#line 3 "/home/maspy/library/ds/segtree.hpp"
template <typename T>
struct SegTree{
using F = function<T(T,T)>;
int N_;
int N;
F seg_f;
T T_unit;
vector<T> dat;
SegTree(F f,T T_unit): seg_f(f), T_unit(T_unit) {}
void init(int n_){
N_ = n_;
N=1;
while(N<n_) N<<=1;
dat.assign(N<<1,T_unit);
}
void build(const vector<T> &v){
init(v.size());
FOR(i, v.size()) dat[N + i] = v[i];
FOR3_R(i, 1, N){
dat[i] = seg_f(dat[i<<1 | 0], dat[i<<1 | 1]);
}
}
void set(int i, T x){
assert(i < N_);
dat[i += N] = x;
while(i >>= 1){
dat[i] = seg_f(dat[i<<1 | 0],dat[i<<1 | 1]);
}
}
T fold(int L, int R){
assert(L <= R);
assert(R <= N_);
T vl = T_unit, vr = T_unit;
L += N;
R += N;
while(L < R){
if(L & 1) vl = seg_f(vl, dat[L++]);
if(R & 1) vr = seg_f(dat[--R], vr);
L >>= 1;
R >>= 1;
}
return seg_f(vl, vr);
}
template<class F>
int max_right(int L, F &check){
assert(0<=L && L <= N_ && check(T_unit));
if(L==N_) return N_;
L += N;
T sm = T_unit;
do {
while(L%2==0) L>>=1;
if(!check(seg_f(sm, dat[L]))){
while(L < N) {
L = 2 * L;
if(check(seg_f(sm, dat[L]))){
sm = seg_f(sm, dat[L]);
L++;
}
}
return L - N;
}
sm = seg_f(sm, dat[L]);
L++;
} while((L&-L)!=L);
return N_;
}
template<class F>
int min_left(int R, F &check){
assert(0<=R && R <= N_ && check(T_unit));
if(R==0) return 0;
R += N;
T sm = T_unit;
do {
--R;
while(R>1 && (R%2)) R>>=1;
if(!check(seg_f(dat[R], sm))){
while(R < N) {
R = 2 * R + 1;
if(check(seg_f(dat[R], sm))){
sm = seg_f(dat[R], sm);
R--;
}
}
return R + 1 - N;
}
sm = seg_f(dat[R], sm);
} while((R&-R)!=R);
return 0;
}
};
#line 4 "main.cpp"
void solve() {
const ll INF = 1LL << 60;
LL(N, Q, H);
struct E {
// a 未満から始めると死亡。
// a 以上のxから始めた場合、max(x+b,c) に変化する
ll a, b, c;
};
SegTree<E> seg(
[&](E P, E Q) {
E R;
R.a = P.a;
if (P.c < Q.a) chmax(R.a, Q.a - P.b);
R.b = P.b + Q.b;
R.c = max(P.c + Q.b, Q.c);
chmin(R.a, INF);
chmax(R.a, -INF);
chmin(R.b, INF);
chmax(R.b, -INF);
chmin(R.c, INF);
chmax(R.c, -INF);
return R;
},
E({-INF, 0, -INF}));
auto to_data = [&](char c, ll val) -> E {
if (c == 'B') return E({val + 1, -val, -INF});
if (c == 'K') return E({val, -INF, val});
if (c == 'C') return E({-INF, 0, val});
};
seg.init(N);
vc<E> seg_raw(N);
FOR(i, N) {
CHR(c);
LL(val);
seg_raw[i] = to_data(c, val);
}
seg.build(seg_raw);
FOR(q, Q) {
CHR(t);
LL(i);
--i;
if (t == 'Z') {
CHR(d);
LL(val);
seg.set(i, to_data(d, val));
} else {
auto check = [&](E P) -> bool { return H >= P.a; };
auto R = seg.max_right(i, check);
if (i == R) R = -1;
print(R);
}
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15);
LL(T);
FOR(_, T) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2367ms
memory: 148404kb
input:
1 2000000 4000000 1000000000000 B 2982992025 B 1226542907 B 2299259203 B 1223702056 B 1407121251 B 340783317 B 1259091208 B 2101980047 B 2611543443 B 2010658889 B 4233714406 B 3112120167 B 2311876255 B 2789960428 B 3008572010 B 1 B 2464951378 B 1364240867 B 2829584762 B 2511437438 B 692948679 B 1113...
output:
833302 238155 1007466 1912727 1483707 996123 913464 595444 1956432 168794 1224759 214012 1606540 541216 578117 1279590 1652446 647870 900696 1010723 1723225 1909185 765245 1770241 994028 135066 146309 423001 379625 188229 561102 1020950 25262 1007466 624537 1150303 892424 856521 175916 1187336 16896...
result:
ok 2911608 lines
Test #2:
score: 0
Accepted
time: 2006ms
memory: 148320kb
input:
1 2000000 4000000 900000000000 B 18921988072 B 1 B 162148155907 C 1000000000000 B 331992393119 K 428836058413 B 440712983778 B 534362851268 B 923013640108 B 472973869469 B 21294011490 B 1 B 1000000000000 B 76485869786 C 1000000000000 C 333159763195 B 1 B 277856669933 B 1 C 445619227450 B 86360111824...
output:
1625020 1618321 511712 1446036 1302385 244605 534722 1807821 1673978 -1 -1 1286986 650766 1419851 -1 -1 510520 -1 1996906 814567 719623 1737532 180266 285086 -1 1455059 11092 1030131 1479157 372584 399911 1897918 1585202 566085 1522965 63081 1721860 673731 1530763 -1 -1 134340 1467445 -1 1410807 -1 ...
result:
ok 1999947 lines
Test #3:
score: 0
Accepted
time: 2296ms
memory: 148472kb
input:
1 2000000 4000000 1000000000000 B 17342013 B 14555766 B 26630469 B 66103720 B 62383790 B 17433493 B 66256092 B 74496332 B 66470344 B 17971159 B 46755192 B 33871545 B 47843886 B 17737257 B 91180218 B 2450298 B 31650513 B 2066148 B 82311128 B 56600828 B 12144701 B 83637831 B 73789298 B 108092 B 684688...
output:
6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 6137 ...
result:
ok 2002501 lines
Test #4:
score: 0
Accepted
time: 459ms
memory: 3592kb
input:
100000 30 36 694566294336 B 399381235378 K 116128223667 B 571309322680 B 3999476334 C 694813305215 B 242568539922 K 275534906854 B 627467159442 C 603373692516 B 736482925501 B 857566416940 B 192161825500 B 709599212240 B 402172637373 B 400573894654 B 256573224769 B 294629292373 B 267978037726 B 7412...
output:
-1 16 21 -1 -1 23 9 7 -1 2 6 -1 24 22 -1 14 -1 26 -1 5 -1 -1 -1 14 -1 -1 6 -1 14 5 6 -1 -1 -1 -1 2 -1 7 2 -1 -1 7 11 11 11 11 11 11 11 11 11 11 11 2 7 7 11 2 -1 2 11 4 8 6 10 4 15 15 15 -1 15 8 15 -1 -1 -1 15 7 15 -1 -1 15 7 15 -1 15 7 6 6 9 -1 12 6 6 12 6 9 7 7 8 3 -1 -1 -1 5 -1 -1 13 20 11 22 -1 -...
result:
ok 927823 lines
Extra Test:
score: 0
Extra Test Passed