QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#884829 | #9877. Segment Tree | w4p3r | WA | 20ms | 5732kb | C++20 | 2.8kb | 2025-02-06 11:07:15 | 2025-02-06 11:07:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 5e8;
#define N 20
#define ls (x << 1)
#define rs (x << 1 | 1)
#define mid ((l + r) >> 1)
int c[1 << N], dis[1 << N];
int n, q;
void pushup(int x)
{
dis[x] = min(c[x], dis[ls] + dis[rs]);
}
void build(int l, int r, int x)
{
if (l == r) {dis[x] = c[x]; return ;}
build(l, mid, ls); build(mid + 1, r, rs);
pushup(x);
}
void qry(int l, int r, int x, int s, int t, int &ans, int &sl, int &sr, int &tl, int &tr)
{
if (((s < l - 1) || (s > r)) && ((t < l - 1) || (t > r))) return ;
if (l == r)
{
if (s == l - 1)sl = 0, sr = c[x];
if (s == r)sl = c[x], sr = 0;
if (t == l - 1)tl = 0, tr = c[x];
if (t == r)tl = c[x], tr = 0;
if (s == l - 1 && t == r)ans = c[x];
return ;
}
int ansL, slL, srL, tlL, trL;
int ansR, slR, srR, tlR, trR;
ansL = slL = srL = tlL = trL = ansR = slR = srR = tlR = trR = inf;
qry(l, mid, ls, s, t, ansL, slL, srL, tlL, trL);
qry(mid + 1, r, rs, s, t, ansR, slR, srR, tlR, trR);
ans = min(ansL, ansR);
if (s >= l - 1 && s <= mid)
{
sl = min(sl, min(slL, srL + dis[rs] + c[x]));
sr = min(sr, min(srL + dis[rs], slL + c[x]));
}
if (s >= mid && s <= r)
{
sl = min(sl, min(slR + dis[ls], srR + c[x]));
sr = min(sr, min(srR, slR + dis[ls] + c[x]));
}
if (t >= l - 1 && t <= mid)
{
tl = min(tl, min(tlL, trL + dis[rs] + c[x]));
tr = min(tr, min(trL + dis[rs], tlL + c[x]));
}
if (t >= mid && t <= r)
{
tl = min(tl, min(tlR + dis[ls], trR + c[x]));
tr = min(tr, min(trR, tlR + dis[ls] + c[x]));
}
ans = min(ans, sl + c[x] + tr);
ans = min(ans, sr + c[x] + tl);
ans = min(ans, srL + tlR);
ans = min(ans, trL + slR);
// cout << "EEE:" << l << ' ' << r << ' ' << c[x] << endl;
// cout << ans << ' ' << sl << ' ' << sr << ' ' << tl << ' ' << tr << endl;
}
void sol()
{
cin >> n;
for (int i = 1; i <= (1 << n + 1) - 1; i ++)cin >> c[i];
build(1, (1 << n), 1);
cin >> q;
while (q --)
{
int op; cin >> op;
if (op == 1)
{
int x, v;
cin >> x >> v;
c[x] = v;
while (x) {pushup(x); x >>= 1;}
}
if (op == 2)
{
int s, t;
cin >> s >> t;
int ans, sl, sr, tl, tr;
ans = sl = sr = tl = tr = inf;
qry(1, (1 << n), 1, s, t, ans, sl, sr, tl, tr);
cout << ans << '\n';
}
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while (T --) sol();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5732kb
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: 20ms
memory: 5728kb
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:
8344965 5684734 187581 2512448 130126 6420140 2615908 6363152 5163219 4380822 7995571 4042733 3236055 8653391 2905234 3506828 8583126 371692 371692 2539201 5810476 8465921 4517278 1913351 7400947 3327130 1759308 6081288 2783848 3409112 2827247 3285702 4704960 5224682 7152753 1622556 2599805 1833019 ...
result:
wrong answer 3rd words differ - expected: '2756417', found: '187581'