제출 #240049

#제출 시각아이디문제언어결과실행 시간메모리
240049VimmerGaraža (COCI17_garaza)C++14
0 / 160
4077 ms41848 KiB
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("no-stack-protector") #define F first #define S second #define sz(x) int(x.size()) #define pb push_back #define N 101001 #define M ll(1e9 + 7) #define inf 1e9 + 1e9 using namespace std; //using namespace __gnu_pbds; typedef long double ld; typedef long long ll; typedef short int si; typedef array <int, 3> a3; //typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; struct st { vector <pair <ll, ll> > pr, sf; ll ans; }; ll n, q, a[N]; st t[N * 4]; st combine(st a, st b) { st res; res.pr = a.pr; for (ll i = 0; i < sz(b.pr); i++) { pair <ll, ll> cur = b.pr[i]; ll g = __gcd(res.pr.back().F, cur.F); if (g == res.pr.back().F) res.pr.back().S += cur.S; else res.pr.pb({g, cur.S}); } res.sf = b.sf; for (ll i = sz(a.sf) - 1; i >= 0; i--) { pair <ll, ll> cur = a.sf[i]; ll g = __gcd(res.sf.back().F, cur.F); if (g == res.sf.back().F) res.sf.back().S += cur.S; else res.sf.pb({g, cur.S}); } res.ans = a.ans + b.ans; ll pr[sz(b.pr)]; pr[0] = b.pr[0].S; for (ll i = 1; i < sz(b.pr); i++) pr[i] = pr[i - 1] + b.pr[i].S; ll j = sz(b.pr) - 1; for (ll i = 0; i < sz(a.sf); i++) { pair <ll, ll> cur = a.sf[i]; while (1) { if (j == -1) break; ll g = __gcd(cur.F, b.pr[j].F); if (g == 1) j--; else break; } if (j == -1) break; res.ans += pr[j] * cur.S; } return res; } void build(ll v, ll tl, ll tr) { if (tl == tr) { t[v].ans = ll(a[tl] > 1); t[v].pr.pb({a[tl], 1}); t[v].sf.pb({a[tl], 1}); return; } ll md = (tl + tr) >> 1; build(v + v, tl, md); build(v + v + 1, md + 1, tr); t[v] = combine(t[v + v], t[v + v + 1]); } void upd(ll v, ll tl, ll tr, ll pos) { if (tl == tr) { t[v].ans = bool(a[tl] > 1); t[v].pr.clear(); t[v].sf.clear(); t[v].pr.pb({a[tl], 1}); t[v].sf.pb({a[tl], 1}); return; } ll md = (tl + tr) >> 1; if (pos <= md) upd(v + v, tl, md, pos); else upd(v + v + 1, md + 1, tr, pos); t[v] = combine(t[v + v], t[v + v + 1]); } st calc(ll v, ll tl, ll tr, ll l, ll r) { if (tl == tr) return t[v]; ll md = (tl + tr) >> 1; if (r <= md) return calc(v + v, tl, md, l, r); if (md + 1 <= l) return calc(v + v + 1, md + 1, tr, l, r); return combine(calc(v + v, tl, md, l, r), calc(v + v + 1, md + 1, tr, l, r)); } int main() { //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout); ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for (ll i = 0; i < n; i++) cin >> a[i]; build(1, 0, n - 1); for ( ;q > 0; q--) { ll tp, l, r; cin >> tp >> l >> r; if (tp == 2) cout << calc(1, 0, n - 1, l - 1, r - 1).ans << '\n'; else {l--; a[l] = r; upd(1, 0, n - 1, l); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...