QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243194#4758. Captivating processningagoWA 10ms69908kbC++148.6kb2023-11-07 21:56:392023-11-07 21:56:39

Judging History

你现在查看的是最新测评结果

  • [2023-11-07 21:56:39]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:69908kb
  • [2023-11-07 21:56:39]
  • 提交

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];
#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(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[k] - dep[t.fi] + 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]) 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
5 7 8 2 6 8 7 5 5 10
8 2 5 8 3 9 10 7 2 8
4 9
*/
int main()
{
	uvu::mian();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 69908kb

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: 69124kb

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: 4ms
memory: 65440kb

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: 10ms
memory: 65576kb

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: 68364kb

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: 69256kb

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: 3ms
memory: 68372kb

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: -100
Wrong Answer
time: 4ms
memory: 68760kb

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
NO
YES

result:

wrong answer expected YES, found NO [9th token]