/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 580.0 KiB
#2 Accepted 3ms 392.0 KiB
#3 Accepted 558ms 696.0 KiB
#4 Accepted 84ms 696.0 KiB
#5 Accepted 74ms 628.0 KiB
#6 Accepted 684ms 640.0 KiB
#7 Accepted 74ms 636.0 KiB
#8 Accepted 92ms 676.0 KiB
#9 Accepted 99ms 692.0 KiB
#10 Accepted 148ms 648.0 KiB
#11 Accepted 205ms 624.0 KiB
#12 Accepted 140ms 928.0 KiB
#13 Accepted 174ms 940.0 KiB
#14 Accepted 209ms 952.0 KiB
#15 Accepted 209ms 948.0 KiB
#16 Accepted 211ms 956.0 KiB
#17 Accepted 214ms 944.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;

struct DSU {
  vector<int> par, sz;
  int N;

  DSU() {}
  DSU(int N) : N(N) {
    sz.resize(N);
    fill(sz.begin(), sz.end(), 1);

    par.resize(N);
    iota(par.begin(), par.end(), 0);
  }

  void union_sets(int u, int v) {
    u = get_parent(u);
    v = get_parent(v);
    if (u == v) return;
    
    if (sz[u] < sz[v]) swap(u, v);
    par[v] = u;
    sz[u] += sz[v];
  }

  bool in_same_set(int u, int v) {
    return (get_parent(u) == get_parent(v));
  }

  int get_parent(int u) {
    if (u == par[u]) return u;
    par[u] = get_parent(par[u]);
    return par[u];
  }

  int get_size(int u) {
    return sz[get_parent(u)];
  }
};

vector<ll> primes;
void gen_primes(ll n) {
  ll i, j;
  vector<bool> s(n+1, 1);
  primes.push_back(2);
  for (i = 4; i <= n; i += 2) s[i] = 0;
  for (i = 3; i*i <= n; i += 2) {
    if (!s[i]) continue;
    for (j = i*i; j <= n; j += i) s[j] = 0;
  }
  for (i = 3; i <= n; i += 2) {
    if (s[i]) primes.push_back(i);
  }
}

int main() {
  FAST;
  gen_primes(32000);
  
  int tc = 1, ti;
  cin >> tc;

  for (ti = 1; ti <= tc; ++ti) {
    ll n, k, i, j, u, x, curr, ans;
    cin >> n >> k;

    vector<ll> a(n);
    for (i = 0; i < n; ++i) cin >> a[i];

    DSU dsu(n);

    for (u = 0; u < (int)primes.size(); ++u) {
      x = primes[u];
      j = -1;
      for (i = 0; i < n; ++i) {
        if (a[i] % x == 0) {
          if (j == -1) j = i;
            else dsu.union_sets(j, i);
        }
      }
    }

    vector<ll> col(n);
    for (i = 0; i < n; ++i) col[i] = dsu.get_parent(i);

    vector<vector<ll>> pf(n, vector<ll>(1, 0));
    for (i = 0; i < n; ++i) {
      pf[col[i]].push_back(a[i]);
    }
    for (i = 0; i < n; ++i) {
      sort(pf[i].begin()+1, pf[i].end());
      reverse(pf[i].begin()+1, pf[i].end());
      for (j = 1; j < (int)pf[i].size(); ++j) {
        pf[i][j] += pf[i][j-1];
      }
    }

    vector<ll> ptr(n, 0);
    curr = 0;

    auto add = [&](ll col) {
      curr -= pf[col][ptr[col]];
      ptr[col] += 1;
      curr += pf[col][ptr[col]];
    };

    auto remove = [&](ll col) {
      curr -= pf[col][ptr[col]];
      ptr[col] -= 1;
      curr += pf[col][ptr[col]];
    };

    for (i = 0; i < k; ++i) add(col[i]);
    ans = curr;

    for (i = k; i < n; ++i) {
      remove(col[i-k]);
      add(col[i]);
      ans = max(ans, curr);
    }

    cout << ans << "\n";
  }

  return 0;
}

Information

Submit By
Type
Submission
Problem
P1063 Another Maximum Sum in Subarray
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 08:48:56
Judged At
2024-11-11 02:43:20
Judged By
Score
100
Total Time
684ms
Peak Memory
956.0 KiB