/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 35ms 788.0 KiB
#4 Accepted 6ms 1.02 MiB
#5 Accepted 6ms 1.27 MiB
#6 Accepted 42ms 656.0 KiB
#7 Accepted 6ms 788.0 KiB
#8 Accepted 7ms 788.0 KiB
#9 Accepted 91ms 1.793 MiB
#10 Accepted 132ms 840.0 KiB
#11 Accepted 13ms 644.0 KiB
#12 Accepted 10ms 1.77 MiB
#13 Accepted 13ms 2.07 MiB
#14 Accepted 16ms 2.27 MiB
#15 Accepted 621ms 14.543 MiB
#16 Accepted 578ms 9.668 MiB
#17 Accepted 546ms 5.434 MiB

Code

#include <bits/stdc++.h>

using namespace std;

struct dsu {
  vector<int> par, rank, sz;
  int cnt;
  dsu(int n) {
    par.resize(n);
    iota(par.begin(), par.end(), 0);
    rank.resize(n, 0);
    sz.resize(n, 1);
    cnt = n;
  }
  int find(int x) {
    return x == par[x] ? x : par[x] = find(par[x]);
  }
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y);
    par[y] = x, sz[x] += sz[y];
    if (rank[x] == rank[y]) rank[x] += 1;
    --cnt; return true;
  }
};

int Gcd(int a, int b) {
  if (a == 0) return b;
  if (b == 0) return a;
  int shift = __builtin_ctz(a | b);
  a >>= __builtin_ctz(a);
  do {
    b >>= __builtin_ctz(b);
    if (a > b) std::swap(a, b);
    b -= a;
  } while (b != 0);
  return a << shift;
}

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dsu d(n);
  map<int, vector<int>> divs;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        divs[j].push_back(i);
        if (j * j != a[i]) {
          divs[a[i] / j].push_back(i);
        }  
      }
    }
  }
  for (auto &[_, v] : divs) {
    if (_ == 1) continue;
    for (int i = 1; i < v.size(); i++) {
      d.unite(v[i], v[i - 1]);
    }
  }
  
  vector<vector<int>> who(n);
  for (int i = 0; i < n; i++) {
    who[d.find(i)].push_back(a[i]);
  }
  for (auto &v : who) {
    sort(v.rbegin(), v.rend());
  }
  int64_t cur = 0, ans = 0;
  vector<int> fu(n, 0);
  for (int i = 0; i < n; i++) {
    if (i >= k) {
      int v = d.find(i - k);
      fu[v] -= 1;
      cur -= who[v][fu[v]];
    }
    int u = d.find(i);
    cur += who[u][fu[u]];
    fu[u] += 1;
    ans = max(ans, cur);
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(!cin.tie(0));
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve(i);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1063 Another Maximum Sum in Subarray
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 10:15:47
Judged At
2025-02-17 10:15:47
Judged By
Score
100
Total Time
621ms
Peak Memory
14.543 MiB