이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using T = tuple<ll, ll, ll>;
#define int long long
#define Base 41
#define sz(a) (int)a.size()
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) x.begin() , x.end()
#define pii pair<int , int>
#define fi first
#define se second
#define Lg(x) 31 - __builtin_clz(x)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 1e5 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
int nArr, numQuery;
int a[MAX];
//(Q + N) * (logN * log(1e9))
struct SegmentTree{
struct Node{
int ans;
vector<pair<int, int>> l_r, r_l;
Node(){
ans = 0;
l_r.clear(), r_l.clear();
}
Node(int x){
l_r.clear(), r_l.clear();
l_r.emplace_back(x, 1);
r_l.emplace_back(x, 1);
ans = (x > 1);
}
friend Node operator + (const Node& a, const Node& b){
Node res;
vector<pair<int, int>> lf = a.r_l;
vector<pair<int, int>> rg = b.l_r;
res.l_r.emplace_back(0, 0);
res.r_l.emplace_back(0, 0);
REP(i, sz(lf)) REP(j, sz(rg)){
int l = lf[i].fi, lc = lf[i].se;
int r = rg[j].fi, rc = rg[j].se;
if (__gcd(l, r) > 1){
res.ans += 1ll * lc * rc;
}
}
res.ans += a.ans + b.ans;
/*--------------------------------------------------*/
res.l_r.insert(res.l_r.begin() + 1, all(a.l_r));
REP(i, sz(b.l_r)){
int lst = res.l_r.back().fi;
int g = __gcd(b.l_r[i].fi, lst);
if (lst == g) res.l_r.back().se += b.l_r[i].se;
else res.l_r.emplace_back(g, b.l_r[i].se);
}
reverse(all(res.l_r)); res.l_r.pop_back(); reverse(all(res.l_r));
/*--------------------------------------------------*/
res.r_l.insert(res.r_l.begin() + 1, all(b.r_l));
REP(i, sz(a.r_l)){
int lst = res.r_l.back().fi;
int g = __gcd(a.r_l[i].fi, lst);
if (lst == g) res.r_l.back().se += a.r_l[i].se;
else res.r_l.emplace_back(g, a.r_l[i].se);
}
reverse(all(res.r_l)); res.r_l.pop_back(); reverse(all(res.r_l));
return res;
}
};
vector<Node> st;
int n;
SegmentTree(int _n){
this -> n = _n;
st.resize(4 * n + 5);
}
void modify(int id, int l, int r, int pos, int val){
if (pos < l || pos > r) return;
if (l == r){
st[id] = Node(val);
return;
}
int mid = (l + r) >> 1;
modify(id << 1, l, mid, pos, val);
modify(id << 1 | 1, mid + 1, r, pos, val);
st[id] = st[id << 1] + st[id << 1 | 1];
}
Node query(int id, int l, int r, int u, int v){
if (u > r || v < l) return Node();
if (u <= l && v >= r) return st[id];
int mid = (l + r) >> 1;
Node ql = query(id << 1, l, mid, u, v);
Node qr = query(id << 1 | 1, mid + 1, r, u, v);
return (ql + qr);
}
void modify(int l, int r){
modify(1, 1, n, l, r);
}
int query(int l, int r){
return query(1, 1, n, l, r).ans;
}
};
void Whisper(){
cin >> nArr >> numQuery;
for (int i = 1; i <= nArr; ++i) cin >> a[i];
SegmentTree st(nArr + 5);
for (int i = 1; i <= nArr; ++i){
st.modify(i, a[i]);
}
for (int t = 0; t < numQuery; ++t){
int cmd; cin >> cmd;
if(cmd == 1){
int pos, val; cin >> pos >> val;
st.modify(pos, val);
}
else{
int l, r; cin >> l >> r;
if (l > r) swap(l, r);
cout << st.query(l, r) << '\n';
}
}
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\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... |