QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#814924 | #9877. Segment Tree | ucup-team3691# | WA | 1ms | 5600kb | C++14 | 3.0kb | 2024-12-14 22:46:52 | 2024-12-14 22:46:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
using ll = long long;
using ull = unsigned long long;
string to_string(const string &s) {
return '"' + s + '"';
}
string to_string(bool b) {
return b ? "true" : "false";
}
template <typename A, typename B>
string to_string(const pair<A, B> &p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T>
string to_string(const T &v) {
string s = "{";
bool first = true;
for (const auto &it : v) {
if (!first)
s += ", ";
else
first = false;
s += to_string(it);
}
return s += "}";
}
void debug_out() {
cerr << endl;
}
template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
cerr << to_string(first) << " ";
debug_out(rest...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto startTime = high_resolution_clock::now();
int get_time() {
auto stopTime = chrono::high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stopTime - startTime);
return duration.count(); // in ms
}
struct node {
ll r, d;
};
const int DIM = 3e5 + 5;
const ll INF = 1e18;
int nr;
int c[4 * DIM];
node arb[4 * DIM];
void build(int st, int dr, int nod) {
arb[nod].r = arb[nod].d = c[nod];
if (st == dr) {
arb[nod].d = c[nod];
return;
}
int mij = (st + dr) / 2;
build(st, mij, nod * 2);
build(mij + 1, dr, nod * 2 + 1);
arb[nod].d = min(arb[nod].d, arb[nod * 2].d + arb[nod * 2 + 1].d);
}
void update(int x, int y) {
arb[x].r = y;
while (x > 0) {
arb[x].d = arb[x].r;
if (x * 2 <= nr) {
arb[x].d = min(arb[x].d, arb[x * 2].d + arb[x * 2 + 1].d);
}
x >>= 1;
}
}
ll query(int x, int y, ll up, int st, int dr, int nod) {
if (x <= st && dr <= y) {
return min(up, arb[nod].d);
}
int mij = (st + dr) / 2;
ll ans = 0;
if (x <= mij) {
ll nup = min(arb[nod].d, up) + arb[nod * 2 + 1].d;
ans += query(x, y, nup, st, mij, nod * 2);
}
if (mij + 1 <= y) {
ll nup = min(arb[nod].d, up) + arb[nod * 2].d;
ans += query(x, y, nup, mij + 1, dr, nod * 2 + 1);
}
return ans;
}
void solve() {
int n;
cin >> n;
int l = (1 << n);
nr = l * 2 - 1;
for (int i = 1; i <= nr; ++i) {
cin >> c[i];
}
build(0, l - 1, 1);
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
int t, x, y;
cin >> t >> x >> y;
if (t == 1) {
update(x, y);
} else {
ll ans = INF;
if (x > y)
swap(x, y);
if (x == y) {
cout << 0 << "\n";
continue;
}
cout << query(x, y, INF, 0, l - 1, n) << "\n";
}
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5600kb
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:
12 4 2 5 2 2 2 14
result:
wrong answer 1st words differ - expected: '2', found: '12'