結果
問題 |
No.3244 Range Multiple of 8 Query
|
ユーザー |
|
提出日時 | 2025-08-22 23:35:20 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3,490 ms / 5,000 ms |
コード長 | 5,202 bytes |
コンパイル時間 | 3,709 ms |
コンパイル使用メモリ | 295,120 KB |
実行使用メモリ | 27,100 KB |
最終ジャッジ日時 | 2025-08-22 23:37:20 |
合計ジャッジ時間 | 68,318 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 40 |
ソースコード
#include<bits/stdc++.h> #if __has_include(<atcoder/all>) #endif using namespace std; #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll IJK=n;IJK-->0;) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step) #define of(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(1,__VA_ARGS__);i>=i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) #define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);} using ll=long long; template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} template<class T>constexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} auto encode_integer(char c){return c-'0';} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} template<class T>constexpr ll maxsize10(){return size10(numeric_limits<T>::max());} bool amin(auto&a,const auto&b){return b<a?a=b,1:0;} auto max(auto...a){return max(initializer_list<common_type_t<decltype(a)...>>{a...});} template<class A,class B=A>struct pair{ A a;B b; pair()=default; pair(A aa,B bb):a(aa),b(bb){} auto operator<=>(const pair&)const=default; }; template<bool is_negative=false>struct infinity{ template<integral T>static constexpr T ones(size_t n){return n?ones<T>(n-1)*10+1:0;} template<integral T>constexpr operator T()const{static constexpr T v=ones<T>(maxsize10<T>())*(1-is_negative*2);return v;} template<class T>constexpr bool operator==(const T&x)const{return static_cast<T>(*this)==x;} }; constexpr infinity oo; template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>; } namespace my{ template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>; template<class V>constexpr int depth=0; template<vectorial V>constexpr int depth<V> =depth<typename V::value_type>+1; template<class T>struct core_t_helper{using type=T;}; template<vectorial V>struct core_t_helper<V>{using type=typename core_t_helper<typename V::value_type>::type;}; template<class T>using core_t=core_t_helper<T>::type; template<class V>struct vec; template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;}; template<class T>struct hvec_helper<0,T>{using type=T;}; template<int D,class T>using hvec=hvec_helper<D,T>::type; template<class V>struct vec:vector<V>{ static constexpr int D=depth<vec<V>>; using C=core_t<V>; using vector<V>::vector; vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);} void resizes(const auto&...a){if constexpr(sizeof...(a)==D){ }else*this=make(a...);} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]); else return vec<decltype(make(a...))>(n,make(a...)); } auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;} auto fold(const auto&f)const{ pair<C,bool>r{}; fe(*this,e){ if constexpr(!vectorial<V>){ if(r.b)f(r.a,e); else r={e,1}; }else { } } return r; } auto min()const{return fold([](auto&a,auto b){if(b<a)a=b;}).a;} auto rev()const{vec v=*this;ranges::reverse(v);return v;} }; template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>; auto sin(){string s;lin(s);return s;} template<class T=ll>auto sinen_integer(){vec<T>r;fe(sin(),e)r.eb(encode_integer(e));return r;} } namespace my{ auto decimal(ll x,ll L=-1){if(L==-1)L=size10(x);vec<ll>r;while(x)r.eb(x%10),x/=10;r.resize(L);return r;} } namespace my{entry void main(){ LL(N,Q); auto s=sinen_integer(); vec<vec<ll>>v; fo(i,1000)if(i%8==0)v.eb(decimal(i,3).rev()); ll A=10; vec pre(N+1,A,-1); // pre[i][x]:iより前で最後にxが現れた位置 fo(i,1,N+1){ fo(x,A)pre[i][x]=pre[i-1][x]; pre[i][s[i-1]]=i-1; } FO(Q){ LL(l,r);--l; if(r-l==1){ pp(s[l]%8==0?0:-1); continue; }else if(r-l==2){ if((s[l]*10+s[l+1])%8==0){ pp(0); continue; }else if((s[l+1]*10+s[l])%8==0){ pp(1); continue; }else{ pp(-1); continue; } } ll res=oo; fe(v,tar){ vec<ll>cnt(A); vec<ll>pos(3,-1); of(i,3){ ll cu=pre[r][tar[i]]; FO(cnt[tar[i]]){ if(cu==-1)break; cu=pre[cu][tar[i]]; } pos[i]=cu; cnt[tar[i]]++; } if(pos.min()<l)continue; ll tmp=0; // pos[i]をr-3+i (i=0,1,2)の位置に移動させるコスト. tmp+=max((r-1)-pos[2],0); if(pos[2]<pos[0])pos[0]--; if(pos[2]<pos[1])pos[1]--; tmp+=max((r-2)-pos[1],0); if(pos[1]<pos[0])pos[0]--; tmp+=max((r-3)-pos[0],0); amin(res,tmp); } pp(res==oo?-1:res); } }}