QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#59765 | #1375. TripTik | Sa3tElSefr | WA | 3ms | 5620kb | C++20 | 3.5kb | 2022-11-01 03:15:10 | 2022-11-01 03:15:13 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define ll long long
#define ld double
using namespace std;
const int N = 2e5 + 5, lg = 19, mod = 998244353;
vector<pair<int, int> > a;
pair<int, int> tree[N << 2];
void build(int node, int l, int r) {
if(l == r) {
tree[node] = {a[l].second, l};
return;
}
int mid = l + r >> 1;
build(node << 1, l, mid);
build(node << 1 | 1, mid + 1, r);
tree[node] = max(tree[node << 1], tree[node << 1 | 1]);
}
void update(int node, int l, int r, int idx, int val) {
if(l == r) {
tree[node] = {val, l};
return;
}
int mid = l + r >> 1;
if(idx <= mid) update(node << 1, l, mid, idx, val);
else update(node << 1 | 1, mid + 1, r, idx, val);
tree[node] = max(tree[node << 1], tree[node << 1 | 1]);
}
pair<int, int> get(int node, int l, int r, int a, int b) {
if(a > b || a > r || l > b) return {-1, -1};
if(a <= l && r <= b) return tree[node];
int mid = l + r >> 1;
return max(get(node << 1, l, mid, a, b),
get(node << 1 | 1, mid + 1, r, a, b));
}
int n, A[N], k, dis[N][30];
void bfs() {
for(int i = 0; i <= n; i++) {
for(int power = 0; power < 30; power++) {
dis[i][power] = mod;
}
}
dis[0][0] = 0;
queue<pair<int, int> > q;
q.push({0, 0});
while((int) q.size() > 0) {
int center = q.front().first, power = q.front().second;
q.pop();
// power += 1;
if(power + 1 < 30 && dis[center][power + 1] > dis[center][power] + 1) {
dis[center][power + 1] = dis[center][power] + 1;
q.push({center, power + 1});
}
// power -= 1;
if(power - 1 >= 0 && dis[center][power - 1] > dis[center][power] + 1) {
dis[center][power - 1] = dis[center][power] + 1;
q.push({center, power - 1});
}
// Recenter
int rem = k;
vector<pair<int, int> > newCenter;
int curVal = A[center];
int L = lower_bound(a.begin(), a.end(), make_pair(curVal - (1 << power), -5)) - a.begin();
int R = upper_bound(a.begin(), a.end(), make_pair(curVal + (1 << power), mod)) - a.begin();
R -= 1;
while(rem--) {
auto p = get(1, 0, n, L, R);
if(p.first == -1) break;
newCenter.push_back(p);
update(1, 0, n, p.second, -1);
}
for(auto i: newCenter) {
// update new Center
int Recenter = i.first;
if(dis[Recenter][power] > dis[center][power] + 1) {
dis[Recenter][power] = dis[center][power] + 1;
q.push({Recenter, power});
}
update(1, 0, n, i.second, i.first);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
a.resize(n);
for(int i = 0; i < n; i++) {
cin >> a[i].first;
A[i + 1] = a[i].first;
a[i].second = i + 1;
}
A[0] = 0;
a.push_back({0, 0});
sort(a.begin(), a.end());
build(1, 0, n);
bfs();
for(int i = 1; i <= n; i++) {
int ans = mod;
for(int power = 0; power < 30; power++) {
ans = min(ans, dis[i][power]);
}
if(ans == mod) ans = -1;
cout << ans << '\n';
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 5620kb
input:
4 2 -2 -1 -3 2
output:
2 1 3 2
result:
wrong answer 1st lines differ - expected: '3', found: '2'