結果

問題 No.3284 Picnic with Friends
ユーザー Iroha_3856
提出日時 2025-09-26 21:40:10
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,259 ms / 7,000 ms
コード長 3,574 bytes
コンパイル時間 6,042 ms
コンパイル使用メモリ 335,508 KB
実行使用メモリ 125,256 KB
最終ジャッジ日時 2025-09-26 21:41:26
合計ジャッジ時間 64,440 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint = atcoder::modint998244353;
// using mint = double;

#define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++)
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define siz(x) (int)(x).size()

template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const int inf = 1e9;
const ll INF = 4e18;

template<class T> using pq = priority_queue<T, vector<T>, less<T>>;
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

vector<int> di = {0, 0, 1, -1};
vector<int> dj = {1, -1, 0, 0};

struct Edge {
    int to, cost;
};

//1次元累積和
//全て0-indexedで処理
template<class T>
struct CumulativeSum {
    int siz;
    vector<T> S;
    bool done;
    CumulativeSum() : CumulativeSum(0) {}
    CumulativeSum(int N) : CumulativeSum(vector<T>(N, 0)) {}
    //累積和の構築はしない
    CumulativeSum(vector<T> A) {
        done = false;
        siz = (int)A.size();
        S.resize(siz+1);
        S[0] = 0;
        for (int i = 0; i < siz; i++) {
            S[i+1] = A[i];
        }
    }
    //累積
    void build() {
        assert(!done);
        for (int i = 1; i <= siz; i++) {
            S[i] += S[i-1];
        }
        done = true;
    }
    //加算(累積前のみ)
    T add(int idx, T a) {
        assert(!done);
        return S[idx+1] += a;
    }
    //代入(累積前のみ)
    T set(int idx, T a) {
        assert(!done);
        return S[idx+1] = a;
    }
    //取得(累積前、累積後両方とも可能だが、どちらを想定するかをexpected_doneで渡す)
    [[nodiscard]]
    T get(int idx, bool expected_done) {
        assert(expected_done == done);
        return S[idx+1];
    }
    //区間和取得(累積後のみ)
    //半開区間で与える
    [[nodiscard]]
    T sum(int l, int r) {
        assert(done);
        return S[r]-S[l];
    }
};

void solve() {
    int N; cin >> N;
    vector<ll> S(N);
    rep(i, 0, N) cin >> S[i];
    int Q; cin >> Q;
    vector<ll> lens;
    ll pre = -INF;
    ll clen = 0;
    rep(i, 0, Q) {
        ll t, f; cin >> t >> f;
        if (pre >= t) {
            pre += f;
            clen += f;
        }
        else {
            if (clen != 0) lens.push_back(clen);
            clen = f;
            pre = t + f;
        }
    }
    if (clen != 0) lens.push_back(clen);
    // for (ll l : lens) cout << l << " ";
    // cout << endl;
    
    //フレンズ多すぎ!通るのこれ

    ll B = 10'000'000;
    vector<int> cnt(B+1);
    vector<ll> biglens;
    for (ll len : lens) {
        if (len <= B) cnt[len]++;
        else biglens.push_back(len);
    }
    CumulativeSum<int> C(cnt);
    C.build();
    //Sをdistinctにする
    map<ll, ll> mp;
    rep(i, 0, N) {
        //やる
        if (mp.find(S[i]) != mp.end()) {
            cout << mp[S[i]] << '\n';
            continue;
        }
        ll ans = 0;
        for (int b = 1; b <= B / S[i]; b++) {
            ll l = b * S[i], r = min((b + 1) * S[i], B+1);
            ans += (ll)b * C.sum(l, r);
        }
        for (ll len : biglens) ans += len / S[i];
        mp[S[i]] = ans;
        cout << ans << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
}
0