結果
問題 | No.2704 L to R Graph |
ユーザー | HIR180 |
提出日時 | 2024-03-29 22:55:24 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,372 ms / 3,000 ms |
コード長 | 8,876 bytes |
コンパイル時間 | 3,230 ms |
コンパイル使用メモリ | 193,952 KB |
実行使用メモリ | 32,364 KB |
最終ジャッジ日時 | 2024-09-30 16:41:19 |
合計ジャッジ時間 | 17,166 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 3 ms
6,820 KB |
testcase_02 | AC | 3 ms
6,820 KB |
testcase_03 | AC | 4 ms
6,820 KB |
testcase_04 | AC | 3 ms
6,816 KB |
testcase_05 | AC | 4 ms
6,820 KB |
testcase_06 | AC | 4 ms
6,816 KB |
testcase_07 | AC | 4 ms
6,816 KB |
testcase_08 | AC | 83 ms
29,696 KB |
testcase_09 | AC | 79 ms
29,568 KB |
testcase_10 | AC | 79 ms
29,824 KB |
testcase_11 | AC | 770 ms
32,364 KB |
testcase_12 | AC | 726 ms
32,232 KB |
testcase_13 | AC | 808 ms
32,240 KB |
testcase_14 | AC | 941 ms
32,232 KB |
testcase_15 | AC | 572 ms
32,364 KB |
testcase_16 | AC | 801 ms
32,232 KB |
testcase_17 | AC | 726 ms
32,192 KB |
testcase_18 | AC | 829 ms
32,204 KB |
testcase_19 | AC | 834 ms
32,236 KB |
testcase_20 | AC | 810 ms
32,236 KB |
testcase_21 | AC | 1,241 ms
32,364 KB |
testcase_22 | AC | 1,372 ms
32,232 KB |
testcase_23 | AC | 321 ms
32,232 KB |
testcase_24 | AC | 289 ms
32,236 KB |
testcase_25 | AC | 390 ms
32,240 KB |
testcase_26 | AC | 344 ms
32,236 KB |
testcase_27 | AC | 55 ms
29,688 KB |
testcase_28 | AC | 53 ms
29,952 KB |
ソースコード
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <cassert> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> //#include <unordered_map> //#include <unordered_set> #include <cassert> #include <iomanip> #include <chrono> #include <random> #include <bitset> #include <complex> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#include <atcoder/convolution> //#include <atcoder/lazysegtree> //#include <atcoder/maxflow> //#include <atcoder/mincostflow> //#include <atcoder/segtree> //#include <atcoder/string> using namespace std; //using namespace atcoder; #define int long long //#define L __int128 typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,x) for(int i=0;i<x;i++) #define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--) #define per(i,b) gnr(i,0,b) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) (int)(x.size()) #define pcnt(x) __builtin_popcountll(x) #define all(x) x.begin(),x.end() #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.fi<<","<<p.sc<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } //https://codeforces.com/blog/entry/62393 struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } //don't make x negative! size_t operator()(pair<int,int> x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; //unordered_set -> dtype, null_type //unordered_map -> dtype(key), dtype(value) using namespace __gnu_pbds; template<class t,class u> using hash_table=gp_hash_table<t,u,custom_hash>; template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 998244353; //const ll mod = 1000000007; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); struct mint{ ll v; mint(ll vv=0){s(vv%mod+mod);} mint& s(ll vv){ v=vv<mod?vv:vv-mod; return *this; } mint operator-()const{return mint()-*this;} mint &operator+=(const mint&rhs){return s(v+rhs.v);} mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);} mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;} mint &operator/=(const mint&rhs){return *this*=rhs.inv();} mint operator+(const mint&rhs)const{return mint(*this)+=rhs;} mint operator-(const mint&rhs)const{return mint(*this)-=rhs;} mint operator*(const mint&rhs)const{return mint(*this)*=rhs;} mint operator/(const mint&rhs)const{return mint(*this)/=rhs;} mint pow(ll n)const{ if(n<0)return inv().pow(-n); mint res(1),x(*this); while(n){ if(n&1)res*=x; x*=x; n>>=1; } return res; } mint inv()const{return pow(mod-2);} friend mint operator+(ll x,const mint&y){ return mint(x)+y; } friend mint operator-(ll x,const mint&y){ return mint(x)-y; } friend mint operator*(ll x,const mint&y){ return mint(x)*y; } friend mint operator/(ll x,const mint&y){ return mint(x)/y; } friend ostream& operator<<(ostream&os,const mint&m){ return os<<m.v; } friend istream& operator>>(istream&is,mint&m){ ll x;is>>x; m=mint(x); return is; } bool operator<(const mint&r)const{return v<r.v;} bool operator==(const mint&r)const{return v==r.v;} bool operator!=(const mint&r)const{return v!=r.v;} explicit operator bool()const{ return v; } }; ll modpow(ll x,ll n,ll _md=mod){ ll res=1; while(n>0){ if(n&1) res=res*x%_md; x=x*x%_md; n>>=1; } return res; } #define _sz 1 mint F[_sz],R[_sz]; void make(){ F[0] = 1; for(int i=1;i<_sz;i++) F[i] = F[i-1] * i; R[_sz-1] = F[_sz-1].pow(mod-2); for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1); } mint C(int a,int b){ if(b < 0 || a < b) return mint(); return F[a]*R[b]*R[a-b]; } using ld=long double; using vi=vc<int>; using ull=unsigned long long; /*int dst(P p, P q){ return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b); }*/ /*template<class t> void add_inplace(t &a, t b){ if(a.size() < b.size()) swap(a, b); for(int i=0;i<si(b);i++){ a[i] += b[i]; if(a[i] < 0) a[i] += mod; if(a[i] >= mod) a[i] -= mod; } return ; }*/ template<class t> void ov(const vc<t>&a){ if(a.empty()) return; rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' '); } //o(ans?"Yes":"No"); #define sz 100005 int par[sz],ran[sz]; void init(){ for(int i=0;i<sz;i++) par[i] = i, ran[i] = 0; } int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x); y = find(y); if(x==y) return; if(ran[x] < ran[y]) par[x] = y; else{ par[y] = x; if(ran[x] == ran[y]) ran[x]++; } } bool same(int x,int y){ return find(x)==find(y); } struct gausssum{ ll md(ll x,ll y){ if(x>=0) return x/y; ll vl=md(-x,y); if((-x)%y==0) return -vl; else return -vl-1; } //[(ax+b)/n] (s<=x<=t) ll count(ll s,ll t,ll n,ll a,ll b){ if(a < 0){ return count(-t,-s,n,-a,b); } ll S = md(a*s+b,n), T = md(a*t+b,n); if(S == T) return (t-s+1)*S; if(n <= a){ ll sum = (s+t)*(t-s+1)/2LL; return count(s,t,n,a%n,b)+sum*(a/n); } ll sum = count(S+1,T,a,n,-b-1); ll all = T*t-S*(s-1); return all-sum; } }gs; void solve(){ int n,lb,ub;cin>>n>>lb>>ub; vi a(n+1); vc<vi>e(n+1); repn(i,n)cin>>a[i]; repn(i,n)e[a[i]].pb(i); init(); repn(i,n) unite(i,a[i]); vi cnt(n+1); repn(i,n) cnt[find(i)] ++; vi cyc(n+1, -1), len(n+1); vi dep(n+1); vc<vi>up(20,vi(n+1,-1)); auto dfs=[&](auto self,int v,int u)->void{ dep[v] = dep[u] + 1; up[0][v] = u; for(auto to:e[v]){ self(self,to,v); } }; repn(i,n){ if(i != find(i)) continue; int now = i; rep(_, cnt[i]*2) now = a[now]; int beg = now; rep(_, cnt[i]*2){ cyc[now] = _; now = a[now]; if(now == beg){ len[i] = _+1; break; } } now = beg; rep(_, cnt[i]*2){ for(auto to:e[now]){ if(cyc[to] == -1) dfs(dfs, to, now); } now = a[now]; if(now == beg) break; } } rep(j,19){ repn(i,n){ if(up[j][i] == -1) up[j+1][i] = -1; else up[j+1][i] = up[j][up[j][i]]; } } int q;cin>>q; while(q--){ int s,t;cin>>s>>t; if(!same(s,t)) o(-1); else{ int beg, d; if(cyc[s] >= 0 and cyc[t] < 0){ o(-1); continue; } else if(cyc[t] >= 0){ if(cyc[s] == -1){ beg = dep[s]; rep(_, 20) if(((beg>>_)&1)) s = up[_][s]; } else beg = 0; int diff = cyc[t] - cyc[s]; diff = (diff % len[find(s)] + len[find(s)]) % len[find(s)]; beg += diff; d = len[find(s)]; } else{ if(dep[s] < dep[t]){ o(-1); continue; } else{ int D = dep[s] - dep[t]; rep(_,20) if(((D>>_)&1)) s = up[_][s]; if(s == t){ beg = D, d = 0; } else{ o(-1); continue; } } } //cout << beg << " d " << d << endl; if(d == 0){ int x = (beg+ub-1)/ub; if(x*lb > beg) o(-1); else o(x); } else{ cerr << "IN\n"; int l2b = -1, u2b = 1000000; while(u2b-l2b > 1){ int mid = (l2b+u2b)/2; if(gs.count(0,mid,lb,d,beg) > gs.count(0,mid,ub,d,beg-1)){ u2b = mid; } else l2b = mid; } if(u2b == 1000000) o(-1); else{ int tar = beg+d*u2b; o((tar+ub-1)/ub); } } } } } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int t; t=1;//cin >> t; while(t--) solve(); }