先占坑,这题实在不会。。。。
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define SZ(X) ((int)X.size())
#define mp make_pair
#define pb push_back
#define RALL(X) X.rbegin(),X.rend()
#define ALL(X) X.begin(),X.end()
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define Pii pair<int,int>
using ll = long long ;
using ld = long double ;
const int N = 1E5 + 7;
int A[N<<2], a[N];
int n, res;
int g[N * 10];
inline void pushup(int rt)
{
A[rt] = __gcd(A[rt<<1], A[rt<<1|1]);
}
void build(int l, int r, int rt)
{
A[rt] = 0;
if(l == r) return ;
int mid = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int p, int d, int l, int r, int rt)
{
if(l == r) {
A[rt] = d;
return ;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p,d,lson);
else update(p,d,rson);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R) return A[rt];
int mid = (l + r) >> 1;
if(R <= mid) return query(L,R,lson);
if(L > mid) return query(L,R,rson);
return __gcd(query(L,R,lson), query(L,R,rson));
}
Pii queryl(int R, int v, int l, int r, int rt)
{
if(R >= r) {
if(A[rt] % v == 0) return Pii(v, l);
else if(l == r) return Pii(__gcd(A[rt], v), l);
}
int mid = (l + r) >> 1;
if(R <= mid) return queryl(R, v, lson);
Pii tmp = queryl(R, v, rson);
if(tmp.first < v) return tmp;
return queryl(R, v, lson);
}
Pii queryr(int L, int v, int l, int r, int rt)
{
if(l >= L) {
if(A[rt] % v == 0) return Pii(v, l);
else if(l == r) return Pii(__gcd(v, A[rt]), l);
}
int mid = (l + r) >> 1;
if(L > mid) return queryr(L, v, rson);
Pii tmp = queryr(L, v, lson);
if(tmp.first < v) return tmp;
return queryr(L, v, rson);
}
vector<Pii>G[2];
void getl(int i)
{
int nowg = a[i], lastg = query(1, i, 1, n, 1), x = i;
G[0].pb(mp(a[i], x));
while(nowg != lastg) {
Pii y = queryl(x, nowg, 1, n, 1);
G[0].pb(y);
x = y.second;
nowg = y.first;
}
G[0].pb(mp(nowg, 0));
}
void getr(int i)
{
int nowg = a[i] , lastg = query(i , n , 1, n, 1) , x = i ;
G[1].push_back ( Pii ( a[i] , i ) ) ;
while ( nowg != lastg ) {
Pii y = queryr (x, nowg, 1, n, 1) ;
G[1].push_back ( y ) ;
x = y.second ;
nowg = y.first ;
}
G[1].push_back(Pii(nowg , n + 1 ));
}
void add(int x, int y)
{
if(g[x] == 0) ++ res;
g[x] += y;
if(g[x] == 0) -- res;
}
void calc ( int i , int f ) {
G[0].clear () ;
G[1].clear () ;
getl ( i ) ;
getr ( i ) ;
for ( int i = 1 ; i < G[0].size () ; ++ i ) {
int x = G[0][i - 1].second - G[0][i].second ;
int gx = G[0][i - 1].first ;
//add ( gx , f * x ) ;
for ( int j = 1 ; j < G[1].size () ; ++ j ) {
int y = G[1][j].second - G[1][j - 1].second ;
int gy = G[1][j - 1].first ;
add ( __gcd ( gx , gy ) , f * x * y ) ;
}
}
}
void sovle()
{
int q;
cin >> n >> q;
build(1, n, 1);
res = 0;
for(int i = 1;i <= n;i ++) {
cin >> a[i];
update(i, a[i], 1, n, 1);
int nowg = a[i], lastg = query(1,i,1,n,1), x = i;
while(nowg != lastg) {
Pii y = queryl(x, nowg, 1, n, 1);
if(!g[nowg]) res ++;
g[nowg] += (x - y.second);
x = y.second;
nowg = y.first;
}
if(!g[nowg]) res ++;
g[nowg] += x; // 从最后一次找到的l,到第1个数gcd都为nowg,所以加上x;
}
while(q --) {
int p, v;
cin >> p >> v;
calc(p, -1);
a[p] = v;
update(p, a[p], 1, n, 1);
calc(p, 1);
cout << res << "\n";
}
}
int main()
{
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cout << fixed;
int T, Case = 0;
cin >> T;
while(T --) {
cout << "Case #" << ++Case << ":\n";
sovle();
}
return 0;
}