1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<set> #include<bitset> #include<map>
#include<cstdio> #include <iomanip> #include <unordered_map> #pragma GCC optimize(2) #define up(i,a,b) for(int i=a;i<b;i++) #define dw(i,a,b) for(int i=a;i>b;i--) #define upd(i,a,b) for(int i=a;i<=b;i++) #define dwd(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll; #define ld long double typedef unsigned long long ull; const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; const int mod = 1e9+7; inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll g = exgcd(b,a%b,y,x);y-=a/b*x;return g;} inline ll quick_pow(ll a,ll b,ll mod){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;} inline ll quick_pow(ll a,ll b){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;} inline ll inv(ll x){return quick_pow(x,mod-2);} inline ll inv(ll x,ll mod){return quick_pow(x,mod-2,mod);} inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll read() { char ch = getchar(); ll x = 0, f = 1; while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } typedef pair<int, int> pir; #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lrt root<<1 #define rrt root<<1|1 const int N=2e5+10; ll n,m; vector<ll>fac[N]; vector<ll>p[N]; ll cnt[N*2]; ll ans[2*N]; ll a[N*2]; vector<ll>vec[N*2]; vector<ll>son[N*2]; ll lastans=0; unordered_map<ll,ll>unmap[N*2]; void insert(int rt,ll val){ unmap[rt][val]++; for(auto k:p[val]){ ans[rt]+=unmap[rt][k]; lastans+=unmap[rt][k]; } } struct bcj{ int height[2*N]; int par[2*N]; void init() { upd(i, 0, 2 * N - 10) { par[i]=i; height[i]=1; } } int f(int x){ return x==par[x]?x:par[x]=f(par[x]); } bool same(int i,int j){ return f(i)==f(j); } bool merge(int x,int y){ x=f(x); y=f(y); if(x==y)return 0; if(height[y]>height[x]) swap(x,y); for(auto k:son[y]){ son[x].push_back(k); insert(x,a[k]); } unmap[y].clear(); par[y]=x; height[x]+=height[y]; return 1; } }B; void init(){ for(int i=2;i<N;i++){ int m = sqrt(i); fac[i].push_back(1); for(int j=2;j<=m;j++){ if(i%j==0){ fac[i].push_back(j); if(1ll*j*j!=i)fac[i].push_back(i/j); } } } }
int main(){ n=read(),m=read(); init(); B.init(); int x,y; upd(i,1,n){ x=read();vec[i].push_back(x); son[i].push_back(i); unmap[i][x]++; a[i]=x; } for(int i=2;i<N;i++){ int len = fac[i].size(); for(int j=0;j<len;j++){ int l = i^fac[i][j]; if(gcd(i,l)==(i^l)){ p[i].push_back(l); } } } int op; while(m--){ op=read(); if(op==2){ x=read(),y=read(); if(B.same(x,y)){ printf("%lld\n",lastans); continue; } if(B.height[B.f(y)]>B.height[B.f(x)])swap(x,y); lastans-=ans[B.f(y)]; ans[B.f(y)]=0; B.merge(x,y); printf("%lld\n",lastans); } else if(op==1){ x=read(),y=read(); a[x]=y; son[x].push_back(x); unmap[x][y]++; printf("%lld\n",lastans); }else { x=read(),y=read(); int fa=B.f(x); for(auto k : p[a[x]]){ lastans-=unmap[fa][k]; ans[fa]-=unmap[fa][k]; } unmap[fa][a[x]]--; insert(fa,y ); a[x]=y; printf("%lld\n",lastans); } } return 0; };
|