Submission #635814

# Submission time Handle Problem Language Result Execution time Memory
635814 2022-08-27T04:19:39 Z IWTIM Garaža (COCI17_garaza) C++14
160 / 160
2752 ms 17408 KB
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python! 
 
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
 
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
 
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
 
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 998244353;
const int MX = 2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1,0,-1,0}, dy[4]{0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
struct DSU {
	vi e; void init(int N) { e = vi(N,-1); }
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } 
	bool sameSet(int a, int b) { return get(a) == get(b); }
	int size(int x) { return -e[get(x)]; }
	bool unite(int x, int y) { // union by size
		x = get(x), y = get(y); if (x == y) return 0;
		if (e[x] > e[y]) swap(x,y);
		e[x] += e[y]; e[y] = x; return 1;
	}
};
/*
inline namespace Helpers {
	//////////// is_iterable
	// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
	// this gets used only when we can call begin() and end() on that type
	tcT, class = void> struct is_iterable : false_type {};
	tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
	                                  decltype(end(declval<T>()))
	                                 >
	                       > : true_type {};
	tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
 
	//////////// is_readable
	tcT, class = void> struct is_readable : false_type {};
	tcT> struct is_readable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cin >> declval<T&>()), istream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_readable_v = is_readable<T>::value;
 
	//////////// is_printable
	// // https://nafe.es/posts/2020-02-29-is-printable/
	tcT, class = void> struct is_printable : false_type {};
	tcT> struct is_printable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cout << declval<T>()), ostream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python! 
 
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
 
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
 
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
 
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
 
/*
inline namespace Helpers {
	//////////// is_iterable
	// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
	// this gets used only when we can call begin() and end() on that type
	tcT, class = void> struct is_iterable : false_type {};
	tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
	                                  decltype(end(declval<T>()))
	                                 >
	                       > : true_type {};
	tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
 
	//////////// is_readable
	tcT, class = void> struct is_readable : false_type {};
	tcT> struct is_readable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cin >> declval<T&>()), istream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_readable_v = is_readable<T>::value;
 
	//////////// is_printable
	// // https://nafe.es/posts/2020-02-29-is-printable/
	tcT, class = void> struct is_printable : false_type {};
	tcT> struct is_printable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cout << declval<T>()), ostream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
#define int long long
const int N = 3e5 + 5;
int tree[4 * N],a[N],lazy[4 * N],n,q,ty;
long long tree1[4 * N];
int gcd(int a, int b) {
	if (a < b) swap(a,b);
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}
void build(int node, int le, int ri) {
	if (le == ri) {
		tree[node] = a[le];
		return ;
	}
	int mid = (le + ri) / 2;
	build(2 * node,le,mid);
	build(2 * node + 1, mid + 1, ri);
	tree[node] = gcd(tree[2 * node], tree[2 * node + 1]);
}
void update(int node, int le, int ri, int idx, int val) {
	if (le > idx || ri < idx) return ;
	if (le == ri) {
		tree[node] = val;
		return ;
	}
	int mid = (le + ri) / 2;
	update(2 * node,le,mid,idx,val);
	update(2 * node + 1, mid + 1, ri, idx,val);
	tree[node] = gcd(tree[2 * node], tree[2 * node + 1]);
}
int getgcd(int node, int le, int ri, int start, int end) {
	if (le > end || ri < start) return 0;
	if (le >= start && ri <= end) {
		return tree[node];
	}
	int mid = (le + ri) / 2;
	return gcd(getgcd(2 * node,le,mid,start,end), getgcd(2 * node + 1, mid + 1, ri, start,end));
}
void push(int node, int le, int ri) {
    if (lazy[node] == -1) return ;
	tree1[node] = (ri - le + 1LL) * lazy[node];
	if (le != ri) {
		lazy[2 * node] = lazy[node];
		lazy[2 * node + 1] = lazy[node];
	}
	lazy[node] = -1;
}
void update1(int node, int le, int ri, int start, int end, int val) {
	push(node,le,ri);
	if (le > end || ri < start) return ;
	if (le >= start && ri <= end) {
		lazy[node] = val;
		push(node,le,ri);
		return ;
	}
	int mid = (le + ri) / 2;
	update1(2 * node, le, mid,start,end,val);
	update1(2 * node + 1, mid + 1, ri, start,end,val);
	tree1[node] = tree1[2 * node] + tree1[2 * node + 1];
}
long long getans1(int node, int le, int ri, int start, int end) {
	if (start > end) return 0;
	push(node,le,ri);
	if (le > end || ri < start) return 0;
	if (le >= start && ri <= end) {
		return tree1[node];
	}
	int mid = (le + ri) / 2;
	return getans1(2 * node, le,mid,start,end) + getans1(2 * node + 1, mid + 1, ri, start,end);
}
void go(int idx) {
	int le = idx;
	int ri = n;
	int ans = idx - 1;
	while (le <= ri) {
		int mid = (le + ri) / 2;
		if (getgcd(1,1,n,idx,mid) > 1) {
			ans = mid; 
			le = mid + 1;
		} else {
			ri = mid - 1;
		}
	}
	update1(1,1,n,idx,idx,ans);
} 
int fnd(int idx) {
    
	int le = idx; 
	int ri = n, ans = idx - 1;
	while (le <= ri) {
		int mid = (le + ri) / 2;
		if(getgcd(1,1,n,idx,mid) > 1) {
			le = mid + 1;
			ans = mid;
		} else {
			ri = mid - 1;
		}
	}//if (idx == 6) cout<<getgcd(1,1,n,i<<"\n";
	return ans;
}
void update(int idx, int val) {
    update(1,1,n,idx,val);
	int cur = idx;
	while (cur >= 1) {
		int ri = cur;
		int gc = getgcd(1,1,n,ri,idx);
		int l = 1;
		int r = ri, ps = ri;
		while (l <= r) {
			int md = (l + r) / 2;
			if (getgcd(1,1,n,md,idx) == gc) {
				r = md - 1;
				ps = md;
			} else l = md + 1;
		}
		int le = ps;
		// TO speed up  !!
		if (gc == 1) {
			int l = le;
			int r = ri, ps = 0;
			while (l <= r) {
				int md = (l + r) / 2;
				if (getans1(1,1,n,md,md) >= idx) {
					ps = md;
					r = md - 1;
				} else l = md + 1;
			}
			if (ps) update1(1,1,n,ps,ri,idx - 1);
		    break;
		}
		update1(1,1,n,le,ri,fnd(le));
		cur = ps - 1;
	}
}
long long get(long long le, long long ri) {
	// sum of rights
	int l = le;
	int r = ri;
	long long ans = le - 1;
	while (l <= r) {
		int md = (l + r) / 2;
		if (getans1(1,1,n,md,md) <= ri) {
			l = md + 1;
			ans = md;
		} else r = md - 1;
	}
	return ri*(ri - ans)+getans1(1,1,n,le,ans) - ri*(ri + 1LL) / 2LL + le*(le - 1LL) / 2LL + (ri - le + 1LL);
}
main() {
    cin>>n>>q;
    for (int i = 1; i <= n; i++) {
    	cin>>a[i];
	}
	for (int i = 1; i < 4 * N; i++) lazy[i] = -1;
	build(1,1,n);
	for (int i = n; i >= 1; i--) {
		go(i);
	}
	
	for (int i = 1; i <= q; i++) {
		cin>>ty;
		if (ty == 1) {
			int idx, val;
			cin>>idx>>val;
			update(idx,val);
			continue;
		}
		if (ty == 2) {
			int le, ri;
			cin>>le>>ri;
			cout<<get(le,ri)<<"\n";
		}
	}
}

Compilation message

garaza.cpp:332:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  332 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9684 KB Output is correct
2 Correct 37 ms 9872 KB Output is correct
3 Correct 81 ms 10008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 512 ms 11084 KB Output is correct
2 Correct 371 ms 11732 KB Output is correct
3 Correct 450 ms 11780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1492 ms 12276 KB Output is correct
2 Correct 1078 ms 13988 KB Output is correct
3 Correct 994 ms 13996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1495 ms 15620 KB Output is correct
2 Correct 2752 ms 15076 KB Output is correct
3 Correct 1948 ms 17408 KB Output is correct