4028: [HEOI2015]公约数数列

4028: [HEOI2015]公约数数列


Description

设计一个数据结构. 给定一个正整数数列 a_0, a_1, ..., a_{n - 1},你需要支持以下两种操作:

1. MODIFY id x: 将 a_{id} 修改为 x.
2. QUERY x: 求最小的整数 p (0 <= p < n),使得 gcd(a_0, a_1, ..., a_p) * XOR(a_0, a_1, ..., a_p) = x. 其中 XOR(a_0, a_1, ..., a_p) 代表 a_0, a_1, ..., a_p 的异或和,gcd表示最大公约数。

Input

 输入数据的第一行包含一个正整数 n.

接下来一行包含 n 个正整数 a_0, a_1, ..., a_{n - 1}.
之后一行包含一个正整数 q,表示询问的个数。
之后 q 行,每行包含一个询问。格式如题目中所述。

Output

对于每个 QUERY 询问,在单独的一行中输出结果。如果不存在这样的 p,输出 no.

Sample Input

10
1353600 5821200 10752000 1670400 3729600 6844320 12544000 117600 59400 640
10
MODIFY 7 20321280
QUERY 162343680
QUERY 1832232960000
MODIFY 0 92160
QUERY 1234567
QUERY 3989856000
QUERY 833018560
MODIFY 3 8600
MODIFY 5 5306112
QUERY 148900352

Sample Output

6
0
no
2
8
8

HINT

 对于 100% 的数据,n <= 100000,q <= 10000,a_i <= 10^9 (0 <= i < n),QUERY x 中的 x <= 10^18,MODIFY id x 中的 0 <= id < n,1 <= x <= 10^9.

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 100050
#define tp t[n]
#define L(i) (i-1)*k+1
#define R(i) min(i*k,n)
#define ff() for(int i=L(x);i<=R(x);i++)
#define f(a,b,c)   for(int a=b;a<=c;a++)
using namespace std;
typedef long long LL;
int n,m,t[N],a[N],k;
LL g[N],xo[N],q;
struct Mo{LL x;int r;}b[N];
bool cmp(Mo p1,Mo p2) {return p1.x==p2.x?p1.r<p2.r:p1.x<p2.x;}
LL gcd(LL x,LL y){return y==0?x:gcd(y,x%y);}
void rebuild(int x) {g[x] = xo[x] = 0;
    ff()g[x]=gcd(g[x],a[i]);ff()xo[x]^=a[i];
    b[L(x)]=(Mo){a[L(x)],L(x)};
    f(i,L(x)+1,R(x))b[i]=(Mo){b[i-1].x^a[i],i};sort(b+L(x),b+R(x)+1,cmp); }
void modify(){int id,x;scanf("%d%d",&id,&x);a[id+1]=x;rebuild(t[id+1]);}
int lb(int l,int r,int p) {while(l<r){int mid=(l+r)>>1;b[mid].x>=p?r=mid:l=mid+1;}return l;}
void solve() {scanf("%lld",&q);LL sum=0,sg=0;
    f(i,1,tp){
        if(gcd(g[i],sg)==sg){
            if(q%sg!=0){sum^=xo[i];sg=gcd(sg,g[i]);continue;}
            LL p =(q/sg)^sum;int pos=lb(L(i),R(i),p);
            if(b[pos].x==p){printf("%d
",b[pos].r-1);return ;}} 
        else{LL p=sg,cur=sum;
            f(j,L(i),R(i)){p=gcd(p,a[j]),cur^=a[j];if(1LL*p*cur==q){printf("%d
",j-1);return;}}}
        sum^=xo[i],sg=gcd(sg,g[i]);}puts("no");}
int main() {scanf("%d",&n);k=sqrt(n);
    f(i,1,n)t[i]=(i-1)/k+1,scanf("%d",&a[i]);
    f(i,1,tp)rebuild(i);scanf("%d",&m);
    f(i,1,m){char s[10];scanf("%s",s+1);s[1]=='M'?modify():solve();}return 0;
}
原文地址:https://www.cnblogs.com/muzu/p/7898396.html