首页 > 其他分享 >CodeForces 1422F Boring Queries

CodeForces 1422F Boring Queries

时间:2023-03-04 14:44:13浏览次数:56  
标签:rt big int Boring ll CodeForces maxn 1422F res

洛谷传送门

CF 传送门

套路题。

考虑根号分治,\(\le \sqrt{V} = 447\) 的质因子直接暴力 ST 表维护。对于 \(> \sqrt{V}\) 的质因子每个数最多有一个。记 \(big_i\) 为 \(a_i > \sqrt{V}\) 的质因子,维护 \(pre_i\) 表示上一个使得 \(big_i = big_j\) 的 \(j\)(若 \(big_i\) 不存在则忽略),则所求即为 \(\prod\limits_{i \in [l,r] \land pre_i \le l - 1} big_i\)。随便主席树维护即可。

时间复杂度 \(O(kn \log n)\),其中 \(k = \sum\limits_{i \le \sqrt{V} \and i \in prime} 1\)。

code
// Problem: F. Boring Queries
// Contest: Codeforces - Codeforces Round 675 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1422/F
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 100100, logn = 18;
const int N = 200000, B = 447, K = 86;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

int n, m, a[maxn], pr[maxn], tot, lg[maxn], big[maxn], pre[maxn];
ll pw[K + 5][100];
bool vis[maxn << 1];
vector<int> vc[maxn];

struct ST {
	char f[maxn][logn];
	
	void init(int p) {
		for (int i = 1; i <= n; ++i) {
			int x = a[i];
			while (x % p == 0) {
				x /= p;
				++f[i][0];
			}
		}
		for (int j = 1; (1 << j) <= n; ++j) {
			for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
				f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
			}
		}
	}
	
	inline int query(int l, int r) {
		int k = lg[r - l + 1];
		return (int)max(f[l][k], f[r - (1 << k) + 1][k]);
	}
} st[87];

void init() {
	for (int i = 2; i <= N; ++i) {
		if (!vis[i]) {
			pr[++tot] = i;
		}
		for (int j = 1; j <= tot && i * pr[j] <= N; ++j) {
			vis[i * pr[j]] = 1;
			if (i % pr[j] == 0) {
				break;
			}
		}
	}
}

int ls[maxn << 6], rs[maxn << 6], rt[maxn], ntot;
ll tree[maxn << 6];

int build(int l, int r) {
	int rt = ++ntot;
	tree[rt] = 1;
	if (l == r) {
		return rt;
	}
	int mid = (l + r) >> 1;
	ls[rt] = build(l, mid);
	rs[rt] = build(mid + 1, r);
	return rt;
}

int update(int rt, int l, int r, int x) {
	int dir = ++ntot;
	ls[dir] = ls[rt];
	rs[dir] = rs[rt];
	tree[dir] = tree[rt] * big[x] % mod;
	if (l == r) {
		return dir;
	}
	int mid = (l + r) >> 1;
	x <= mid ? (ls[dir] = update(ls[rt], l, mid, x)) : (rs[dir] = update(rs[rt], mid + 1, r, x));
	return dir;
}

ll query(int rt, int l, int r, int ql, int qr) {
	if (ql <= l && r <= qr) {
		return tree[rt];
	}
	int mid = (l + r) >> 1;
	ll res = 1;
	if (ql <= mid) {
		res = res * query(ls[rt], l, mid, ql, qr) % mod;
	}
	if (qr > mid) {
		res = res * query(rs[rt], mid + 1, r, ql, qr) % mod;
	}
	return res;
}

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		int x = a[i];
		for (int j = 1; j <= K; ++j) {
			while (x % pr[j] == 0) {
				x /= pr[j];
			}
		}
		if (x != 1) {
			big[i] = x;
		}
	}
	scanf("%d", &m);
	for (int i = 2; i <= n; ++i) {
		lg[i] = lg[i >> 1] + 1;
	}
	for (int i = 1; i <= K; ++i) {
		st[i].init(pr[i]);
	}
	map<int, int> mp;
	for (int i = 1; i <= n; ++i) {
		if (big[i]) {
			pre[i] = mp[big[i]];
			mp[big[i]] = i;
			vc[pre[i]].pb(i);
		}
	}
	int tr = build(1, n);
	for (int i = 0; i < n; ++i) {
		rt[i] = (i ? rt[i - 1] : tr);
		for (int x : vc[i]) {
			rt[i] = update(rt[i], 1, n, x);
		}
	}
	for (int i = 1; i <= K; ++i) {
		pw[i][0] = 1;
		for (int j = 1; j < 100; ++j) {
			pw[i][j] = pw[i][j - 1] * pr[i] % mod;
		}
	}
	ll lstans = 0;
	while (m--) {
		int l, r;
		scanf("%d%d", &l, &r);
		l = (l + lstans) % n + 1;
		r = (r + lstans) % n + 1;
		if (l > r) {
			swap(l, r);
		}
		ll ans = query(rt[l - 1], 1, n, l, r);
		for (int i = 1; i <= K; ++i) {
			ans = ans * pw[i][st[i].query(l, r)] % mod;
		}
		printf("%lld\n", lstans = ans);
	}
}

int main() {
	init();
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:rt,big,int,Boring,ll,CodeForces,maxn,1422F,res
From: https://www.cnblogs.com/zltzlt-blog/p/17178272.html

相关文章

  • Codeforces Round #855 (Div. 3) E1 E2
    E1.UnforgivableCurse(easyversion)https://codeforces.com/contest/1800/problem/E1题目大意:这是这个问题的一个简单版本。在这个版本中,k始终是3。有两个长度为......
  • Codeforces Round 853 (Div. 2)(C,D)
    CodeforcesRound853(Div.2)(C,D)CC题目大意就是给你\(n\)个数,我们可以按顺序得到接下来的\(m\)个序列,每一个操作是对前面一个序列的第\(p\)个数变成\(v\),保证每次变......
  • Codeforces Round #277 (Div. 2) A B
    http://codeforces.com/contest/486/problem/AA.CalculatingFunctiontimelimitpertest1secondm......
  • Codeforces Round #279 (Div. 2) A B C
    http://codeforces.com/contest/490/problem/AA题贪心水题A.TeamOlympiadtimelimitpertest1secondmemorylimitpertest256......
  • Codeforces Round #277.5 (Div. 2) C
    题目: http://codeforces.com/contest/489/problem/CC.GivenLengthandSumofDigits...timelimitpertest1secondmemorylimitper......
  • Codeforces Round #266 (Div. 2) C
    http://codeforces.com/contest/466/problem/CC.NumberofWaystimelimitpertest2secondsmemorylimitpertest256megabytes......
  • Codeforces Round #271 (Div. 2) A B D
    http://codeforces.com/contest/474A水题枚举每个字符即可A.Keyboardtimelimitpertest2secondsmemorylimitpertest256mega......
  • Codeforces Round #280 (Div. 2) A B C
    http://codeforces.com/contest/492A水题A.VanyaandCubestimelimitpertest1secondmemorylimitpertest256megabytes......
  • Codeforces Round #281 (Div. 2) A B
    http://codeforces.com/contest/493A第一次结构体开二维数组。。。A.VasyaandFootballtimelimitpertest2secondsmemorylimitper......
  • Codeforces Round #850 (Div. 2, based on VK Cup 2022 - Final Round)
    Preface补题,之前由于要准备开学考(其实只是临时抱佛脚罢了),所以好久没写题不过索性学校题目简单,微积分线代C程都满绩了(甚至溢出好多),思政被卡了一分满绩点,而大英不出所料3.7......