結果
| 問題 |
No.2690 A present from B (Hard)
|
| ユーザー |
Nachia
|
| 提出日時 | 2024-03-17 23:24:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 178 ms / 2,000 ms |
| コード長 | 5,846 bytes |
| コンパイル時間 | 1,054 ms |
| コンパイル使用メモリ | 103,284 KB |
| 最終ジャッジ日時 | 2025-02-20 07:05:17 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
#include <cmath>
#include <atcoder/modint>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(i64 i=0; i<(i64)(n); i++)
#define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--)
const i64 INF = 1001001001001001001;
const char* yn(bool x){ return x ? "Yes" : "No"; }
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
template<typename A> using nega_queue = std::priority_queue<A,std::vector<A>,std::greater<A>>;
using Modint = atcoder::static_modint<998244353>;
// #include "nachia/vec.hpp"
using namespace std;
namespace nachia{
template<
class S,
class F,
S op(S l, S r),
F composition(F f, F x),
S mapping(F f, S x)
>
struct LazySegtree {
private:
struct Node { S s; F f; bool propagated; };
int N;
int logN;
int xN;
std::vector<Node> A;
void mapf(Node& a, F f){
a.propagated = false;
a.f = composition(f, a.f);
a.s = mapping(f, a.s);
}
void mergev(int i){
if(i<N) A[i].s = op(A[i*2].s, A[i*2+1].s);
}
void spread(int i){
if(A[i].propagated || !(i < N)) return;
mapf(A[i*2], A[i].f);
mapf(A[i*2+1], A[i].f);
A[i].f = A[0].f;
A[i].propagated = true;
}
// bool cmp(S)
template<class E>
int minLeft2(int r, E cmp, int a = 0, int b = 0, int i = -1){
static S x;
if(i == -1){ a=0; b=N; i=1; x = A[0].s; }
if(r <= a) return a;
if(b <= r){
S nx = op(A[i].s, x);
if(cmp(nx)){ x = nx; return a; }
}
if(b-a == 1) return b;
spread(i);
int q = minLeft2(r, cmp, (a+b)/2, b, i*2+1);
if(q > (a+b)/2) return q;
return minLeft2(r, cmp, a, (a+b)/2, i*2);
}
// bool cmp(S)
template<class E>
int maxRight2(int l, E cmp, int a = 0, int b = 0, int i = -1){
static S x;
if(i == -1){ a=0; b=N; i=1; x = A[0].s; }
if(b <= l) return b;
if(l <= a){
S nx = op(x, A[i].s);
if(cmp(nx)){ x = nx; return b; }
}
if(b - a == 1) return a;
spread(i);
int q = maxRight2(l, cmp, a, (a+b)/2, i*2);
if(q < (a+b)/2) return q;
return maxRight2(l, cmp, (a+b)/2, b, i*2+1);
}
public:
LazySegtree() : N(0), logN(-1), xN(0){}
LazySegtree(int n, S e, F id){
N=1; logN=0; xN=n;
while(N<n){ N *= 2; logN++; }
A.assign(N*2, { e, id, true });
}
LazySegtree(const std::vector<S>& a, S e, F id)
: LazySegtree(a.size(), std::move(e), std::move(id)){
for(std::size_t i=0; i<a.size(); i++) A[i+N].s = a[i];
for(int i=N-1; i>=1; i--) mergev(i);
}
void set(int p, S x){
p += N;
for(int d=logN; d; d--) spread(p >> d);
A[p].s = x;
for(int d=1; d<=logN; d++) mergev(p >> d);
}
S get(int p){
p += N;
for(int d=logN; d; d--) spread(p >> d);
return A[p].s;
}
void apply(int p, F f){ set(p, mapping(f, get(p))); }
void apply(int l, int r, F f){
if(!(l < r)) return;
if(l == 0 && r == N){ mapf(A[1], f); return; }
l += N; r += N;
for(int d=logN; d; d--){
if((l >> d) << d != l) spread(l >> d);
if((r >> d) << d != r) spread(r >> d);
}
int lp = l, rp = r;
while(l < r){
if(l&1){ mapf(A[l++], f); } l /= 2;
if(r&1){ mapf(A[--r], f); } r /= 2;
}
for(int d=1 ; d<=logN; d++){
if((lp >> d) << d != lp) mergev(lp >> d);
if((rp >> d) << d != rp) mergev(rp >> d);
}
}
S prod(int l, int r){
if(!(l < r)) return A[0].s;
l += N; r += N;
for(int d=logN; d; d--){
if((l >> d) << d != l) spread(l >> d);
if((r >> d) << d != r) spread(r >> d);
}
S q1 = A[0].s, q2 = A[0].s;
while(l < r){
if(l&1){ q1 = op(q1, A[l++].s); } l /= 2;
if(r&1){ q2 = op(A[--r].s, q2); } r /= 2;
}
return op(q1, q2);
}
S allProd() const { return A[1].s; }
// bool cmp(S)
template<class E>
int minLeft(int r, E cmp){
return minLeft2(r, cmp);
}
// bool cmp(S)
template<class E>
int maxRight(int l, E cmp){
int x = maxRight2(l, cmp);
return x > xN ? xN : x;
}
};
} // namespace nachia;
i64 lst_chmin(i64 f, i64 x){ return min(f, x); }
using RangeChminPointGet = nachia::LazySegtree<i64, i64, lst_chmin, lst_chmin, lst_chmin>;
void testcase(){
int N, M; cin >> N >> M;
RangeChminPointGet ds_ru(N, INF, INF);
RangeChminPointGet ds_lu(N, INF, INF);
auto getVal = [&](i64 i){ return min(ds_ru.get(i) + i, ds_lu.get(i) - i); };
auto applyVal = [&](i64 i, i64 x){
ds_ru.apply(i, N, x-i);
ds_lu.apply(0, i+1, x+i);
};
vector<int> A(M); rep(i,M){ cin >> A[i]; A[i]--; }
reverse(A.begin(), A.end());
applyVal(0, 0);
for(int a : A){
auto qa = getVal(a);
auto qb = getVal(a+1);
ds_ru.set(a, INF); ds_lu.set(a, INF);
ds_ru.set(a+1, INF); ds_lu.set(a+1, INF);
if(a != 0) chmin(qb, getVal(a-1) + 1);
if(a+2 < N) chmin(qa, getVal(a+2) + 1);
applyVal(a, qb);
applyVal(a+1, qa);
}
rep(i,N-1){
if(i) cout << ' ';
cout << getVal(i+1);
} cout << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
#ifdef NACHIA
int T; cin >> T; for(int t=0; t<T; T!=++t?(cout<<'\n'),0:0)
#endif
testcase();
return 0;
}
Nachia