/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 764.0 KiB
#3 Accepted 34ms 852.0 KiB
#4 Accepted 6ms 1.02 MiB
#5 Accepted 7ms 1.27 MiB
#6 Accepted 48ms 536.0 KiB
#7 Accepted 12ms 788.0 KiB
#8 Accepted 8ms 1020.0 KiB
#9 Accepted 94ms 1.75 MiB
#10 Accepted 131ms 832.0 KiB
#11 Accepted 13ms 636.0 KiB
#12 Accepted 11ms 1.77 MiB
#13 Accepted 13ms 1.93 MiB
#14 Accepted 16ms 2.312 MiB
#15 Accepted 610ms 14.496 MiB
#16 Accepted 576ms 9.805 MiB
#17 Accepted 549ms 5.445 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 < k; i++) {
    int u = d.find(i);
    cur += who[u][fu[u]];
    fu[u] += 1;
  }
  ans = cur;
  for (int i = k; i < n; i++) {
    int u = d.find(i - k);
    fu[u] -= 1;
    cur -= who[u][fu[u]];
    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:12:31
Judged At
2025-02-17 10:12:31
Judged By
Score
100
Total Time
610ms
Peak Memory
14.496 MiB