結果
問題 | No.2704 L to R Graph |
ユーザー | hamamu |
提出日時 | 2024-03-26 21:14:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 756 ms / 3,000 ms |
コード長 | 53,908 bytes |
コンパイル時間 | 6,716 ms |
コンパイル使用メモリ | 320,976 KB |
実行使用メモリ | 42,616 KB |
最終ジャッジ日時 | 2024-09-30 14:25:57 |
合計ジャッジ時間 | 14,136 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 3 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 162 ms
39,284 KB |
testcase_09 | AC | 178 ms
39,292 KB |
testcase_10 | AC | 173 ms
39,288 KB |
testcase_11 | AC | 156 ms
42,360 KB |
testcase_12 | AC | 156 ms
42,488 KB |
testcase_13 | AC | 158 ms
42,492 KB |
testcase_14 | AC | 155 ms
42,364 KB |
testcase_15 | AC | 156 ms
42,360 KB |
testcase_16 | AC | 156 ms
42,488 KB |
testcase_17 | AC | 156 ms
42,364 KB |
testcase_18 | AC | 156 ms
42,360 KB |
testcase_19 | AC | 156 ms
42,488 KB |
testcase_20 | AC | 160 ms
42,360 KB |
testcase_21 | AC | 148 ms
42,484 KB |
testcase_22 | AC | 146 ms
42,356 KB |
testcase_23 | AC | 142 ms
42,488 KB |
testcase_24 | AC | 143 ms
42,488 KB |
testcase_25 | AC | 161 ms
42,616 KB |
testcase_26 | AC | 166 ms
42,488 KB |
testcase_27 | AC | 726 ms
39,892 KB |
testcase_28 | AC | 756 ms
39,796 KB |
ソースコード
#if !defined(MYLOCAL)//提出時用テンプレート #pragma GCC optimize("Ofast") #if defined(NDEBUG) #undef NDEBUG #endif #include "bits/stdc++.h" #if !defined(_MSC_VER) && __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; using ll=long long; using dd=double; using vll=vector< ll>; using vdd=vector< dd>; using vvll=vector< vll>; using vvdd=vector<vdd>; using vvvll=vector< vvll>; using vvvdd=vector<vvdd>; using vvvvll=vector<vvvll>; using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; using qll=tuple<ll,ll,ll,ll>; using vpll=vector< pll>; using vtll=vector< tll>; using vqll=vector< qll>; using vvpll=vector<vpll>; using vvtll=vector<vtll>; using vvqll=vector<vqll>; using namespace chrono; constexpr ll INF = 1001001001001001001; struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(numeric_limits<double>::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, (N)-1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, (N)-1) #define EXPAND( x ) x//VS用おまじない #define overload3(_1,_2,_3,name,...) name #define overload4(_1,_2,_3,_4,name,...) name #define overload5(_1,_2,_3,_4,_5,name,...) name #define rep3(i, S, E) for (ll i = (S); i <= (E); i++) #define rep4(i, S, E, t) for (ll i = (S); i <= (E); i+=(t)) #define rep(...) EXPAND(overload4(__VA_ARGS__,rep4,rep3,_,_)(__VA_ARGS__)) #define dep3(i, E, S) for (ll i = (E); i >= (S); i--) #define dep4(i, E, S, t) for (ll i = (E); i >= (S); i-=(t)) #define dep(...) EXPAND(overload4(__VA_ARGS__, dep4, dep3,_,_)(__VA_ARGS__)) #define each2(e,v) for (auto && e:v) #define each3(a,b,v) for (auto &&[a,b]:v) #define each4(a,b,c,v) for (auto &&[a,b,c]:v) #define each5(a,b,c,d,v) for (auto &&[a,b,c,d]:v) #define each(...) EXPAND(overload5(__VA_ARGS__,each5,each4,each3,each2,_)(__VA_ARGS__)) #define ALL1(v) (v).begin(), (v).end() #define ALL2(v,E) (v).begin(), (v).begin()+((E)+1) #define ALL3(v,S,E) (v).begin()+(S), (v).begin()+((E)+1) #define ALL(...) EXPAND(overload3(__VA_ARGS__, ALL3, ALL2, ALL1)(__VA_ARGS__)) #define all ALL #define RALL1(v) (v).rbegin(), (v).rend() #define RALL2(v,E) (v).rbegin(), (v).rbegin()+((E)+1) #define RALL3(v,S,E) (v).rbegin()+(S), (v).rbegin()+((E)+1) #define RALL(...) EXPAND(overload3(__VA_ARGS__, RALL3, RALL2, RALL1)(__VA_ARGS__)) #define rall RALL template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; } template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; } template<class T> inline T MaxE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmax(m,v[i]); return m; } template<class T> inline T MinE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmin(m,v[i]); return m; } template<class T> inline T MaxE(vector<T> &v) { return MaxE(v,0,(ll)v.size()-1); } template<class T> inline T MinE(vector<T> &v) { return MinE(v,0,(ll)v.size()-1); } template<class T> inline auto maxe(T &&v,ll S,ll E){ return *max_element(ALL(v,S,E)); } template<class T> inline auto maxe(T &&v){ return *max_element(ALL(v)); } template<class T> inline auto mine(T &&v,ll S,ll E){ return *min_element(ALL(v,S,E)); } template<class T> inline auto mine(T &&v){ return *min_element(ALL(v)); } template<class T> inline T Sum(vector<T> &v,ll S,ll E){ T s=T(); rep(i,S,E)s+=v[i]; return s; } template<class T> inline T Sum(vector<T> &v) { return Sum(v,0,v.size()-1); } template<class T,class U=typename remove_reference<T>::type::value_type> inline U sum(T &&v,ll S,ll E) {return accumulate(all(v,S,E),U());} template<class T> inline auto sum(T &&v) {return sum(v,0,v.end()-v.begin()-1);} template<class T> inline ll sz(T &&v){ return (ll)v.size(); } inline ll CEIL(ll a,ll b){ return (a<0) ? -(-a/b) : (a+b-1)/b; } //負もOK inline ll FLOOR(ll a,ll b){ return -CEIL(-a,b); } //負もOK //pair用テンプレート template<class T,class S> inline pair<T,S>& operator+=(pair<T,S> &a,const pair<T,S> &b){ a.first+=b.first; a.second+=b.second; return a; } template<class T,class S> inline pair<T,S>& operator-=(pair<T,S> &a,const pair<T,S> &b){ a.first-=b.first; a.second-=b.second; return a; } template<class T,class S> inline pair<T,S>& operator*=(pair<T,S> &a,const pair<T,S> &b){ a.first*=b.first; a.second*=b.second; return a; } template<class T,class S> inline pair<T,S>& operator/=(pair<T,S> &a,const pair<T,S> &b){ a.first/=b.first; a.second/=b.second; return a; } template<class T,class S> inline pair<T,S>& operator%=(pair<T,S> &a,const pair<T,S> &b){ a.first%=b.first; a.second%=b.second; return a; } template<class T,class S,class R> inline pair<T,S>& operator+=(pair<T,S> &a,R b){ a.first+=b; a.second+=b; return a; } template<class T,class S,class R> inline pair<T,S>& operator-=(pair<T,S> &a,R b){ a.first-=b; a.second-=b; return a; } template<class T,class S,class R> inline pair<T,S>& operator*=(pair<T,S> &a,R b){ a.first*=b; a.second*=b; return a; } template<class T,class S,class R> inline pair<T,S>& operator/=(pair<T,S> &a,R b){ a.first/=b; a.second/=b; return a; } template<class T,class S,class R> inline pair<T,S>& operator%=(pair<T,S> &a,R b){ a.first%=b; a.second%=b; return a; } template<class T,class S,class R> inline pair<T,S> operator+(const pair<T,S> &a,R b){ pair<T,S> c=a; return c+=b; } template<class T,class S,class R> inline pair<T,S> operator-(const pair<T,S> &a,R b){ pair<T,S> c=a; return c-=b; } template<class T,class S,class R> inline pair<T,S> operator*(const pair<T,S> &a,R b){ pair<T,S> c=a; return c*=b; } template<class T,class S,class R> inline pair<T,S> operator/(const pair<T,S> &a,R b){ pair<T,S> c=a; return c/=b; } template<class T,class S,class R> inline pair<T,S> operator%(const pair<T,S> &a,R b){ pair<T,S> c=a; return c%=b; } template<class T,class S,class R> inline pair<T,S> operator-(R b,const pair<T,S> &a){ pair<T,S> c=-a; return c+=b; } template<class T,class S> inline pair<T,S> operator-(const pair<T,S> &a,const pair<T,S> &b){ pair<T,S> c=a; return c-=b; } template<class T,class S> inline pair<T,S> operator-(const pair<T,S> &a){ pair<T,S> c=a; return c*=(-1); } template<class T,class S> inline ostream &operator<<(ostream &os,const pair<T,S> &a){ return os << a.first << ' ' << a.second; } //tuple用テンプレート 出力用のみ template<class T,class S,class R> inline ostream &operator<<(ostream &os,const tuple<T,S,R> &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a); } template<class T,class S,class R,class Q> inline ostream &operator<<(ostream &os,const tuple<T,S,R,Q> &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a) << ' ' << get<3>(a); } //vector用テンプレート template<class T> inline ostream &operator<<(ostream &os,const vector<T> &a){ for (ll i=0; i<(ll)a.size(); i++) os<<(i>0?" ":"")<<a[i]; return os; } //array用テンプレート template<class T,size_t S> inline array<T,S>& operator+=(array<T,S> &a,const array<T,S> &b){ for (ll i=0; i<(ll)S; i++) a[i]+=b[i]; return a; } template<class T,size_t S> inline array<T,S>& operator-=(array<T,S> &a,const array<T,S> &b){ for (ll i=0; i<(ll)S; i++) a[i]-=b[i]; return a; } template<class T,size_t S> inline array<T,S>& operator*=(array<T,S> &a,const array<T,S> &b){ for (ll i=0; i<(ll)S; i++) a[i]*=b[i]; return a; } template<class T,size_t S> inline array<T,S>& operator/=(array<T,S> &a,const array<T,S> &b){ for (ll i=0; i<(ll)S; i++) a[i]/=b[i]; return a; } template<class T,size_t S> inline array<T,S>& operator%=(array<T,S> &a,const array<T,S> &b){ for (ll i=0; i<(ll)S; i++) a[i]%=b[i]; return a; } template<class T,size_t S,class R> inline array<T,S>& operator+=(array<T,S> &a,R b){ for (T &e: a) e+=b; return a; } template<class T,size_t S,class R> inline array<T,S>& operator-=(array<T,S> &a,R b){ for (T &e: a) e-=b; return a; } template<class T,size_t S,class R> inline array<T,S>& operator*=(array<T,S> &a,R b){ for (T &e: a) e*=b; return a; } template<class T,size_t S,class R> inline array<T,S>& operator/=(array<T,S> &a,R b){ for (T &e: a) e/=b; return a; } template<class T,size_t S,class R> inline array<T,S>& operator%=(array<T,S> &a,R b){ for (T &e: a) e%=b; return a; } template<class T,size_t S,class R> inline array<T,S> operator+(const array<T,S> &a,R b){ array<T,S> c=a; return c+=b; } template<class T,size_t S,class R> inline array<T,S> operator-(const array<T,S> &a,R b){ array<T,S> c=a; return c-=b; } template<class T,size_t S,class R> inline array<T,S> operator*(const array<T,S> &a,R b){ array<T,S> c=a; return c*=b; } template<class T,size_t S,class R> inline array<T,S> operator/(const array<T,S> &a,R b){ array<T,S> c=a; return c/=b; } template<class T,size_t S,class R> inline array<T,S> operator%(const array<T,S> &a,R b){ array<T,S> c=a; return c%=b; } template<class T,size_t S,class R> inline array<T,S> operator-(R b,const array<T,S> &a){ array<T,S> c=-a; return c+=b; } template<class T,size_t S> inline array<T,S> operator-(const array<T,S> &a,const array<T,S> &b){ array<T,S> c=a; return c-=b; } template<class T,size_t S> inline array<T,S> operator-(const array<T,S> &a){ array<T,S> c=a; return c*=(-1); } template<class T,size_t S> inline ostream &operator<<(ostream &os,const array<T,S> &a){ for (ll i=0; i<(ll)S; i++) os<<(i>0?" ":"")<<a[i]; return os; } template<class T,size_t S,class R> struct view1d; template<class T,size_t S,class R> struct view1dIter{ view1d<T,S,R> *vw=nullptr; ll idx=INF; view1dIter(){} view1dIter(view1d<T,S,R> *vw_,ll idx_):vw(vw_),idx(idx_){} view1dIter(const view1dIter<T,S,R> &it):vw(it.vw),idx(it.idx){} R &operator*() { return (*vw)[idx]; } R &operator*() const{ return (*vw)[idx]; } R &operator[](ll i) { return (*vw)[idx+i]; } R &operator[](ll i)const{ return (*vw)[idx+i]; } auto &operator++(){ idx++; return *this; } auto &operator--(){ idx--; return *this; } auto operator++(int){ auto it=*this; idx++; return it; } auto operator--(int){ auto it=*this; idx--; return it; } auto &operator+=(ll n){ idx+=n; return *this; } auto &operator-=(ll n){ idx-=n; return *this; } auto operator+(ll n){ auto it=*this; return it+=n; } auto operator-(ll n){ auto it=*this; return it-=n; } ll operator- (const view1dIter<T,S,R> &it)const{ return idx- it.idx; } bool operator< (const view1dIter<T,S,R> &it)const{ return idx< it.idx; } bool operator> (const view1dIter<T,S,R> &it)const{ return idx> it.idx; } bool operator<=(const view1dIter<T,S,R> &it)const{ return idx<=it.idx; } bool operator>=(const view1dIter<T,S,R> &it)const{ return idx>=it.idx; } bool operator!=(const view1dIter<T,S,R> &it)const{ return idx!=it.idx; } bool operator==(const view1dIter<T,S,R> &it)const{ return idx==it.idx; } using iterator_category = random_access_iterator_tag; using value_type = R; using difference_type = ll; using pointer = R*; using reference = R&; }; template<class T,size_t S,class R> struct view1d{ using Sll=array<ll,S>; T &data; //参照先データ Sll dsize; //参照先データ各軸のサイズ Sll s=Sll(); //view先頭 Sll d=Sll(); //view方向 ll len; //view長さ R &dummy; //範囲外の値参照 R dummyj=R(); //範囲外の値実体 /*---- コンストラクタ ----*/ view1d(T &v): data(v),dummy(dummyj){ SetDsize(v,dsize); d[S-1]=1; len=dsize[S-1]; } //このコンストラクタは、マイナス座標指定を末尾にしない(暫定) //代入時Array<ll,2>{...}等と書くため、使い勝手悪い。将来廃止するかも。 view1d(T &v,Sll s,Sll e,Sll d): data(v),s(s),d(d),len(Len(s,e,d)),dummy(dummyj){ SetDsize(v,dsize); } view1d(T &v,Sll s,Sll d,ll len,R &dmy_): data(v),s(s),d(d),len(len),dummy(dmy_){ SetDsize(v,dsize); } /*---- 演算 ----*/ template<class Q> auto &operator =(const Q &b){ rep(i,0,len-1) (*this)[i] =b; return *this; } template<class Q> auto &operator+=(const Q &b){ rep(i,0,len-1) (*this)[i]+=b; return *this; } template<class Q> auto &operator-=(const Q &b){ rep(i,0,len-1) (*this)[i]-=b; return *this; } template<class Q> auto &operator*=(const Q &b){ rep(i,0,len-1) (*this)[i]*=b; return *this; } template<class Q> auto &operator/=(const Q &b){ rep(i,0,len-1) (*this)[i]/=b; return *this; } template<class Q> auto &operator%=(const Q &b){ rep(i,0,len-1) (*this)[i]%=b; return *this; } auto &operator =(const string &b){ return CpSeq(b); } template<size_t Q> auto &operator =(const char (&b)[Q]){ return *this=string(b); } template<class Q> auto &operator =(const vector<Q> &b){ return CpSeq(b); } template<class Q> auto &operator+=(const vector<Q> &b){ return PlSeq(b); } template<class Q> auto &operator-=(const vector<Q> &b){ return MnSeq(b); } template<class Q> auto &operator*=(const vector<Q> &b){ return PrSeq(b); } template<class Q> auto &operator/=(const vector<Q> &b){ return DvSeq(b); } template<class Q> auto &operator%=(const vector<Q> &b){ return RmSeq(b); } template<class Q,size_t P,class O> auto &operator =(const view1d<Q,P,O> &b){ return CpSeq(b); } template<class Q,size_t P,class O> auto &operator+=(const view1d<Q,P,O> &b){ return PlSeq(b); } template<class Q,size_t P,class O> auto &operator-=(const view1d<Q,P,O> &b){ return MnSeq(b); } template<class Q,size_t P,class O> auto &operator*=(const view1d<Q,P,O> &b){ return PrSeq(b); } template<class Q,size_t P,class O> auto &operator/=(const view1d<Q,P,O> &b){ return DvSeq(b); } template<class Q,size_t P,class O> auto &operator%=(const view1d<Q,P,O> &b){ return RmSeq(b); } template<class Q> auto &CpSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i] =b[i]; return *this; } template<class Q> auto &PlSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i]+=b[i]; return *this; } template<class Q> auto &MnSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i]-=b[i]; return *this; } template<class Q> auto &PrSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i]*=b[i]; return *this; } template<class Q> auto &DvSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i]/=b[i]; return *this; } template<class Q> auto &RmSeq(const Q &b){ rep(i,0,min(sz(b),len)-1) (*this)[i]%=b[i]; return *this; } //template<class Q,class P> static bool eq(const Q &a,const P &b){ // return equals(ALL(a),ALL(b)); //} template<class Q,class P> static bool eq(const Q &a,const P &b){ if ((ll)a.size()!=(ll)b.size()) return false; rep(i,0,(ll)a.size()-1){ if (a[i]!=b[i]) return false; } return true; } template<class Q,class P> static bool lt(const Q &a,const P &b){ ll n=min((ll)a.size(),(ll)b.size()); rep(i,0,n-1){ if (a[i]<b[i])return true; if (a[i]>b[i])return false; } return (ll)a.size()<(ll)b.size(); } template<class Q,size_t P,class O> bool operator==( const view1d<Q,P,O> &b){ return eq(*this,b); } template<class Q> bool operator==( const Q &b){ return eq(*this,b); } template<class Q> friend bool operator==(const Q &a,const view1d<T,S,R> &b){ return eq( a,b); } template<size_t Q> bool operator==( const char (&b)[Q]){ return eq(*this,string(b)); } template<class Q,size_t P,class O> bool operator!=( const view1d<Q,P,O> &b){ return !(*this==b); } template<class Q> bool operator!=( const Q &b){ return !(*this==b); } template<class Q> friend bool operator!=(const Q &a,const view1d<T,S,R> &b){ return !( a==b); } template<class Q,size_t P,class O> bool operator< ( const view1d<Q,P,O> &b){ return lt(*this,b); } template<class Q> bool operator< ( const Q &b){ return lt(*this,b); } template<class Q> friend bool operator< (const Q &a,const view1d<T,S,R> &b){ return lt( a,b); } template<class Q,size_t P,class O> bool operator> ( const view1d<Q,P,O> &b){ return lt(b,*this); } template<class Q> bool operator> ( const Q &b){ return lt(b,*this); } template<class Q> friend bool operator> (const Q &a,const view1d<T,S,R> &b){ return lt(b,a ); } template<class Q,size_t P,class O> bool operator<=( const view1d<Q,P,O> &b){ return !(*this>b); } template<class Q> bool operator<=( const Q &b){ return !(*this>b); } template<class Q> friend bool operator<=(const Q &a,const view1d<T,S,R> &b){ return !( a>b); } template<class Q,size_t P,class O> bool operator>=( const view1d<Q,P,O> &b){ return !(*this<b); } template<class Q> bool operator>=( const Q &b){ return !(*this<b); } template<class Q> friend bool operator>=(const Q &a,const view1d<T,S,R> &b){ return !( a<b); } /*---- getter ----*/ ll size() const { return len; } R &operator[](ll i){ return const_cast<R&>(OrgAt(s+d*i)); } const R &operator[](ll i) const { return OrgAt(s+d*i); } R &at(ll i){ if(i<0)i+=len; return (*this)[i]; } //vector<R> tov(){ vector<R> vvv(len); rep(i,0,len-1) vvv[i]=(*this)[i]; return vvv; } operator vector<R>(){ vector<R> v(len); rep(i,0,len-1) v[i]=(*this)[i]; return v; } bool contains(R a){ rep(i,0,len-1) if((*this)[i]==a)return true; return false; } auto begin(){ return view1dIter<T,S,R>(this,0); } auto end(){ return view1dIter<T,S,R>(this,len); } /*---- view設定 ----*/ view1d<T,S,R> &dmy(R dmy_) { dummy=dmy_; return *this; }//ダミー値セット template<class...Q> view1d<T,S,R> &st(Q... s_){//始点set 負は末尾からの位置 this->s=RevIfNeg(SllD(s_...)); this->len=AutoLen(this->s,this->d,this->dsize); return *this; } template<class...Q> view1d<T,S,R> &en(Q... e_){//終了条件再設定 負は末尾から this->len=Len(s, RevIfNeg(SllD(e_...)), d); return *this; } template<class...Q> view1d<T,S,R> &dir(Q... d_){//方向set、長さはdata端まで this->d=SllD(d_...); this->len=AutoLen(this->s,this->d,this->dsize); return *this; } template<class...Q> view1d<T,S,R> &mv(Q... s_){ this->s+=SllD(s_...); return *this; }//平行移動 view1d<T,S,R> &size(ll len_) { len=len_; return *this; }//長さset template<class Q> view1d<T,S,R> &size(Q &v){ len=(ll)v.size(); return *this; }//長さset view1d<T,S,R> &rev() { s+=d*(len-1); d*=-1; return *this; }//反転 /*---- utility ----*/ template<class Q> inline static ll sz(Q &v){ return (ll)v.size(); } Sll RevIfNeg(Sll pos){//!< 負なら末尾からの位置に変換 rep(i,0,S-1) if (pos[i]<0) pos[i]+=dsize[i]; return pos; } static ll AutoLen(Sll s_,Sll d_,Sll dsz){//位置s_から方向d_ではみ出すまでの長さ Sll e=dsz-1; rep(i,0,S-1) e[i] *= (d_[i]>=0);//方向が負の軸を0にする return Len(s_,e,d_); } /*---- 先頭位置s、方向d、終了条件eから長さlen計算 ----*/ //template<size_t Q> static ll Len(array<ll,Q> s,array<ll,Q> e,array<ll,Q> d){ // ll ret=INF; // rep(i,0,Q-1) chmin(ret, Len1(s[i],e[i],d[i])); // return ret; //} static ll Len(Sll s,Sll e,Sll d){ ll ret=INF; rep(i,0,S-1) chmin(ret, Len1(s[i],e[i],d[i])); return ret; } static ll Len1(ll s,ll e,ll d){ if (d==0) return INF; if (d<0){ s=-s; e=-e; d=-d; } if (s>e) return 0; return (e-s)/d+1; } /*---- 可変長引数をSllに変換 ----*/ template<class...Q> static Sll SllD(Q... args){ return SllRec(0,args...); } template<class...Q> static Sll SllRec(ll i,ll first,Q... rest) { Sll sll = (i==S-1) ? Sll() : SllRec(i+1,rest...); sll[i]=first; return sll; } static Sll SllRec(ll i){ return Sll(); } /*---- dataの位置posの値取得 ----*/ const R &OrgAt(Sll pos) const { rep(i,0,S-1){ if (pos[i]<0 || dsize[i]<=pos[i]) return dummy; } return OrgAt_(data,pos); } template<class Q> using V = vector<Q>; template<class Q> using VV = V<V<Q>>; template<class Q> using VVV = V<V<V<Q>>>; using Vs = V<string>; using VVs = VV<string>; using ll1 = array<ll,1>; using ll2 = array<ll,2>; using ll3 = array<ll,3>; auto &OrgAt_(V<R> &dat,ll1 pos)const{ auto [i] =pos; return dat[i]; } auto &OrgAt_(string &dat,ll1 pos)const{ auto [i] =pos; return dat[i]; } auto &OrgAt_(VV<R> &dat,ll2 pos)const{ auto [i,j] =pos; return dat[i][j]; } auto &OrgAt_(Vs &dat,ll2 pos)const{ auto [i,j] =pos; return dat[i][j]; } auto &OrgAt_(VVV<R> &dat,ll3 pos)const{ auto [i,j,k]=pos; return dat[i][j][k]; } auto &OrgAt_(VVs &dat,ll3 pos)const{ auto [i,j,k]=pos; return dat[i][j][k]; } /*---- dataの各軸size取得 ----*/ static void SetDsize(V<R> &dat,ll1 &dsz){ dsz={sz(dat)}; } static void SetDsize(string &dat,ll1 &dsz){ dsz={sz(dat)}; } static void SetDsize(VV<R> &dat,ll2 &dsz){ dsz={sz(dat),sz(dat[0])}; } static void SetDsize(Vs &dat,ll2 &dsz){ dsz={sz(dat),sz(dat[0])}; } static void SetDsize(VVV<R> &dat,ll3 &dsz){ dsz={sz(dat),sz(dat[0]),sz(dat[0][0])}; } static void SetDsize(VVs &dat,ll3 &dsz){ dsz={sz(dat),sz(dat[0]),sz(dat[0][0])}; } typedef view1dIter<T,S,R> iterator; using value_type = R; }; template<class Q> using V = vector<Q>; template<class Q> using VV = V<V<Q>>; template<class Q> using VVV = V<V<V<Q>>>; template<class T> view1d( VVV<T> ) -> view1d< VVV<T>,3,T>; template<class T> view1d( VV<T> ) -> view1d< VV<T>,2,T>; template<class T> view1d( V<T> ) -> view1d< V<T>,1,T>; ; view1d(VV<string> ) -> view1d<VV<string>,3,char>; ; view1d( V<string> ) -> view1d< V<string>,2,char>; ; view1d( string ) -> view1d< string ,1,char>; template<class T,class S> view1d( VVV<T>,S,S,S) -> view1d< VVV<T>,3,T>; template<class T,class S> view1d( VV<T>,S,S,S) -> view1d< VV<T>,2,T>; template<class T,class S> view1d( V<T>,S,S,S) -> view1d< V<T>,1,T>; template<class S> view1d(VV<string>,S,S,S) -> view1d<VV<string>,3,char>; template<class S> view1d( V<string>,S,S,S) -> view1d< V<string>,2,char>; template<class S> view1d( string ,S,S,S) -> view1d< string ,1,char>; template<class VIEW2D> struct view2dIter{ VIEW2D *vw=nullptr; ll idx=INF; view2dIter(){} view2dIter(VIEW2D *vw_,ll idx_):vw(vw_),idx(idx_){} view2dIter(const view2dIter &it):vw(it.vw),idx(it.idx){} auto &operator*() { return (*vw)[x(idx)][y(idx)]; } auto &operator*() const{ return (*vw)[x(idx)][y(idx)]; } auto &operator[](ll i) { return (*vw)[x(idx+i)][y(idx+i)]; } auto &operator[](ll i)const{ return (*vw)[x(idx+i)][y(idx+i)]; } auto &operator++(){ idx++; return *this; } auto &operator--(){ idx--; return *this; } auto operator++(int){ auto it=*this; idx++; return it; } auto operator--(int){ auto it=*this; idx--; return it; } auto &operator+=(ll n){ idx+=n; return *this; } auto &operator-=(ll n){ idx-=n; return *this; } auto operator+(ll n){ auto it=*this; return it+=n; } auto operator-(ll n){ auto it=*this; return it-=n; } ll operator- (const view2dIter &it)const{ return idx- it.idx; } bool operator< (const view2dIter &it)const{ return idx< it.idx; } bool operator> (const view2dIter &it)const{ return idx> it.idx; } bool operator<=(const view2dIter &it)const{ return idx<=it.idx; } bool operator>=(const view2dIter &it)const{ return idx>=it.idx; } bool operator!=(const view2dIter &it)const{ return idx!=it.idx; } bool operator==(const view2dIter &it)const{ return idx==it.idx; } ll x(ll i)const{ return i/vw->leny(); } ll y(ll i)const{ return i%vw->leny(); } using iterator_category = random_access_iterator_tag; using value_type = typename VIEW2D::value_type; using difference_type = ll; using pointer = value_type*; using reference = value_type&; }; template<class T,size_t S,class R> struct view2d{ using Sll=array<ll,S>; T &data; //参照先データ Sll dsize; //参照先データ各軸のサイズ Sll s=Sll(); //view始点 Sll dx=Sll(); //view x軸方向 Sll dy=Sll(); //view y軸方向 ll xl; //view x軸長さ ll yl; //view y軸長さ R dummy=R(); //範囲外の値 /*---- コンストラクタ ----*/ view2d(T &v): data(v){ view1d<T,S,R>::SetDsize(v,dsize); dx[S-2]=1; dy[S-1]=1; xl=dsize[S-2]; yl=dsize[S-1]; } /*---- 演算 ----*/ //template<class Q> auto &operator =(const Q &b){ rep(i,0,xl-1) (*this)[i] =b; return *this; } //template<class Q,size_t P,class O> auto &operator+=(const view2d<Q,P,O> &b){ // rep(i,0,xl-1) (*this)[i] // return PlSeq(b); } /*---- getter ----*/ ll size() const { return xl; } array<ll,2> shape() const { return {xl,yl}; } ll lenx() const { return xl; } ll leny() const { return yl; } ///pll shape() const { return {xl,yl}; } view1d<T,S,R> operator[](ll i){ return view1d(data,s+dx*i,dy,yl,dummy); } const view1d<T,S,R> operator[](ll i) const { return view1d(data,s+dx*i,dy,yl,dummy); } R &at(ll i,ll j){ if(i<0)i+=xl; if(j<0)j+=yl; return (*this)[i][j]; } //vector<vector<R>> tovv(){ // vector<vector<R>> vvv(xl); // ///rep(i,0,xl-1) vvv[i]=(*this)[i].tov(); return vvv; // rep(i,0,xl-1) vvv[i]=(*this)[i]; return vvv; //} operator vector<vector<R>>(){ vector<vector<R>> v(xl); rep(i,0,xl-1) v[i]=(*this)[i]; return v; } auto begin(){ return view2dIter(this,0); } auto end(){ return view2dIter(this,xl*yl); } #if defined(_DEBUG) void dump(){ ::dump(tovv()); } #endif /*---- view設定 ----*/ view2d<T,S,R> &dmy(R dmy_) { dummy=dmy_; return *this; }//ダミー値セット template<class...Q> view2d<T,S,R> &st(Q... s_){//始点set 負は末尾からの位置 this->s=RevIfNeg(view1d<T,S,R>::SllD(s_...)); this->xl=view1d<T,S,R>::AutoLen(this->s,this->dx,this->dsize); this->yl=view1d<T,S,R>::AutoLen(this->s,this->dy,this->dsize); return *this; } template<class...Q> view2d<T,S,R> &dirx(Q... d_){//x軸set、長さはdata端まで this->dx=view1d<T,S,R>::SllD(d_...); this->xl=view1d<T,S,R>::AutoLen(this->s,this->dx,this->dsize); return *this; } template<class...Q> view2d<T,S,R> &diry(Q... d_){//y軸set、長さはdata端まで this->dy=view1d<T,S,R>::SllD(d_...); this->yl=view1d<T,S,R>::AutoLen(this->s,this->dy,this->dsize); return *this; } template<class...Q> view2d<T,S,R> &mv(Q... s_){//平行移動 this->s += view1d<T,S,R>::SllD(s_...); return *this; } view2d<T,S,R> &lenx(ll xl_) { xl=xl_; return *this; } view2d<T,S,R> &leny(ll yl_) { yl=yl_; return *this; } view2d<T,S,R> &shape(ll xl_,ll yl_) { xl=xl_; yl=yl_; return *this; } template<class Q> view2d<T,S,R> &shape(Q &v) { xl=v.lenx(); yl=v.leny(); return *this; } view2d<T,S,R> &rot90 () { s+=dx*(xl-1); swap(xl,yl); swap(dx,dy); dy*=-1; return *this; } view2d<T,S,R> &rot270() { s+=dy*(yl-1); swap(xl,yl); swap(dx,dy); dx*=-1; return *this; } view2d<T,S,R> &rot180() { s+=dx*(xl-1)+dy*(yl-1); dx*=-1; dy*=-1; return *this; } view2d<T,S,R> &revx() { s+=dx*(xl-1); dx*=-1; return *this; } view2d<T,S,R> &revy() { s+=dy*(yl-1); dy*=-1; return *this; } view2d<T,S,R> &swapxy() { swap(xl,yl); swap(dx,dy); return *this; }//xy軸入替 /*---- utility ----*/ Sll RevIfNeg(Sll pos){//!< 負なら末尾からの位置に変換 rep(i,0,S-1) if (pos[i]<0) pos[i]+=dsize[i]; return pos; } using value_type = R; }; template<class T> view2d( VVV<T>) -> view2d< VVV<T>,3,T>; template<class T> view2d( VV<T>) -> view2d< VV<T>,2,T>; ; view2d(VV<string>) -> view2d<VV<string>,3,char>; ; view2d( V<string>) -> view2d< V<string>,2,char>; /* zipは参照のpairを返す。それをさらに参照するのはまずいので、コピーする。 */ template<class ZIP> struct zipIter{ ZIP *z=nullptr; ll idx=INF; zipIter(){} zipIter(ZIP *z_,ll idx_):z(z_),idx(idx_){} zipIter(const zipIter<ZIP> &it):z(it.z),idx(it.idx){} auto operator*() { return (*z)[idx]; }//参照ではなく値戻し auto operator*() const{ return (*z)[idx]; }//同上 auto operator[](ll i) { return (*z)[idx+i]; }//同上 auto operator[](ll i)const{ return (*z)[idx+i]; }//同上 auto &operator++(){ idx++; return *this; } auto &operator--(){ idx--; return *this; } auto operator++(int){ auto it=*this; idx++; return it; } auto operator--(int){ auto it=*this; idx--; return it; } auto &operator+=(ll n){ idx+=n; return *this; } auto &operator-=(ll n){ idx-=n; return *this; } auto operator+(ll n){ auto it=*this; return it+=n; } auto operator-(ll n){ auto it=*this; return it-=n; } ll operator- (const zipIter<ZIP> &it)const{ return idx- it.idx; } bool operator< (const zipIter<ZIP> &it)const{ return idx< it.idx; } bool operator> (const zipIter<ZIP> &it)const{ return idx> it.idx; } bool operator<=(const zipIter<ZIP> &it)const{ return idx<=it.idx; } bool operator>=(const zipIter<ZIP> &it)const{ return idx>=it.idx; } bool operator!=(const zipIter<ZIP> &it)const{ return idx!=it.idx; } bool operator==(const zipIter<ZIP> &it)const{ return idx==it.idx; } using iterator_category = random_access_iterator_tag; using value_type = typename ZIP::value_type; using difference_type = ll; using pointer = value_type*; using reference = value_type&; }; template<class T,class S> struct zip2{ T &t; S &s; zip2(T &t_,S &s_):t(t_),s(s_){} ll size() const { return t.end()-t.begin(); } auto operator[](ll i) { return make_pair(ref(t.begin()[i]),ref(s.begin()[i])); } auto operator[](ll i)const{ return make_pair(ref(t.begin()[i]),ref(s.begin()[i])); } auto begin(){ return zipIter(this,0); } auto end(){ return zipIter(this,size()); } using value_type = pair<typename T::value_type,typename S::value_type>; }; template<class T,class S,class R> struct zip3{ T &t; S &s; R &r; zip3(T &t_,S &s_,R &r_):t(t_),s(s_),r(r_){} ll size() const { return t.end()-t.begin(); } auto operator[](ll i) { return make_tuple(ref(t.begin()[i]),ref(s.begin()[i]),ref(r.begin()[i])); } auto operator[](ll i)const{ return make_tuple(ref(t.begin()[i]),ref(s.begin()[i]),ref(r.begin()[i])); } auto begin(){ return zipIter(this,0); } auto end(){ return zipIter(this,size()); } using value_type = tuple<typename T::value_type,typename S::value_type,typename R::value_type>; }; #endif//テンプレートend #if defined(_MSC_VER) && __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif struct{ system_clock::time_point st = system_clock::now(); ll operator()()const{return duration_cast<microseconds>(system_clock::now()-st).count()/1000;} } timeget; ////////////////////////////////////////// struct fraction{ ll bunsi=0,bunbo=1; /*---- utilty ----*/ fraction &norm(){//正規化(約分,分母正) ll g=gcd(bunsi,bunbo); bunsi/=g,bunbo/=g; if (bunbo<0) bunsi*=-1,bunbo*=-1; return *this; } fraction &invsg(){//正負反転 bunsi*=-1; return *this; } /*---- I/F ----*/ fraction(){} fraction(ll bunsi_,ll bunbo_):bunsi(bunsi_),bunbo(bunbo_){ norm(); } fraction(ll b):bunsi(b){}//整数用コンストラクタ fraction(ll bunsi_,ll bunbo_,bool b)//正規化無のコンストラクタ :bunsi(bunsi_),bunbo(bunbo_){} fraction &operator+=(fraction b){ ll bo=bunbo*b.bunbo; ll si=bunsi*b.bunbo + b.bunsi*bunbo; bunsi=si,bunbo=bo; return norm(); } fraction &operator-=(fraction b){ return (*this)+=b.invsg(); } fraction &operator*=(fraction b){ bunsi*=b.bunsi; bunbo*=b.bunbo; return norm(); } fraction &operator/=(fraction b){ return (*this)*=b.inv(); } fraction &operator+=(ll b){ bunsi+=bunbo*b; return *this; } fraction &operator-=(ll b){ return (*this)+=-b; } fraction &operator*=(ll b){ bunsi*=b; return norm(); } fraction &operator/=(ll b){ bunbo*=b; return norm(); } fraction operator-()const{ return {-bunsi,bunbo,0}; } fraction operator+(const fraction &b)const{ return fraction(*this)+=b; } fraction operator-(const fraction &b)const{ return fraction(*this)-=b; } fraction operator*(const fraction &b)const{ return fraction(*this)*=b; } fraction operator/(const fraction &b)const{ return fraction(*this)/=b; } fraction operator+(ll b)const{ return fraction(*this)+=b; } fraction operator-(ll b)const{ return fraction(*this)-=b; } fraction operator*(ll b)const{ return fraction(*this)*=b; } fraction operator/(ll b)const{ return fraction(*this)/=b; } friend fraction operator+(ll a,const fraction &b){ return b+a; } friend fraction operator-(ll a,const fraction &b){ return -b+a; } friend fraction operator*(ll a,const fraction &b){ return b*a; } friend fraction operator/(ll a,const fraction &b){ return b.inv()*a; } bool operator==(const fraction &b)const{ return bunsi==b.bunsi && bunbo==b.bunbo; } bool operator!=(const fraction &b)const{ return !(*this==b); } bool operator<(const fraction &b)const{ return (b-*this).bunsi>0; } bool operator>(const fraction &b)const{ return b<*this; } bool operator>=(const fraction &b)const{ return !(*this<b); } bool operator<=(const fraction &b)const{ return b>=*this; } friend ostream &operator << (ostream &os,const fraction &a){ return os<<a.bunsi<<'/'<<a.bunbo; } fraction inv()const{ if (bunsi<0) return {-bunbo,-bunsi,0}; else return {bunbo, bunsi,0}; } }; /* - 定義 ↓b分のa (a/b) fraction x(a,b); . ↓整数a fraction x(a); . ↓正規化無(約分と分母正化なしで良い時) fraction x(a,b,true); fraction y=x.inv(); 逆数 x+y,x-y,x*y,x/y,-x, x+=y,x-=y,x*=y,x/=y, x==y,x!=y,x<y,x>y,x<=y,x>=y cout << x; //"a/b"と表示 */ template<long long MOD> struct mll_{ using Int = long long; using ll = long long; ll val_=0; /*---- utility ----*/ mll_ &norm(){ return normR().normS(); }//正規化 mll_ &normR(){ val_%=MOD; return *this; }//剰余正規化のみ mll_ &normS(){ if (val_<0) val_+=MOD; return *this; }//正負正規化のみ mll_ &normP(){ if (val_>=MOD) val_-=MOD; return *this; }//加算時正規化 mll_ &invsg(){ val_=-val_; return normS(); }//正負反転 ll modinv(int a){//a^-1 mod MOD int ypre=0,y=1,apre=MOD; while (a>1){ int t=apre/a; apre-=a*t,swap(a,apre); ypre-=y*t,swap(y,ypre); } return y<0 ? y+MOD: y; } /*---- I/F ----*/ mll_(){} mll_(ll v): val_(v){ norm(); } mll_(ll v,bool b): val_(v){} //正規化無のコンストラクタ Int val()const{ return (Int)val_; } bool isnone() const { return val_==-1; } //true:値なし mll_ &none() { val_=-1; return *this; } //値なしにする mll_ &inv(){ val_=modinv((int)val_); return *this; } mll_ &operator+=(mll_ b){ val_+=b.val_; return normP(); } mll_ &operator-=(mll_ b){ val_-=b.val_; return normS(); } mll_ &operator*=(mll_ b){ val_*=b.val_; return normR(); } mll_ &operator/=(mll_ b){ return *this*=b.inv(); } mll_ &operator+=(ll b){ return *this+=mll_(b); } mll_ &operator-=(ll b){ return *this-=mll_(b); } mll_ &operator*=(ll b){ return *this*=mll_(b); } mll_ &operator/=(ll b){ return *this/=mll_(b); } mll_ operator-()const{ return mll_(*this).invsg(); } mll_ operator+(mll_ b)const{ return mll_(*this)+=b; } mll_ operator-(mll_ b)const{ return mll_(*this)-=b; } mll_ operator*(mll_ b)const{ return mll_(*this)*=b; } mll_ operator/(mll_ b)const{ return mll_(*this)/=b; } mll_ operator+(ll b)const{ return mll_(*this)+=b; } mll_ operator-(ll b)const{ return mll_(*this)-=b; } mll_ operator*(ll b)const{ return mll_(*this)*=b; } mll_ operator/(ll b)const{ return mll_(*this)/=b; } friend mll_ operator+(ll a,mll_ b){ return b+a; } friend mll_ operator-(ll a,mll_ b){ return -b+a; } friend mll_ operator*(ll a,mll_ b){ return b*a; } friend mll_ operator/(ll a,mll_ b){ return mll_(a)/b; } bool operator==(mll_ b)const{ return val_==b.val_; } bool operator!=(mll_ b)const{ return val_!=b.val_; } bool operator==(ll b)const{ return *this==mll_(b); } bool operator!=(ll b)const{ return *this!=mll_(b); } friend bool operator==(ll a,mll_ b){ return mll_(a)==b; } friend bool operator!=(ll a,mll_ b){ return mll_(a)!=b; } friend ostream &operator<<(ostream &os,mll_ a){ return os << a.val_; } friend istream &operator>>(istream &is,mll_ &a){ return is >> a.val_; } mll_ pow(ll k)const{ mll_ ret(1,false),a(*this); for (; k>0; k>>=1,a*=a) if (k&1)ret*=a; return ret; } static constexpr int mod() { return MOD; } //enum{ modll=MOD }; }; #if 0 #define MODLL (1000000007LL) #else #define MODLL (998244353LL) #endif using mll = mll_<MODLL>; //using mll = fraction; //using mll = modint; using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; template<class T> struct Vector: vector<T>{ using Int = long long; using vT=vector<T>; using cvT=const vector<T>; using cT=const T; using vT::vT; //親クラスのコンストラクタの隠蔽を回避 using vT::begin,vT::end,vT::insert,vT::erase; auto it(Int i){ return begin()+i; } auto it(Int i)const{ return begin()+i; } Vector(cvT& b):vT(b){} Vector(vT&& b):vT(move(b)){} Vector(Int n,T s,T d){ iota(n,s,d); } template<class S> Vector &operator+=(S x){ for (T &e: *this) e+=x; return *this; } template<class S> Vector &operator-=(S x){ for (T &e: *this) e-=x; return *this; } template<class S> Vector &operator*=(S x){ for (T &e: *this) e*=x; return *this; } template<class S> Vector &operator/=(S x){ for (T &e: *this) e/=x; return *this; } template<class S> Vector &operator%=(S x){ for (T &e: *this) e%=x; return *this; } template<class S> Vector operator+(S x)const{ return Vector(*this)+=x; } template<class S> Vector operator-(S x)const{ return Vector(*this)-=x; } template<class S> Vector operator*(S x)const{ return Vector(*this)*=x; } template<class S> Vector operator/(S x)const{ return Vector(*this)/=x; } template<class S> Vector operator%(S x)const{ return Vector(*this)%=x; } Vector operator-()const{ return Vector(*this)*=-1; } template<class S> friend Vector operator-(S x,const Vector &a){ return -a+=x; } Int size()const{ return (Int)vT::size(); } Vector &push_back(cT& x){ vT::push_back(x); return *this; } Vector &pop_back(){ vT::pop_back(); return *this; } Vector &push_front(cT& x){ insert(begin(),x); return *this; } Vector &pop_front(){ erase(0); return *this; } Vector &insert(Int i,cT& x){ insert(it(i),x); return *this; } Vector &insert(Int i,cvT& b){ insert(it(i),b.begin(),b.end()); return *this; } Vector &erase(Int i){ erase(it(i)); return *this; } Vector &erase(Int l,Int r){ erase(it(l),it(r+1)); return *this; } Vector &concat(cvT &b){ insert(end(),b.begin(),b.end()); return *this; } Vector &concat(cvT &b,Int n){ for (int i=0;i<n;++i) concat(b); return *this; } Vector &repeat(Int n){ concat(vT(*this),n-1); return *this; } Vector &reverse(){ std::reverse(begin(),end()); return *this; } Vector &sort(){ std::sort(begin(),end()); return *this; } Vector &rsort(){ std::sort(vT::rbegin(),vT::rend()); return *this; } template<class Pr> Vector &sort(Pr pr){ std::sort(begin(),end(),pr); return *this; } Vector &uniq(){ erase(unique(begin(),end()),end()); return *this; } Vector &sortq(){ return sort().uniq(); } Vector &fill(Int l,Int r,cT& x){ std::fill(it(l),it(r+1),x); return *this; } template<class S=Int> Vector &iota(Int n,T s=0,S d=1){ vT::resize(n); if (n==0) return *this; (*this)[0]=s; for (int i=1;i<n;++i) (*this)[i]=(*this)[i-1]+d; return *this; } Int count(cT& x)const{ return Int(std::count(begin(),end(),x)); } Int count(Int l,Int r,cT& x)const{ return Int(std::count(it(l),it(r+1),x)); } template<class Pr> Int countif(Pr pr)const{ return Int(count_if(begin(),end(),pr)); } template<class Pr> Int countif(Int l,Int r,Pr pr)const{ return Int(count_if(it(l),it(r+1),pr)); } Int find(cT& x)const{ return Int(std::find(begin(),end(),x)-begin()); } Int find(Int l,Int r,cT& x)const{ return Int(std::find(it(l),it(r+1),x)-begin()); } template<class Pr> Int findif(Pr pr)const{ return Int(find_if(begin(),end(),pr)-begin()); } template<class Pr> Int findif(Int l,Int r,Pr pr)const{ return Int(find_if(it(l),it(r+1),pr)-begin()); } Vector<Int> findall(cT& x)const{ return findall(0,size()-1,x); } Vector<Int> findall(Int l,Int r,cT& x)const{ return findallif(l,r,[&](cT& y){return y==x;}); } template<class Pr> Vector<Int> findallif(Pr pr)const{ return findallif(0,size()-1,pr); } template<class Pr> Vector<Int> findallif(Int l,Int r,Pr pr)const{ Vector<Int> ret; for (Int i=l;i<=r;++i) if (pr((*this)[i])) ret.push_back(i); return ret; } Int flooridx(cT& x)const{ return Int(upper_bound(begin(),end(),x)-begin()-1); } Int ceilidx(cT& x)const{ return Int(lower_bound(begin(),end(),x)-begin()); } Int leftnmof(cT& x)const{ return flooridx(x)+1; } Int rightnmof(cT& x)const{ return size()-ceilidx(x); } bool contains(cT& x)const{ Int i=flooridx(x); return i>=0 && (*this)[i]==x; } template<class Pr> Int flooridx(cT& x,Pr pr)const{ return Int(upper_bound(begin(),end(),x,pr)-begin()-1); } template<class Pr> Int ceilidx(cT& x,Pr pr)const{ return Int(lower_bound(begin(),end(),x,pr)-begin()); } template<class Pr> Int leftnmof(cT& x,Pr pr)const{ return flooridx(x,pr)+1; } template<class Pr> Int rightnmof(cT& x,Pr pr)const{ return size()-ceilidx(x,pr); } template<class Pr> bool contains(cT& x,Pr pr)const{ Int i=flooridx(x,pr); return i>=0 && (*this)[i]==x; } }; /* vll a={9,8,7},b={1,2,3}; vpll p={{5,3},{7,8},{0,2},}; - -------- 操作系 -------- a+=x a-=x a*=x a/=x a%=x a+x a-x a*x a/x a%x -a x-a //∀i a[i]にxを演算 a.push_front(x); a.push_back(x); a.pop_front(); a.pop_back(); a.insert(i,x); //a[i]にx挿入 a.insert(i,b); //a[i]にvll b挿入 a.erase(i); //a[i]削除 a.erase(l,r); //区間[l,r]削除 a.concat(b); //aにbを結合 a.concat(b,n); //aにbをn回結合 a.repeat(n); //aをn回繰り返す a.reverse(); //反転 a.sort(); //ソート a.rsort(); //逆順ソート p.sort([&](pll x,pll y){return x.second<y.second;});//比較関数指定ソート a.uniq(); //連続同値を1つにする a.sortq(); //ソートしてユニーク a.fill(l,r,x); //[l,r]にx代入 a.iota(n,s,d); //aを等差数列にする 長さn,初項s,公差d vll a(n,s,d); //コンストラクタ版iota - -------- 検索系 -------- auto pr=[&](auto &x){ return x>0; }; //検索条件 ll m=a.count(x); //xの個数 ll m=a.count(l,r,x); //xの個数in[l,r] ll m=a.countif(pr); //条件満たす個数 ll m=a.countif(l,r,pr); //条件満たす個数in[l,r] ll i=a.find(x); //xの最左位置i ない時N(配列長) ll i=a.find(l,r,x); //xの最左位置i in[l,r] ない時r+1 ll i=a.findif(pr); //条件満たす最左位置i ない時N(配列長) ll i=a.findif(l,r,pr); //条件満たす最左位置i in[l,r] ない時r+1 vll is=a.findall(x); //xの位置i列挙 vll is=a.findall(l,r,x); //xの位置i列挙in[l,r] vll is=a.findallif(pr); //条件満たす位置i列挙 vll is=a.findallif(l,r,pr); //条件満たす位置i列挙in[l,r] - -------- 昇順sort済み配列用 -------- ll i=a.flooridx(x); //x以下の最近傍位置i ない時-1 ll i=a.ceilidx(x); //x以上の最近傍位置i ない時N(配列長) ll m=a.leftnmof(x); //x以下の個数 ll m=a.rightnmof(x); //x以上の個数 bool b=a.contains(x); //xを含む - -------- 比較関数prでsort済みの配列用 -------- auto pr=[&](auto &x,auto &y){ return x>y; }; //降順ソート時 ll i=a.flooridx(x,pr); //x以左の最近傍位置i ない時-1 ll i=a.ceilidx(x,pr); //x以右の最近傍位置i ない時N(配列長) ll m=a.leftnmof(x,pr); //x以左の個数 ll m=a.rightnmof(x,pr); //x以右の個数 bool b=a.contains(x,pr); //xを含む a.concat(b,n).pop_back().rsort().uniq(); //連続適用できる */ namespace SolvingSpace{ template<class T> using vector = Vector<T>; using vll=vector< ll>; using vmll=vector< mll>; using vdd=vector< dd>; using vvll=vector< vll>; using vvmll=vector< vmll>; using vvdd=vector< vdd>; using vvvll=vector< vvll>; using vvvmll=vector< vvmll>; using vvvdd=vector<vvdd>; using vvvvll=vector<vvvll>; using vvvvmll=vector<vvvmll>; using vpll=vector< pll>; using vtll=vector< tll>; using vqll=vector< qll>; using vvpll=vector< vpll>; using vvtll=vector< vtll>; using vvqll=vector<vqll>; struct dubling{ vvll next; //next[i][v]:vの2^i個先 ll N=0,B=1, E=INF/*終点を表すノード番号*/; dubling(){} dubling(const vll &nx,ll tMax){ init(nx,tMax); } void init(const vll &nx, ll tMax){ N = (ll)nx.size(); while (tMax>=(1LL<<B)) B++; next.resize(B,vll(N,INF)); next[0]=nx; rep(i,0,B-2) rep(v,0,N-1) next[i+1][v] = Next(Next(v,i),i); } ll jump(ll v,ll t){ //t個先 t=0も可 for (ll i=0; t>0; t>>=1,i++) if(t&1) v=Next(v,i); return v; } ll jump2i(ll v,ll i){ return Next(v,i); } //2^i個先 pll find_inv (ll s,ll v){ return Find(s,1, [&](ll u,ll t){ return u<=v&&u!=E; }); } pll find_overv (ll s,ll v){ return Find(s,0, [&](ll u,ll t){ return u<=v&&u!=E; }); } pll rfind_inv (ll s,ll v){ return Find(s,1, [&](ll u,ll t){ return u>=v&&u!=E; }); } pll rfind_overv(ll s,ll v){ return Find(s,0, [&](ll u,ll t){ return u>=v&&u!=E; }); } template<class Pr> pll Find(ll v,bool in,Pr pred){ ll t=0; dep(i,B-1,0){ auto[vB,tB]=Jump2i(i,v,t); if (pred(vB,tB)) {v=vB; t=tB;} //条件内→移動 } return in ? pll{v,t} : Jump2i(0,v,t); } /*---- utility ----*/ pll Jump2i(ll i,ll v,ll t){ return {Next(v,i), t+(1LL<<i)}; }//2^i個先に移動 ll Next(ll v,ll i){ return (v==INF) ? INF : next[i][v]; }//vの2^i個先 }; template <class T,class E> struct LazySegmentTree{ using F = function<T(T,T)>; using G = function<T(T,E)>; using H = function<E(E,E)>; F f; //!< データ同士の合成関数 G g; //!< 作用関数 H h; //!< 作用素同士の合成関数 T ti; //!< データの単位元 実際の単位元をセットしないとだめ E ei; //!< 作用素の単位元 実際の単位元ではなくても、使わない値ならOK ll n=1; //!< データ数の2べき上界 vector<T> dat; vector<E> laz; LazySegmentTree() {} LazySegmentTree(vector<T> &v,F f,G g,H h,T ti,E ei) { Init(v,f,g,h,ti,ei); } void Init(vector<T> &v,F f,G g,H h,T ti,E ei){ this->f=f; this->g=g; this->h=h; this->ti=ti; this->ei=ei; while (n<(ll)v.size()) n<<=1; dat.resize(2*n-1,ti); laz.resize(2*n-1,ei); for (ll i=0; i<(ll)v.size(); i++) dat[n-1+i] = v[i]; for (ll i=n-2; i>=0; i--) dat[i] = f(dat[2*i+1],dat[2*i+2]); } void set(ll i,E x){ set(i,i,x); } void set(ll a,ll b,E x){ Set(a,b+1,x,0,0,n); } //[a,b]にxを作用 void Set(ll a,ll b,E x,ll k,ll l,ll r){ //注目ノードk=[l,r) if (r<=a || b<=l) eval(k); //[l,r)が[a,b)と重ならない時 else if (a<=l && r<=b){ //[l,r)が[a,b)の内側の時 laz[k] = h(laz[k],x); eval(k); //親ノードが後でdat[k]を見るため、ここで評価しておく必要あり } else { //[l,r)が[a,b)から一部はみ出す時 eval(k); //子ノードを処理する前に評価しておく必要あり Set(a,b,x,k*2+1,l,(l+r)/2); Set(a,b,x,k*2+2,(l+r)/2,r); dat[k] = f(dat[k*2+1],dat[k*2+2]); } } T operator [] (ll i) { return get(i,i); } T get(ll a,ll b) { return Get(a,b+1,0,0,n); }//[a,b]の値を取得 T Get(ll a,ll b,ll k,ll l,ll r){ //注目ノードk=[l,r) if (r<=a || b<=l) return ti; //[l,r)が[a,b)と重ならない時 eval(k); //自分や子の値を見る前に評価が必要 if (a<=l && r<=b) return dat[k];//[l,r)が[a,b)の内側の時 return f(Get(a,b,k*2+1,l,(l+r)/2),Get(a,b,k*2+2,(l+r)/2,r)); //[l,r)が[a,b)から一部はみ出す時 } template<class C> ll searchl(ll a,ll b,C c){//[a,p](a≦p≦b)がtrueな最左のp auto [pos,ignore]=SearchL(a,c,ti,0,0,n); return min(pos,b+1); } template<class C> ll searchr(ll a,ll b,C c){//[p,b](a≦p≦b)がtrueな最右のp auto [pos,ignore]=SearchR(b+1,c,ti,0,0,n); return max(pos,a-1); } template<class C> pair<ll,T> SearchL(ll a,C c,T s,ll k,ll l,ll r){ if (r<=a) return {r,s}; eval(k); T ss=f(s,dat[k]); if (a<=l && !c(ss)) return {r,ss};//現セル丸ごとがfalseの時 if (n-1 <= k) return {l,s};//葉の時 ll pos,m=(l+r)/2; ; tie(pos,s)=SearchL(a,c,s,2*k+1,l,m); if (pos==m) tie(pos,s)=SearchL(a,c,s,2*k+2,m,r); return {pos,s}; } template<class C> pair<ll,T> SearchR(ll b,C c,T s,ll k,ll l,ll r){ if (b<=l) return {l-1,s}; eval(k); T ss=f(dat[k],s); if (r<=b && !c(ss)) return {l-1,ss};//現セル丸ごとがfalseの時 if (n-1 <= k) return {l,s};//葉の時 ll pos,m=(l+r)/2; ; tie(pos,s)=SearchR(b,c,s,2*k+2,m,r); if (pos==m-1) tie(pos,s)=SearchR(b,c,s,2*k+1,l,m); return {pos,s}; } ll findpeakl(ll l,ll r){ T m=get(l,r); return searchl(l,r,[&](T x){return f(x,m)==x;}); } ll findpeakr(ll l,ll r){ T m=get(l,r); return searchr(l,r,[&](T x){return f(x,m)==x;}); } void eval(ll k){ if (laz[k] == ei) return; if (k<=n-2){ //子に作用素を配る laz[k*2+1] = h(laz[k*2+1],laz[k]); laz[k*2+2] = h(laz[k*2+2],laz[k]); } //自分に作用させる dat[k] = g(dat[k],laz[k]); laz[k] = ei; } #if defined(_DEBUG) void dump(char deli=' '){ for (ll i=0;i<n;i++) cerr<<(*this)[i]<<deli; cerr<<'\n'; } void dump_tree(ll w=5){ auto disp=[&](ll x){ if (x==-INF) cerr << setw(w) << "-INF"; else if (x== INF) cerr << setw(w) << "INF"; else cerr << setw(w) << x; }; REP(i,n) for (ll k=i+n-1,m=1,p=1; k>=0; p=m,m*=k%2,k=(k==0)?-1:(k-1)/2){ if (m){ cerr << ((k<n-1)?" ┬ ":""); disp(dat[k]); cerr<<" /"; disp(laz[k]); } else{ cerr << ((p&~m) ?" ┘ ":""); } if (k==0) cerr << '\n'; } cerr << '\n'; } #else void dump(char deli=' '){} void dump_tree(ll w=5){} #endif }; struct ConnectedComponents{ vvll *pto=nullptr; vll ccids;//ccids[v]:vの連結成分ID ll cnm=0;//連結成分数 vvll vs;//vs[cid]:連結成分ID=cidの頂点リスト ConnectedComponents(){} ConnectedComponents(vvll &to):pto(&to),ccids(to.size(),-1){ for (ll v=0;v<(ll)to.size();++v){ if (ccids[v]!=-1)continue; ccids[v]=cnm++; dfs(v); } vs.resize(cnm); for (ll v=0;v<(ll)to.size();++v){ vs[ccids[v]].push_back(v); } } void dfs(ll v){ for (ll u:(*pto)[v]){ if (ccids[u]!=-1)continue; ccids[u]=ccids[v]; dfs(u); } } pair<vvll,vll> get(){ return move(pair<vvll,vll>{move(vs),move(ccids)}); } }; /* auto[vs,ccids]=ConnectedComponents(to).get(); . ↑ ↑ccids[v]:vの連結成分ID . vs[id]:連結成分ID=idの頂点リスト */ vll bfs(vvll &to, vll ss){ vll dist(to.size(), INF); queue<ll> Q; for (auto&& s: ss) dist[s]=0, Q.push(s); while (!Q.empty()){ ll v = Q.front(); Q.pop(); each(u, to[v]) if(chmin(dist[u], dist[v]+1)) Q.push(u); } return move(dist); } vll cycleget(ll n,vll &A,ll s){//sから辿って到達するcycleを得る vll seen(n); ll v=s; while (seen[v]==0) seen[v]=1, v=A[v]; vll ret{v,}; while (ret[0]!=A[v]) ret.push_back(v=A[v]); return ret; } void cin2solve() { ll n,L,R; cin >> n >> L >> R; vector<ll> A; rep(i,0,n-1){ ll A__; cin>>A__; A.push_back(A__-1); } ll Q; cin >> Q; vector<pair<ll,ll>> st; rep(i,0,Q-1){ ll s; ll t; cin>>s>>t; st.emplace_back(s-1,t-1); } vvll uto(n), dto(n); rep(v,0,sz(A)-1){ dto[v].push_back(A[v]); uto[v].push_back(A[v]); uto[A[v]].push_back(v); } auto[vs,ccids]=ConnectedComponents(uto).get(); vvll cycles(vs); //cycles[id]:連結成分idのcycle(vの列) (23,19,5,2,17) vll dist; //dist[v]: vからcycleまでの距離 vll ciof(n,-1); //ciof[v]: cycleでのインデックスci cycles[id][ci]がv //ci=-1ならcycle外 dubling dbl(A,n+10); rep(id,0,sz(vs)-1){ cycles[id]=cycleget(n,A,vs[id][0]); } rep(id,0,sz(vs)-1){ rep(ci,0,sz(cycles[id])-1) ciof[cycles[id][ci]]=ci; } { vll ss; for (auto&& cycle: cycles) ss.concat(cycle); dist = bfs(uto,ss); } vvll dists(sz(vs));//dists[id]:連結成分idでの元距離と距離の関係 //vector<LazySegmentTree<ll,ll>> dists; //dists[id]:連結成分idでの元距離と距離の関係 rep(id,0,sz(vs)-1){ ll ni=sz(vs[id]);//idの頂点数 ll C=sz(cycles[id]);//idのサイクル長 vll ini(ni,INF); LazySegmentTree<ll,ll> sgt(ini, [](ll x,ll y){ return min(x,y); }, //データ同士の合成関数 [](ll x,ll y){ return min(x,y); }, //データxに作用素yを作用させる関数 [](ll x,ll y){ return min(x,y); }, //作用素の合成関数 INF,INF); rep(t,1,ni){ ll st=t*L,en=t*R; ll D=ni-C; if (st<D) chmin(en,ni-1); else if (en-st+1>=C) st=D,en=ni-1; else{ st=(st-D)%C+D; en=(en-D)%C+D; } //[st,en]にtをchmin if (st<=en) sgt.set(st,en,t); else{ sgt.set(st,ni-1,t); sgt.set(D,en,t); } } dists[id].resize(ni); rep(i,0,ni-1) dists[id][i]=sgt[i]; rep(j,0,C-1){ dep(k,ni-1-j-C,0,C){ chmin(dists[id][k],dists[id][k+C]); } } } vll ans; for (auto&&[S,T]:st){ ll id=ccids[S]; //-- 連結成分が異なる→NG if (id!=ccids[T]) ans.push_back(-1); //-- 両方木上 else if (ciof[S]==-1 and ciof[T]==-1){ ll d=dist[S]-dist[T]; //Sの先にTなし if (d<=0 or dbl.jump(S,d)!=T) ans.push_back(-1); //Sの先にT有 else{ ll times=CEIL(d,R); if (d<L*times) times=-1; //Tを飛び越える ans.push_back(times); } } //-- Tだけ木上→NG else if (ciof[S]!=-1 and ciof[T]==-1) ans.push_back(-1); //-- Tがcycle上 else{ ll C=sz(cycles[id]), ofs; { ll rt=dbl.jump(S,dist[S]); ll rtci=ciof[rt]; ll Tci=ciof[T]; ll drtT=(Tci-rtci+C)%C; ofs=dist[S]+drtT; } ll times=dists[id][ofs]; if (times>INF/2) times=-1; ans.push_back(times); } } each(e,ans) cout << e << '\n'; return; } };//SolvingSpace ////////////////////////////////////////// int main(){ #if 1 //SolvingSpace::labo(); SolvingSpace::cin2solve(); //SolvingSpace::generand(); #else ll t; cin >> t; rep(i,0,t-1){ SolvingSpace::cin2solve(); //SolvingSpace::generand(); } #endif cerr << timeget() <<"ms"<< '\n'; return 0; }