This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N];
inline void Add(vector < pair < int, int > > &vec, vector < pair < int, int > > &add){
int sz = vec.back().first;
for(auto it : add){
int val = __gcd(it.second, vec.back().second);
int cursz = sz + it.first;
if(val == vec.back().second){
vec.back().first = cursz;
}
else{
vec.push_back(make_pair(cursz, val));
}
}
}
struct Node{
long long ans;
vector < pair < int, int > > pref, suf;
Node(){
ans = 0;
}
Node(int x){
pref.clear();
suf.clear();
pref.push_back(make_pair(0, 0));
pref.push_back(make_pair(1, x));
suf.push_back(make_pair(0, 0));
suf.push_back(make_pair(1, x));
ans = (x != 1);
}
};
Node bad, t[4 * N];
inline Node operator + (Node a, Node b){
if(a.pref.empty()){
return b;
}
if(b.pref.empty()){
return a;
}
Node c;
c.pref = a.pref;
c.suf = b.suf;
Add(c.pref, b.pref);
Add(c.suf, a.suf);
c.ans = a.ans + b.ans;
for(int i = 1, j = (int)b.pref.size() - 1; i < (int)a.suf.size(); i++){
while(j > 0 && __gcd(a.suf[i].second, b.pref[j].second) == 1){
j -= 1;
}
c.ans += 1LL * (a.suf[i].first - a.suf[i - 1].first) * b.pref[j].first;
}
return c;
}
void build(int v, int l, int r){
if(l == r){
t[v] = Node(a[l]);
return;
}
int mid = (r + l) >> 1;
build(v + v, l, mid);
build(v + v + 1, mid + 1, r);
t[v] = t[v + v] + t[v + v + 1];
}
void update(int v, int l, int r, int pos, int x){
if(l == r){
t[v] = Node(x);
return;
}
int mid = (r + l) >> 1;
if(pos <= mid){
update(v + v, l, mid, pos, x);
}
else{
update(v + v + 1, mid + 1, r, pos, x);
}
t[v] = t[v + v] + t[v + v + 1];
}
Node get(int v, int l, int r, int tl, int tr){
if(l > r || l > tr || tl > r){
return bad;
}
if(tl <= l && r <= tr){
return t[v];
}
int mid = (r + l) >> 1;
return get(v + v, l, mid, tl, tr) + get(v + v + 1, mid + 1, r, tl, tr);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
build(1, 1, n);
while(m--){
int type;
cin >> type;
if(type == 1){
int pos, x;
cin >> pos >> x;
update(1, 1, n, pos, x);
}
else{
int l, r;
cin >> l >> r;
cout << get(1, 1, n, l, r).ans << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |