题解 P4108【[HEOI2015]公约数数列】
2023.6.21:题解已过审,但是发现有一处复杂度计算错误,修改后重新提交。
看到这种奇怪的操作,首先想到分块。
以下记值域为 ,块长为 。
前缀 显然单调不增,而且后一个必须是前一个的因数,如果变化至少要减半。因此,我们知道,共有 个不同的前缀 。我们可以接受对这些块暴力,只需要对前缀 都相同的块能快速求答案即可。
有以上思路做铺垫,容易想到设 为第 块的左端点(含), 为第 块的右端点(含), 为第 个数所在块, 为第 块的 , 表示区间 的异或和。同时,我们对每个块维护一个 map ,表示这个块内每个值作为 第一次出现的位置。
对于查询 操作,从前往后扫描每个块,维护当前 和异或和。利用 容易判断这个块内前缀 是否发生变化。如果发生变化,我们暴力整个块检查;如果未发生变化,即求 ,其中 为此前所有块的 和异或和。暴力检查的块数为 ,这部分复杂度 ;其他块的复杂度为 。
对于单点修改操作,暴力改所在块即可。复杂度 。
取 ,总复杂度 。
这就是开头处提到的复杂度计算错误。原本我认为调用了 次 函数(见代码),复杂度应为 。但是事实上只有 次调用不为 的形式,因为前缀 只变化 次。而我们注意到, 的复杂度为 。事实上,考虑辗转相除法的过程, 的复杂度为 。因此,总复杂度为 。
//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(ll x=(y);x<=(z);x++)
#define per(x,y,z) for(ll x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
ll randint(ll L, ll R) {
uniform_int_distribution<ll> dist(L, R);
return dist(rnd);
}
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
const ll N = 1e5+5, BS = 505;
ll n, a[N], q, L[BS], R[BS], pos[N], B, tot, bgcd[BS], bxor[N];
map<ll, ll> bfirst[BS];
void init() {
B = sqrt(n);
chkmin(B, n);
chkmax(B, 1LL);
while(++tot) {
L[tot] = R[tot-1] + 1;
R[tot] = min(tot*B, n);
rep(i, L[tot], R[tot]) {
pos[i] = tot;
bgcd[tot] = __gcd(bgcd[tot], a[i]);
bxor[i] = (i == L[tot] ? 0 : bxor[i-1]) ^ a[i];
if(!bfirst[tot].count(bxor[i])) bfirst[tot][bxor[i]] = i;
}
if(R[tot] == n) break;
}
}
void modify(ll p, ll k) {
a[p] = k;
bgcd[pos[p]] = 0;
map<ll, ll>().swap(bfirst[pos[p]]);
rep(i, L[pos[p]], R[pos[p]]) {
bgcd[pos[p]] = __gcd(bgcd[pos[p]], a[i]);
bxor[i] = (i == L[pos[p]] ? 0 : bxor[i-1]) ^ a[i];
if(!bfirst[pos[p]].count(bxor[i])) bfirst[pos[p]][bxor[i]] = i;
}
}
ll query(ll x) {
ll gcd = 0, now = 0;
rep(i, 1, tot) {
if(gcd != __gcd(gcd, bgcd[i])) {
rep(j, L[i], R[i]) {
gcd = __gcd(gcd, a[j]);
now ^= a[j];
if(gcd * now == x) return j;
}
}
else {
if(x % gcd == 0 && bfirst[i].count((x/gcd)^now)) return bfirst[i][(x/gcd)^now];
now ^= bxor[R[i]];
}
}
return -1;
}
int main() {
scanf("%lld", &n);
rep(i, 1, n) scanf("%lld", &a[i]);
init();
for(scanf("%lld", &q); q; q--) {
char op[8];
scanf("%s", op);
if(op[0] == 'M') {
ll p, k;
scanf("%lld%lld", &p, &k);
modify(p+1, k);
}
else {
ll x;
scanf("%lld", &x);
ll ans = query(x);
if(ans == -1) puts("no");
else printf("%lld\n", ans-1);
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通