Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- class RangeGcdST
- {
- public:
- int n;
- vector<int> v, tree;
- void build(int low, int high, int ind)
- {
- if (low == high)
- {
- tree[ind] = v[low];
- return;
- }
- int mid = (low + high) >> 1;
- build(low, mid, 2 * ind + 1);
- build(mid + 1, high, 2 * ind + 2);
- tree[ind] = __gcd(tree[2 * ind + 1], tree[2 * ind + 2]);
- }
- RangeGcdST(vector<int> &v)
- {
- this->v = v;
- this->n = v.size();
- tree.resize(4 * n);
- this->build(0, n - 1, 0);
- }
- int query(int low, int high, int l, int r, int ind)
- {
- int mid = (low + high) >> 1;
- if (low > r || high < l)
- {
- return 0;
- }
- if (low >= l && high <= r)
- {
- return tree[ind];
- }
- int left = query(low, mid, l, r, 2 * ind + 1);
- int right = query(mid + 1, high, l, r, 2 * ind + 2);
- return __gcd(left, right);
- }
- };
- class RangeMaxST
- {
- public:
- int n;
- vector<int> v, tree;
- void build(int low, int high, int ind)
- {
- if (low == high)
- {
- tree[ind] = v[low];
- return;
- }
- int mid = (low + high) >> 1;
- build(low, mid, 2 * ind + 1);
- build(mid + 1, high, 2 * ind + 2);
- tree[ind] = max(tree[2 * ind + 1], tree[2 * ind + 2]);
- }
- RangeMaxST(vector<int> &v)
- {
- this->v = v;
- this->n = v.size();
- tree.resize(4 * n);
- this->build(0, n - 1, 0);
- }
- int query(int low, int high, int l, int r, int ind)
- {
- int mid = (low + high) >> 1;
- if (low > r || high < l)
- {
- return LLONG_MIN;
- }
- if (low >= l && high <= r)
- {
- return tree[ind];
- }
- int left = query(low, mid, l, r, 2 * ind + 1);
- int right = query(mid + 1, high, l, r, 2 * ind + 2);
- return max(left, right);
- }
- };
- void fast()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- }
- void solve()
- {
- int n;
- cin >> n;
- vector<int> v(n);
- for (int i = 0; i < n; i++)
- {
- cin >> v[i];
- }
- RangeGcdST st1(v);
- RangeMaxST st2(v);
- int G;
- cin >> G;
- map<pair<int, int>, int> mp;
- int maxans = 0;
- while (G--)
- {
- int l, r;
- cin >> l >> r;
- l--;
- r--;
- int gcd = st1.query(0, n - 1, l, r, 0);
- int maxi = st2.query(0, n - 1, l, r, 0);
- int val = maxi / gcd;
- mp[{l + 1, r + 1}] = val;
- maxans = max(maxans, val);
- }
- for (auto i : mp)
- {
- if (i.second == maxans)
- {
- cout << i.first.first << ' ' << i.first.second << ' ' << maxans << endl;
- return;
- }
- }
- }
- signed main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int t = 1;
- cin >> t;
- while (t--)
- {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment