QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560332#9250. Max GCDduckindogTL 36ms5964kbC++232.0kb2024-09-12 15:10:172024-09-12 15:10:24

Judging History

你现在查看的是最新测评结果

  • [2024-09-12 15:10:24]
  • 评测
  • 测评结果:TL
  • 用时:36ms
  • 内存:5964kb
  • [2024-09-12 15:10:17]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 150'000 + 10,
          MAX = 1'000'000;
int n, q;
int a[N];
struct Query { 
  int l, r, idx;
  friend istream& operator >> (istream& is, auto& rhs) { 
    return is >> rhs.l >> rhs.r;
  }
} query[N];
int answer[N];

vector<int> pos[MAX + 10];
vector<pair<int, int>> save[N];

int bit[N];
void upd(int i, int x) { 
  for (; i <= n; i += i & -i) bit[i] = max(bit[i], x);
}
int que(int i) { 
  int ret = 0;
  for (; i; i -= i & -i) ret = max(ret, bit[i]);
  return ret;
}

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= q; ++i) cin >> query[i], query[i].idx = i;

  for (int i = 1; i <= n; ++i) pos[a[i]].push_back(i); 

  { //init
    int cnt = 0;
    for (int value = MAX; value >= 1; --value) { 
      vector<int> totalPos;
      for (int multiple = value; multiple <= MAX; multiple += value) { 
        for (const auto& x : pos[multiple]) totalPos.push_back(x);
      }
      if (totalPos.size() <= 2) continue;

      sort(totalPos.begin(), totalPos.end());
      for (auto it = totalPos.begin(); it != totalPos.end(); ++it) {
        if (*it == totalPos[0] || *it == totalPos.end()[-1]) continue;
        
        int l = *(it - 1),
            r = lower_bound(totalPos.begin(), totalPos.end(), 2 * *it - l) - totalPos.begin();
        if (r == totalPos.size()) continue;
        r = totalPos[r];
        cnt += 1;
        save[l].push_back({r, value});
      }
    }
  }

  sort(query + 1, query + q + 1, [&](const auto& a, const auto& b) { return a.l < b.l; });

  int it = n;
  for (int i = q; i >= 1; --i) { 
    const auto& [l, r, idx] = query[i];

    while (it >= l) 
      for (const auto& [r, value] : save[it--]) upd(r, value);

    answer[idx] = que(r);
  }
  
  for (int i = 1; i <= q; ++i) cout << answer[i] << "\n";

  cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 36ms
memory: 5964kb

input:

8 8
8 24 4 6 6 7 3 3
1 5
2 6
3 7
5 8
4 8
1 3
2 5
3 8

output:

4
2
3
1
3
4
2
3

result:

ok 8 tokens

Test #2:

score: -100
Time Limit Exceeded

input:

150000 100000
982800 554400 665280 982800 997920 720720 786240 831600 997920 982800 786240 982800 942480 831600 887040 665280 831600 960960 786240 982800 786240 942480 665280 831600 942480 665280 982800 960960 960960 997920 720720 960960 960960 665280 982800 665280 982800 942480 786240 997920 554400...

output:


result: