답안 #40127

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
40127 2018-01-27T18:59:22 Z alanm Garaža (COCI17_garaza) C++14
0 / 160
2484 ms 39196 KB
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <math.h>
#include <stack>
#include <limits.h>
#include <queue>
#include <functional>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <map>
#include <deque>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#include <time.h>
#include <assert.h>
#include <fstream>
#define REP(i,N)for(long long i=0;i<(N);i++)
#define FOR(i,a,b)for(long long i=(a);i<=(b);i++)
#define FORD(i,a,b)for(long long i=a;i>=(b);i--)
#define ll long long
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define vvi vector<vector<ll> >
#define vb vector<bool>
#define pii pair<ll,ll>
#define ALL(x) (x).begin(), (x).end()
#define SZ(a)((ll)(a).size())
#define CHECK_BIT(n,x)((bool)((n)&(((ll)1)<<(x))))
#define pow2(x) ((x)*(x))
#define VELA 2000000009999999999ll
#define X first
#define Y second
using namespace std;

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	return (a%b==0?b:gcd(a%b,b));
}

struct Range {
	vii pre, suf;
	ll count;
};
vii Add(vii a, vii b) {
	vii res=a;
	res.insert(res.end(),ALL(b));
	FOR(i,1, SZ(res)-1) {
		res[i].second = gcd(res[i].second,res[i-1].second);
	}
	vii zip(1,res[0]);
	FOR(i,1 ,SZ(res)-1) {
		if (zip.back().second == res[i].second) {
			zip[SZ(zip) - 1].first = zip[SZ(zip) - 1].first + res[i].first;
		}
		else {
			zip.push_back(res[i]);
		}
	}
	return zip;
}

vii GetR(vii a) {
	vii res = a;
	reverse(ALL(res));
	return res;
}

Range Union(Range a, Range b) {
	Range res;
	res.pre = Add(a.pre, b.pre);
	res.suf = GetR(Add(GetR(b.suf), GetR(a.suf)));
	res.count = a.count + b.count;
	ll r=0;
	ll rSz=0;
	REP(l, SZ(a.suf)) {
		while (r < SZ(b.pre) && gcd(b.pre[r].second, a.suf[l].second)>1) {
			rSz += b.pre[r].first;
			r++;
		}
		res.count += (a.suf[l].first)*rSz;
	}
	return res;
}

struct Intervalac {
	vector<Range> tree;
	ll nListy = 2;
	Intervalac(ll n) {
		while (nListy < n + 2) {
			nListy *= 2;
		}
		tree = vector<Range>(2 * nListy, Range{ vii(1,{1,1}),vii(1,{1,1}),0 });
		FORD(i, nListy - 1, 1)tree[i] = Union(tree[i * 2], tree[i * 2 + 1]);
	}
	void Change(ll k, ll val) {
		k += nListy + 1;
		tree[k] = Range{ vii(1,{1,val}),vii(1,{1,val}),val>1?1:0};
		k /= 2;
		while (k > 0) {
			tree[k] = Union(tree[k*2ll],tree[k*2ll+1ll]);
			k /= 2;
		}
	}
	ll Get(ll l, ll r) {
		l += nListy;
		r += nListy + 2;
		Range resl,resR= Range{ vii(1, { 1,1 }), vii(1, {1,1}),0 };
		resl = resR;

		while (l / 2 != r / 2) {
			if (l % 2 == 0)resl = Union(resl, tree[l + 1]);
			if (r % 2 == 1)resR = Union(tree[r-1],resR);
			l /= 2;
			r /= 2;
		}
		Range res = Union(resl,resR);
		return res.count;
	}
};

int main() {
	cin.sync_with_stdio(0);
	ll n,q;
	cin >> n>>q;
	Intervalac is(n);
	vi pole(n);
	REP(i, n) {
		ll val;
		cin >> val;

		//val = (rand() % 20) + 1;
		//cout << val << " ";

		is.Change(i, val);
		pole[i] = val;
	}
	//cout << "\n";

	while (q--) {
		ll typ;
		cin >> typ;

		//typ = (rand() % 2) + 1;

		if (typ == 1) {
			ll index, val;
			//cin >> index>>val;
			//index--;

			/*index = rand() % n;
			val = (rand() % 20) + 1;
			pole[index] = val;
			cout << index+1 << " " << val+1 << "\n";*/

			is.Change(index, val);
		}
		else {
			ll l, r;
			cin >> l >> r;
			l--;
			r--;

			/*l = rand() % n;
			r = rand() % n;
			if (l > r)swap(l, r);
			cout << l + 1 << " " << r + 1 << "\n";

			ll pocet = 0;
			FOR(i, l, r) {
				FOR(j, i, r) {
					ll curr_gcd = pole[i];
					FOR(k, i, j) {
						curr_gcd = gcd(curr_gcd, pole[k]);
					}
					if (curr_gcd > 1)pocet++;
				}
			}*/
			
			ll res = is.Get(l, r);
			//if (res != pocet)
			//	cout << pocet<<"\n";
			cout << res << "\n";
		}
	}
	return 0;
}

Compilation message

garaza.cpp: In function 'int main()':
garaza.cpp:100:18: warning: 'index' may be used uninitialized in this function [-Wmaybe-uninitialized]
   k += nListy + 1;
                  ^
garaza.cpp:150:7: note: 'index' was declared here
    ll index, val;
       ^
garaza.cpp:150:14: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
    ll index, val;
              ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 12 ms 2476 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 318 ms 10168 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1141 ms 20640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2484 ms 39196 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -