QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#62533#4596. IronforgeqinjianbinAC ✓426ms46692kbC++3.1kb2022-11-19 23:46:552022-11-19 23:46:58

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-19 23:46:58]
  • 评测
  • 测评结果:AC
  • 用时:426ms
  • 内存:46692kb
  • [2022-11-19 23:46:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 10;
const int MAX = 2e5 + 5;
int pr[MAXN], p_sz;
bool vis[MAXN]; 
void init() {
	for (int i = 2; i < MAX; i++) {
		if (!vis[i]) pr[p_sz++] = i;
		for (int j = 0; j < p_sz; j++) {
			if (pr[j] * i >= MAX) break;
			vis[pr[j] * i] = 1;
			if (i % pr[j] == 0) break;
		}
	}
}
void change(int num, set<int>& st) {
	int t = sqrt(num + 0.5);
	for (int i = 0; pr[i] <= t; i++) {
		if (num % pr[i] == 0) {
			st.insert(pr[i]);
			while (num % pr[i] == 0) num /= pr[i];
		}
	}
	if (num > 1) st.insert(num);
}
int T, n, m;
struct Node {
	int l, r;
	int ll, rr;
	set<int> st;
	void init() {
		st.clear();
	}
}a[MAXN];

struct BNode {
	int num;
	int l, r;
}b[MAXN];
map<int, int> mp;

void fun() { //update a[].l, a[].r
    for (int i = n; i >= 1; i--) {
        int ind = i;
        while (ind < n && b[ind].l >= i) { //需要的在起点的右边
            ind = a[ind + 1].r;
        }
        a[i].r = ind;
    }
    for (int i = 1; i <= n; i++) {
        int ind = i;
        while (ind - 1 > 0 && b[ind - 1].r <= i) {
            ind = a[ind - 1].l;
        }
        a[i].l = ind;
    }
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) a[i].init();
	int tmp;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &tmp);
		change(tmp, a[i].st);
		// for (int it : a[i].st)
		// 	printf("%d ", it);
		// cout << endl;
	}
	for (int i = 1; i < n; i++) 
		scanf("%d", &b[i].num);
	mp.clear();
	for (int i = 1; i <= n; i++) {
		for (auto it : a[i].st)
			mp[it] = i;
		if (i != n) {
			if (mp.count(b[i].num)) b[i].l = mp[b[i].num];
			else b[i].l = 0;
		}
	}
	mp.clear();
	for (int i = n; i >= 1; i--) {
		for (auto it : a[i].st)
			mp[it] = i;
		if (i != 1) {
			if (mp.count(b[i - 1].num)) b[i - 1].r = mp[b[i - 1].num];
			else b[i - 1].r = n + 1;
		}
	}

	fun();

	a[1].ll = a[1].l, a[1].rr = a[1].r;
	for (int i = 2; i <= n; i++) {
		if (a[i - 1].rr >= i && a[i].l <= i - 1) {
			a[i].ll = a[i - 1].ll;
			a[i].rr = a[i - 1].rr;
		}
		else {
			int indl = a[i - 1].ll;
			if (a[i].l >= i) indl = a[i].l;
			int indr = a[i].r;
            // if (i == 3) cout << "asd" << indr << endl;

            while (1) {
				bool flag = false;
				// if (i == 3)
				// cout << "qwe" << indr << endl;
				while (indr < n && b[indr].l >= indl) {
					indr = a[indr + 1].r;
					flag = true;
				} 
				while (indl - 1 > 0 && b[indl - 1].r <= indr) {
					indr = max(indr, a[indl - 1].rr);
					indl = a[indl - 1].ll;
					flag = true;
				}
				if (!flag) break;
			}
			a[i].ll = indl;
			a[i].rr = indr;
		}
	}
	int x, y;
	// for (int i = 1; i <= n; i++) 
	// 	printf("%d %d\n", a[i].l, a[i].r);
	// for (int i = 1; i < n; i++) 
	// 	printf("%d %d\n", b[i].l, b[i].r);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &x, &y);
		if (y >= a[x].ll && y <= a[x].rr) puts("Yes");
		else puts("No");
	}
}

int main() {
#ifdef TanJI
    freopen("D:\\Cpp\\1.in", "r", stdin);
    freopen("D:\\Cpp\\1.out", "w", stdout);
#endif
    init();
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 426ms
memory: 46692kb

input:

3
199997 200000
4147 247 11 1 19 1 11 11 13 19 377 377 4147 319 19 11 13 29 13 1 19 13 11 6061 13 143 551 4147 247 13 29 6061 13 319 377 2717 29 11 247 319 551 247 29 19 551 11 13 377 29 377 19 1 2717 247 1 29 11 1 19 143 29 11 377 143 4147 2717 4147 247 7163 1 209 551 13 1 1 551 13 143 209 143 13 1...

output:

No
No
No
No
Yes
Yes
Yes
Yes
No
Yes
No
Yes
Yes
No
No
No
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
No
Yes
No
Yes
No
No
Yes
No
Yes
Yes
Yes
Yes
No
No
Yes
Yes
Yes
No
Yes
No
No
Yes
No
Yes
Yes
No
No
No
Yes
No
No
No
No
Yes
No
No
Yes
No
Yes
Yes
No
Yes
No
Yes
Yes
No
Yes
Yes
No
No
No
No
Yes
Yes
No
Yes
No
Yes
No...

result:

ok 599998 lines