QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#243225 | #4758. Captivating process | ningago | WA | 14ms | 69556kb | C++14 | 8.7kb | 2023-11-07 22:28:04 | 2023-11-07 22:28:05 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <set>
namespace uvu
{
#define LOCAL ________________don_t_define_me_________________
#define ll long long
#define inf 0x3f3f3f3f
//#define int long long
//#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = (1 << 20);
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
return getchar();
#else
if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return iS == iT ? EOF : *iS++;
#endif
}
void Flush()
{
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
struct Flusher{ ~Flusher() { Flush(); } }iamflusher;
void putc(char c)
{
#ifdef LOCAL
putchar(c);
#else
*oS++ = c;
if(oS == oT) Flush();
#endif
}
template <typename T = int> T read()
{
T x = 0, f = 1; char c = getc();
for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
for(; isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
}
template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
top = 0;
if(x < 0) putc('-'), x = -x;
if(!x) sta[++top] = 0;
for(; x; x /= 10) sta[++top] = x % 10;
for(; top; top--) putc(sta[top] ^ 48);
if(c) putc(c);
}
int readstr(char *s, int base)
{
int idx = base - 1; char c = getc();
for(; !isdigit(c) && !isalpha(c) && c != '.' && c != '#' && c != '?'; c = getc());
for(; isdigit(c) || isalpha(c) || c == '.' || c == '#' || c == '?'; c = getc()) s[++idx] = c;
return idx - base + 1;
}
template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> T gcd(T x, T y) { for(; y; x %= y, x ^= y ^= x ^= y); return x; }
#define mod 998244353
//#define mod 1000000007
void plus_(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }
int sm(int x) { return x >= mod ? x - mod : x; }
#define N 200010
int n, m;
int a[N];
bool in_ring[N], vis[N];
int len[N], top[N], root[N];
std::vector <int> vec[N];
int kfa[N][21], d[N], dep[N], dfn[N], out[N];
void dfs(int k, int fa)
{
dfn[k] = ++dfn[0];
vis[k] = 1;
if(fa)
{
dep[k] = dep[fa] + 1;
top[k] = top[fa], root[k] = root[fa];
kfa[k][0] = fa;
for(int i = 1; i < 20; i++) kfa[k][i] = kfa[kfa[k][i - 1]][i - 1];
}
for(int nx : vec[k]) if(!in_ring[nx] && nx != fa) dfs(nx, k);
out[k] = dfn[0];
}
void solve(int p)
{
for(; !vis[p]; vis[p] = 1, p = a[p]);
int S = p;
top[S] = S;
static int sta[N], tp;
tp = 0;
for(; !in_ring[p]; p = a[p]) in_ring[p] = 1, sta[++tp] = p, len[S]++;
for(int i = 1; i <= tp; i++) kfa[sta[i]][0] = a[sta[i]], d[sta[i]] = i - 1;
for(int j = 1; j < 20; j++) for(int i = 1; i <= tp; i++) kfa[sta[i]][j] = kfa[kfa[sta[i]][j - 1]][j - 1];
for(int i = 1; i <= tp; i++) root[sta[i]] = S, top[sta[i]] = sta[i], dfs(sta[i], 0);
}
int x_[N], y_[N];
bool ok1[N], ok2[N], ok3[N], ok4[N];
namespace S1
{
struct Tree
{
int ls, rs;
int lazy;
}tr[N * 25];
int idx;
#define lson(k) tr[k].ls
#define rson(k) tr[k].rs
void ins(int &k, int l, int r, int ql, int qr, int z)
{
if(!k) k = ++idx;
if(ql <= l && r <= qr) { tr[k].lazy += z; return; }
int mid = (l + r) >> 1;
if(ql <= mid) ins(lson(k), l, mid, ql, qr, z);
if(mid < qr) ins(rson(k), mid + 1, r, ql, qr, z);
}
int query(int k, int l, int r, int q)
{
if(!k) return 0;
if(l == r) return tr[k].lazy;
int mid = (l + r) >> 1;
if(q <= mid) return tr[k].lazy + query(lson(k), l, mid, q);
else return tr[k].lazy + query(rson(k), mid + 1, r, q);
}
int rt[N];
#define D n
std::vector <pii> vc[N];
void dfs(int k)
{
ins(rt[dep[k - n] - dep[k] + D], 1, n + n, dfn[k - n], out[k - n], 1);
for(pii t : vc[k]) if(query(rt[dep[t.fi] - dep[k] + D], 1, n + n, dfn[t.fi])) ok1[t.se] = 1;
for(int nx : vec[k])
{
if(in_ring[nx]) continue;
dfs(nx);
}
ins(rt[dep[k - n] - dep[k] + D], 1, n + n, dfn[k - n], out[k - n], -1);
}
void solve()
{
for(int i = 1; i <= m; i++) vc[y_[i]].push_back(mkp(x_[i], i));
for(int i = n + 1; i <= n + n; i++) if(in_ring[i]) dfs(i);
}
}
namespace S2
{
std::map <int, int> mp[N];
std::vector <pii> vc[N];
void dfs(int k)
{
if(in_ring[k + n])
mp[root[k + n]][(d[k + n] + dep[k]) % len[root[k]]]++;//, printf("ins(%d)\n", k + n);
for(pii t : vc[k]) if(mp[root[t.fi]][(d[t.fi] + dep[k]) % len[root[t.fi]]]) ok3[t.se] = 1;
for(int nx : vec[k])
{
if(in_ring[nx]) continue;
dfs(nx);
}
if(in_ring[k + n])
mp[root[k + n]][(d[k + n] + dep[k]) % len[root[k]]]--;
}
void solve()
{
for(int i = 1; i <= m; i++)
{
int x = x_[i], y = y_[i];
if(in_ring[y]) continue;
// printf("x = %d, y = %d\n", x, y);
int t = 1;
for(int j = 19; ~j; j--) if(!in_ring[kfa[y][j]]) y = kfa[y][j], t += (1 << j);
y = kfa[y][0];
for(int j = 19; ~j; j--) if(t & (1 << j)) x = kfa[x][j];
if(in_ring[x]) continue;
// printf("x = %d, y = %d\n", x, y);
vc[x].push_back(mkp(y, i));
}
for(int i = 1; i <= n; i++) if(in_ring[i]) dfs(i);
}
}
namespace S3
{
std::map <int, int> mp[N];
std::vector <pii> vc[N];
void dfs(int k)
{
if(in_ring[k - n])
mp[root[k - n]][(d[k - n] + dep[k]) % len[root[k - n]]]++;
for(pii t : vc[k]) if(mp[root[t.fi]][(d[t.fi] + dep[k]) % len[root[t.fi]]]) ok2[t.se] = 1;
for(int nx : vec[k])
{
if(in_ring[nx]) continue;
dfs(nx);
}
if(in_ring[k - n])
mp[root[k - n]][(d[k - n] + dep[k]) % len[root[k - n]]]--;
}
void solve()
{
for(int i = 1; i <= m; i++)
{
int x = x_[i], y = y_[i];
if(in_ring[x]) continue;
int t = 1;
for(int j = 19; ~j; j--) if(!in_ring[kfa[x][j]]) x = kfa[x][j], t += (1 << j);
x = kfa[x][0];
for(int j = 19; ~j; j--) if(t & (1 << j)) y = kfa[y][j];
if(in_ring[y]) continue;
vc[y].push_back(mkp(x, i));
}
for(int i = n + 1; i <= n + n; i++) if(in_ring[i]) dfs(i);
}
}
namespace S4
{
std::vector <pii> vc[N];
std::map <int, std::map <int, bool> > mp[N];
void solve()
{
for(int i = 1; i <= m; i++)
{
int x = x_[i], y = y_[i];
// printf("x = %d, y = %d\n", x, y);
if(!in_ring[x])
{
int t = 1;
for(int j = 19; ~j; j--) if(!in_ring[kfa[x][j]]) x = kfa[x][j], t += (1 << j);
x = kfa[x][0];
for(int j = 19; ~j; j--) if(t & (1 << j)) y = kfa[y][j];
}
if(!in_ring[y])
{
int t = 1;
for(int j = 19; ~j; j--) if(!in_ring[kfa[y][j]]) y = kfa[y][j], t += (1 << j);
y = kfa[y][0];
for(int j = 19; ~j; j--) if(t & (1 << j)) x = kfa[x][j];
}
// printf("i = %d S4(%d %d)\n", i, x, y);
vc[y].push_back(mkp(x, i));
}
for(int i = 1; i <= n; i++)
{
if(!in_ring[i] || !in_ring[i + n]) continue;
int g = gcd(len[root[i]], len[root[i + n]]);
mp[root[i]][root[i + n]][((d[i] - d[i + n]) % g + g) % g] = 1;
// printf("i = %d, mp[%d][%d][%d] = 1\n", i, root[i], root[i + n], ((d[i] - d[i + n]) % g + g) % g);
}
for(int i = n + 1; i <= n + n; i++)
{
for(pii t : vc[i])
{
int x = t.fi, id = t.se, y = i;
int g = gcd(len[root[x]], len[root[y]]);
// printf("id = %d, check(%d %d %d)\n", id, root[x], root[y], ((d[x] - d[y]) % g + g) % g);
if(mp[root[x]][root[y]][((d[x] - d[y]) % g + g) % g]) ok4[id] = 1;
}
}
}
}
void printstr(const char *s, int l, int r) { for(int i = l; i <= r; i++) putc(s[i]); }
void solve()
{
// memset(h, idx = -1, sizeof(h));
n = read(), m = read();
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i <= n; i++) a[i + n] = read() + n;
for(int i = 1; i <= n + n; i++) vec[a[i]].push_back(i);
for(int i = 1; i <= n + n; i++) if(!vis[i]) solve(i);
// for(int i = 1; i <= n + n; i++) printf("d[%d] = %d, dep[%d] = %d\n", i, d[i], i, dep[i]);
for(int i = 1; i <= m; i++) x_[i] = read(), y_[i] = read() + n;
S1::solve();
S2::solve();
S3::solve();
S4::solve();
for(int i = 1; i <= m; i++)
{
// printf("%d %d %d %d\n", ok1[i], ok2[i], ok3[i], ok4[i]);
if(ok1[i] || ok2[i] || ok3[i] || ok4[i] || x_[i] + n == y_[i]) printstr("YES\n", 0, 3);
else printstr("NO\n", 0, 2);
}
}
void init()
{
}
char _ED_;
void mian()
{
debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
init();
for(int T = 1; T; solve(), T--);
}
#ifdef int
#undef int
#endif
}
/*
10 1
3 10 1 8 8 3 8 4 9 10
8 6 4 2 10 3 6 3 4 10
8 8
*/
int main()
{
uvu::mian();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 67984kb
input:
3 2 2 3 1 2 3 1 1 2 1 1
output:
NO YES
result:
ok 2 token(s): yes count is 1, no count is 1
Test #2:
score: 0
Accepted
time: 4ms
memory: 64992kb
input:
4 2 2 3 4 2 2 4 4 1 1 2 1 4
output:
NO YES
result:
ok 2 token(s): yes count is 1, no count is 1
Test #3:
score: 0
Accepted
time: 12ms
memory: 69428kb
input:
10 10 7 7 8 10 5 7 9 3 2 3 4 2 7 1 3 3 10 1 9 3 7 8 7 6 7 9 1 5 3 6 5 3 10 6 8 9 9 4 6 7
output:
NO NO YES NO YES NO YES NO NO NO
result:
ok 10 token(s): yes count is 3, no count is 7
Test #4:
score: 0
Accepted
time: 4ms
memory: 64716kb
input:
10 10 5 7 8 2 6 8 7 5 5 10 8 2 5 8 3 9 10 7 2 8 8 7 5 6 4 9 3 6 5 6 2 3 8 4 5 9 6 8 3 4
output:
NO NO YES NO NO NO NO NO NO YES
result:
ok 10 token(s): yes count is 2, no count is 8
Test #5:
score: 0
Accepted
time: 4ms
memory: 64956kb
input:
10 10 1 9 4 4 2 6 7 8 9 4 1 9 9 5 5 1 3 9 3 10 1 7 3 8 7 8 9 8 6 9 10 7 3 5 1 7 8 5 8 5
output:
NO NO NO YES NO NO NO NO NO NO
result:
ok 10 token(s): yes count is 1, no count is 9
Test #6:
score: 0
Accepted
time: 4ms
memory: 64672kb
input:
10 10 8 2 4 4 7 10 9 9 10 5 5 4 9 3 10 6 10 10 5 4 7 8 2 3 6 2 1 2 2 10 5 4 10 4 10 9 10 7 6 1
output:
YES NO YES YES NO YES YES YES YES YES
result:
ok 10 token(s): yes count is 8, no count is 2
Test #7:
score: 0
Accepted
time: 14ms
memory: 69516kb
input:
10 10 4 4 8 3 7 7 7 2 10 9 4 10 6 6 4 10 5 8 5 9 3 7 6 2 1 2 3 2 4 7 1 9 6 5 5 10 6 1 5 2
output:
YES NO YES YES YES YES NO NO NO NO
result:
ok 10 token(s): yes count is 5, no count is 5
Test #8:
score: 0
Accepted
time: 4ms
memory: 69072kb
input:
10 10 3 10 1 8 8 3 8 4 9 10 8 6 4 2 10 3 6 3 4 10 2 7 8 3 2 8 3 3 1 9 4 3 6 8 5 10 8 8 6 8
output:
NO YES NO YES NO NO YES NO YES YES
result:
ok 10 token(s): yes count is 5, no count is 5
Test #9:
score: 0
Accepted
time: 4ms
memory: 64712kb
input:
10 10 1 8 3 5 4 2 6 7 8 7 6 4 6 3 8 7 4 3 9 4 8 2 4 2 4 2 4 3 3 10 4 10 7 1 7 8 4 2 6 3
output:
NO NO NO NO YES NO YES NO NO NO
result:
ok 10 token(s): yes count is 2, no count is 8
Test #10:
score: 0
Accepted
time: 3ms
memory: 64916kb
input:
10 10 5 4 7 3 3 6 4 8 2 9 1 5 2 2 6 3 2 4 1 5 4 4 10 2 2 7 6 4 9 6 9 10 10 9 7 4 7 7 5 4
output:
YES YES YES YES YES YES NO YES YES YES
result:
ok 10 token(s): yes count is 9, no count is 1
Test #11:
score: 0
Accepted
time: 3ms
memory: 69360kb
input:
100 100 41 55 57 45 38 30 83 53 33 51 90 57 82 35 28 54 35 47 39 68 67 98 57 8 71 99 83 39 83 44 76 88 18 41 15 17 39 46 79 79 54 28 57 22 81 33 63 17 1 21 32 58 47 17 4 46 10 45 97 44 29 71 5 50 79 66 83 88 43 97 50 56 5 100 23 1 66 16 76 34 71 60 59 57 87 47 6 23 68 55 79 32 73 10 98 65 90 30 79 5...
output:
YES NO NO YES NO YES YES NO NO YES NO NO YES YES NO NO NO NO YES YES NO YES NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO YES NO YES NO NO NO YES YES NO YES NO YES YES NO NO NO NO NO NO NO YES NO NO NO YES NO NO YES NO NO NO NO NO NO YES YES NO YES NO NO YES NO NO NO NO YES YES YES NO NO NO YES N...
result:
ok 100 token(s): yes count is 35, no count is 65
Test #12:
score: 0
Accepted
time: 4ms
memory: 69556kb
input:
100 100 70 57 94 89 18 74 52 32 87 20 73 47 60 55 74 37 33 81 55 2 23 23 69 23 52 12 92 85 38 25 41 66 49 94 27 50 23 35 23 38 58 81 75 92 11 6 36 33 8 60 13 2 58 93 59 67 46 95 45 12 56 87 31 25 18 5 17 10 95 47 38 72 87 84 17 39 74 36 39 23 91 90 86 68 23 20 38 29 60 60 56 19 66 43 21 7 84 25 41 5...
output:
YES NO NO YES NO YES NO NO NO NO NO NO YES NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO YES NO YES NO NO YES YES YES YES YES YES NO NO YES NO YES YES NO NO NO NO YES NO NO YES NO NO YES YES YES NO NO NO NO YES YES NO NO NO NO YES NO YES NO NO NO NO NO NO NO NO YES NO YES NO NO NO NO NO NO NO NO NO...
result:
ok 100 token(s): yes count is 28, no count is 72
Test #13:
score: 0
Accepted
time: 4ms
memory: 68404kb
input:
100 100 48 86 67 28 67 38 10 31 5 90 67 38 64 38 86 22 29 71 25 27 89 34 4 83 99 82 45 79 50 12 13 38 4 49 77 90 4 81 87 92 4 50 65 37 86 90 45 69 17 34 32 38 13 77 10 68 2 69 39 80 82 32 95 25 1 87 100 74 63 57 61 99 14 87 10 11 28 46 23 71 14 80 10 88 79 70 92 13 79 24 83 5 48 68 65 20 61 27 13 39...
output:
NO NO NO NO NO YES NO NO NO NO YES YES NO NO NO YES NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO YES NO NO YES NO NO YES NO NO NO NO NO NO NO NO YES NO NO NO NO NO YES NO NO NO NO NO NO NO YES NO YES NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO ...
result:
ok 100 token(s): yes count is 16, no count is 84
Test #14:
score: 0
Accepted
time: 3ms
memory: 69392kb
input:
100 100 29 3 9 64 93 98 50 93 98 79 28 28 16 18 30 68 91 80 5 39 93 14 79 96 91 98 25 20 33 49 14 28 65 64 50 15 68 20 62 92 23 50 29 9 47 85 23 36 11 24 35 52 70 70 99 15 1 96 14 32 77 89 100 35 46 82 94 74 32 64 57 46 31 19 13 75 48 62 53 97 26 90 35 92 61 1 59 61 57 73 25 9 55 14 92 47 81 66 76 8...
output:
NO YES NO NO YES YES NO YES YES YES NO NO YES NO YES YES YES NO YES YES NO NO NO YES YES YES YES YES YES YES YES NO YES YES YES YES NO YES NO YES YES YES YES YES YES NO YES NO YES NO YES YES YES YES YES YES YES NO YES YES YES YES NO YES YES NO YES NO YES YES NO YES YES YES YES YES YES YES YES YES NO...
result:
ok 100 token(s): yes count is 74, no count is 26
Test #15:
score: 0
Accepted
time: 7ms
memory: 64900kb
input:
100 100 86 1 12 24 31 19 62 98 11 27 18 13 68 79 20 91 89 5 66 9 42 94 93 52 64 13 63 32 82 3 98 75 39 85 52 21 13 69 46 15 13 67 20 89 56 74 89 30 88 49 22 47 7 58 99 90 92 42 34 3 59 65 71 23 73 86 32 42 59 21 39 83 100 65 27 8 3 30 6 12 11 10 89 9 50 14 42 55 64 61 9 75 51 15 90 76 80 4 90 26 100...
output:
YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES YES NO YES NO YES YES NO YES YES YES YES YES NO NO YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES YES NO NO YES YES NO YES YES YES YES YES YES YES YES YES NO NO YES YES YES NO NO YES YES YES YES YES NO NO YES ...
result:
ok 100 token(s): yes count is 77, no count is 23
Test #16:
score: 0
Accepted
time: 4ms
memory: 67964kb
input:
100 100 35 70 20 10 57 8 23 53 22 79 50 71 33 3 37 55 31 77 68 57 65 59 21 27 75 52 13 81 12 22 57 78 62 19 26 45 48 78 7 87 96 53 15 93 2 88 40 29 84 18 57 75 50 15 46 62 54 48 16 81 29 40 27 14 19 48 53 100 97 45 99 27 39 81 73 75 93 84 69 98 64 93 63 10 73 51 8 35 13 78 18 15 83 60 14 93 60 42 78...
output:
YES NO YES YES NO YES YES NO YES YES YES YES YES YES NO NO YES YES YES YES YES YES YES NO YES YES YES YES NO YES NO NO YES YES YES YES YES NO YES YES YES YES NO YES YES YES YES NO NO NO YES YES YES YES YES YES YES YES NO YES NO YES YES NO YES YES YES NO YES YES NO NO YES NO NO YES NO YES YES NO YES ...
result:
ok 100 token(s): yes count is 67, no count is 33
Test #17:
score: -100
Wrong Answer
time: 7ms
memory: 64720kb
input:
100 100 61 33 6 75 57 51 9 23 64 97 54 28 72 49 55 45 13 89 91 80 43 30 99 89 27 36 59 100 95 61 37 4 91 67 71 61 8 19 81 66 77 5 53 65 68 34 26 98 95 79 96 45 59 66 81 25 86 5 90 6 21 48 82 58 14 78 93 50 88 45 28 20 83 97 62 71 84 20 67 85 13 50 46 93 86 2 88 31 68 71 39 20 69 34 65 32 22 60 83 47...
output:
NO YES NO NO NO NO NO NO NO YES NO YES YES NO NO NO NO NO YES YES NO NO NO YES YES NO YES NO NO NO NO NO NO YES NO NO YES YES YES YES NO NO YES NO NO NO YES YES YES NO NO YES NO YES NO NO YES NO YES NO NO NO NO NO YES NO NO YES NO YES YES YES YES YES NO NO NO NO NO YES NO NO YES YES NO NO NO NO NO N...
result:
wrong answer expected YES, found NO [21st token]