QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#814747 | #9877. Segment Tree | ucup-team4435# | WA | 19ms | 3616kb | C++23 | 4.1kb | 2024-12-14 20:17:26 | 2024-12-14 20:17:28 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ar array
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const int INFi = 2e9;
const ll INF = 2e18;
void solve() {
int n; cin >> n;
n = 1 << n;
vl c(n * 2);
for(int i = 1; i < c.size(); ++i) cin >> c[i];
auto d = c;
for(int i = n - 1; i >= 1; --i) {
d[i] = min(c[i], d[i * 2] + d[i * 2 + 1]);
}
auto Change = [&] (int i, ll x) {
c[i] = x;
for(; i >= 1; i /= 2) {
d[i] = min(c[i], d[i * 2] + d[i * 2 + 1]);
}
};
auto Find = [&] (int i) {
vector<pair<int, ll>> res;
i += n;
res.emplace_back(i, 0);
ar<ll, 2> result = {0, 0};
int l = i;
int r = i;
if (i == n) {
res.emplace_back(i + 1, d[i]);
res.emplace_back(i + 2, d[i / 2]);
r = i + 2;
result[1] = d[i / 2];
} else if (i % 2 == 0) {
res.emplace_back(i - 1, d[i - 1]);
res.emplace_back(i - 2, d[(i - 2) / 2]);
l = i - 2;
result[0] = d[(i - 2) / 2];
} else {
res.emplace_back(i - 1, d[i - 1]);
res.emplace_back(i + 1, d[i]);
l = i - 1;
r = i + 1;
result[0] = d[i - 1];
result[1] = d[i];
}
int shift = 1;
while ((1 << shift) < n) {
ckmin(result[0], result[1] + d[l >> shift]);
ckmin(result[1], result[0] + d[l >> shift]);
res.emplace_back(l, result[0]);
res.emplace_back(r, result[1]);
int step = (1 << shift);
if (l & step) {
l -= step;
result[0] += d[l >> shift];
} else {
result[1] += d[r >> shift];
r += step;
}
shift++;
}
ckmin(result[0], result[1] + d[l >> shift]);
ckmin(result[1], result[0] + d[l >> shift]);
res.emplace_back(l, result[0]);
res.emplace_back(r, result[1]);
sort(all(res));
return res;
};
int q; cin >> q;
rep(_, q) {
int tp; cin >> tp;
if (tp == 1) {
int j; ll x; cin >> j >> x;
Change(j, x);
continue;
}
int s, t; cin >> s >> t;
auto val1 = Find(s);
auto val2 = Find(t);
ll ans = INF;
for(auto &[v, dv] : val2) {
int it = std::lower_bound(val1.begin(), val1.end(), make_pair(v, -1ll)) - val1.begin();
if (it == val1.size() || val1[it].first != v) continue;
ckmin(ans, val1[it].second + dv);
}
cout << ans << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(8) << fixed;
int t = 1;
// cin >> t;
rep(i, t) {
solve();
}
return 0;
}
/*
* 3
7 1 14 3 9 4 8 2 6 5 5 13 8 2 3
1
2 4 8
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
3 7 1 14 3 9 4 8 2 6 5 5 13 8 2 3 10 2 0 1 2 0 4 2 4 6 2 4 8 2 3 5 1 6 30 2 3 5 2 4 6 1 1 10000000 2 0 8
output:
2 1 4 8 17 18 13 15
result:
ok 8 tokens
Test #2:
score: -100
Wrong Answer
time: 19ms
memory: 3616kb
input:
1 7914575 2436426 4979445 199989 1 1 6190629 1 1 1407775 1 1 2804784 1 2 2631932 1 1 3078537 1 3 286918 1 2 3238506 1 3 3361868 1 2 9296263 1 3 4836991 1 3 2177068 1 3 4291757 1 1 594328 1 2 8996221 1 1 5531545 1 3 3575467 1 3 3206504 1 1 8344965 1 3 6045895 2 0 2 1 2 6248153 1 1 5797489 1 1 9766466...
output:
101537 4403205 187614 2512448 130126 6420173 2615941 6363152 33 4380822 33 4042733 33 7492420 2905267 33 7609095 371725 33 2539201 33 4938775 33 33 6347030 3327163 33 330511 2783881 3409112 2827280 33 4704960 33 7152786 33 2599805 1067438 33 33 2950116 2911344 33 33 33 33 3732617 3507956 1083383 379...
result:
wrong answer 1st words differ - expected: '8344965', found: '101537'