QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#356843 | #8301. Hold the Line | FOY | WA | 260ms | 3836kb | C++23 | 1.8kb | 2024-03-18 13:30:09 | 2024-03-18 13:30:10 |
Judging History
answer
#pragma GCC optimize "Ofast"
#include <vector>
#include <set>
#include <iostream>
using namespace std;
const int inf = 1e9+5;
int closest(int x, set<int> &a) {
auto u = a.upper_bound(x);
auto l = a.lower_bound(x);
int ans = -inf;
if (u != a.begin())
ans = *prev(u);
if (l != a.end())
if (abs(ans-x) > abs(*l-x)) ans = *l;
return ans;
}
int closest(int x, int y, int z) {
if (abs(x-y) < abs(x-z)) return y;
else return z;
}
int bs = 4;
struct Segtree {
int n;
vector<set<int>> tree;
vector<int> vals;
Segtree(int n): n(n), tree(1+((2*n)>>bs)), vals(n, -inf) { }
void insert(int idx, int val) {
vals[idx] = val;
idx += n;
idx >>= bs;
while (idx > 0) {
tree[idx].insert(val);
idx /= 2;
}
}
int find(int l, int r, int targ) {
int sl = l, sr = r;
int out = -inf;
for (int sz=0, l=(l+n),r=(r+n); l < r; l /= 2, r /= 2, sz++) {
if ((l&1)) {
if (sz >= bs) out = closest(targ, out, closest(targ, tree[l]));
l++;
}
if (r&1) {
r--;
if (sz >= bs) out = closest(targ, out, closest(targ, tree[r]));
}
}
l = sl; r = sr;
for (int i = l; i < min(r, l+2*(1<<bs)); i++) {
out = closest(targ, out, vals[i]);
}
for (int i = r-1; i >= max(l, r-2*(1<<bs)); i--) {
out = closest(targ, out, vals[i]);
}
return out;
}
};
void solve() {
int n, m; cin >> n >> m;
Segtree s(n);
for (int i = 0; i < m; i++) {
int type; cin >> type;
if (type == 0) {
int x, h; cin >> x>> h;
s.insert(x-1, h);
}
else {
int l, r, h; cin >> l >> r >> h;
l--;
int ans = s.find(l, r, h);
if (ans == -inf) cout << -1 << '\n';
else cout << abs(ans-h) << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3776kb
input:
1 3 5 1 1 3 2 0 1 1 0 3 3 1 1 2 2 1 2 3 2
output:
-1 1 1
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 260ms
memory: 3836kb
input:
3000 100 200 0 59 64091111 1 10 94 45205032 0 41 67249140 1 15 93 79570187 0 51 83215051 1 3 22 20062363 0 21 5188814 1 43 94 79642299 0 73 39313603 1 43 67 17001771 0 65 10784990 1 51 69 73368509 0 42 57377517 1 36 49 17483147 0 40 67280095 1 3 41 25139505 0 56 22833553 1 26 65 15640242 0 22 189761...
output:
-1 12321047 -1 3572752 47089340 9277398 39894370 19950691 4855252 2221206 1596905 -1 3120922 34260194 3353597 -1 2499997 -1 15114024 4972499 49448136 734969 3981124 4159424 5836824 61155540 5163746 -1 283130 3982548 -1 -1 -1 9647216 2356693 8711627 379947 70230536 2673215 7856589 153976 148089 18808...
result:
wrong answer 1st lines differ - expected: '18886079', found: '-1'