結果

問題 No.2065 Sum of Min
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2022-09-02 23:09:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 6,428 bytes
コンパイル時間 2,223 ms
コンパイル使用メモリ 146,608 KB
実行使用メモリ 15,220 KB
最終ジャッジ日時 2023-08-10 05:32:47
合計ジャッジ時間 10,621 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 297 ms
15,220 KB
testcase_05 AC 294 ms
12,940 KB
testcase_06 AC 305 ms
14,420 KB
testcase_07 AC 305 ms
12,580 KB
testcase_08 AC 374 ms
14,772 KB
testcase_09 AC 363 ms
14,828 KB
testcase_10 AC 329 ms
12,680 KB
testcase_11 AC 329 ms
12,672 KB
testcase_12 AC 386 ms
14,628 KB
testcase_13 AC 382 ms
14,636 KB
testcase_14 AC 379 ms
14,692 KB
testcase_15 AC 390 ms
14,760 KB
testcase_16 AC 385 ms
14,800 KB
testcase_17 AC 391 ms
14,680 KB
testcase_18 AC 380 ms
14,832 KB
testcase_19 AC 383 ms
14,748 KB
testcase_20 AC 378 ms
14,528 KB
testcase_21 AC 378 ms
14,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
class BIT
{
private:
    std::vector<long long> bit;
    int siz;

public:
    BIT(int N)
    {
        siz = N;
        bit.assign(N + 1, 0);
    }
    BIT()
    {
        siz = 0;
    }
    long long& get(int id)
    {
        return bit[id];
    }
    void add(int id, long long a, long long m = (long long)8e18)
    {
        while (id <= siz) {
            bit[id] += a;
            bit[id] %= m;
            id += (id & (-id));
        }
    }
    long long sum(int id, long long m = (long long)8e18)
    {
        long long ret = 0;
        while (id) {
            ret += bit[id];
            ret %= m;
            id -= (id & (-id));
        }
        return ret;
    }
};
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Pii = pair<int, int>;
using Vp = vector<Pii>;
using VVp = vector<Vp>;
using Pl = pair<ll, int>;
using Vpl = vector<Pl>;
using VVpl = vector<Vpl>;
using tup = tuple<ll, ll, int>;
using Vt = vector<tuple<int, int, int>>;
using Pll = pair<ll, ll>;
using Vc = vector<char>;
using VVc = vector<Vc>;
template <class U>
using PQmax = priority_queue<U>;
template <class U>
using PQmin = priority_queue<U, vector<U>, greater<U>>;


//再帰で計算するトポロジカルソート
void tprsort(int u, const VVi& gr, Vi& tpr, Vi& par);
//キューで計算するトポロジカルソート
void tprsort(const VVi& gr, Vi& tpr);
ll mpow(ll x, ll n, ll m = 1e9 + 7);
ll comb(int n, int r, const Vl& kai, const Vl& fkai, ll m = 1e9 + 7);
ll gcd(ll a, ll b);
int LCA(const VVi& par, const Vi& depth, int a, int b);
Vi sccResolve(const VVi& gr);
void dijkstra(const VVi& gr, const VVl& cost, Vl& dist, int s);

int main()
{
    int N, Q;
    cin >> N >> Q;
    Vl A(N);
    for (auto& a : A) {
        cin >> a;
    }
    Vpl X(Q);
    VVp rl(N + 1);
    for (int i = 0; i < Q; i++) {
        auto& [x, j] = X[i];
        ll l, r;
        cin >> l >> r >> x;
        j = i;
        rl[--l].emplace_back(1, j);
        rl[r].emplace_back(-1, j);
    }
    for (auto v : rl) {
        sort(v.begin(), v.end());
    }
    sort(X.begin(), X.end());
    Vi iv(Q);
    for (int i = 0; i < Q; i++) {
        iv[X[i].second] = i;
    }
    Vl ans1(Q, 0), ans2(Q, 0);
    BIT my_bit1(Q + 5), my_bit2(Q + 5);
    for (int i = 0; i <= N; i++) {
        for (auto& [x, p] : rl[i]) {
            if (x == -1) {
                ans1[p] = (my_bit1.sum(iv[p] + 1) - ans1[p])
                          + X[iv[p]].first * (my_bit2.sum(iv[p] + 1) - ans2[p]);
            } else {
                ans1[p] = my_bit1.sum(iv[p] + 1);
                ans2[p] = my_bit2.sum(iv[p] + 1);
            }
        }
        if (i == N) {
            break;
        }
        int id = lower_bound(X.begin(), X.end(), Pl{A[i], -1}) - X.begin();
        my_bit1.add(id + 1, A[i]);
        my_bit2.add(id + 1, -1);
        my_bit2.add(1, 1);
    }
    for (auto a : ans1) {
        cout << a << endl;
    }
}
// Library
void tprsort(int u, const VVi& gr, Vi& tpr, Vi& par)
{
    // idx[u] = tpr.size();
    /*for (int v : gr[u]) {
        if (par[v] == u || par[v] == -1) {
            par[v] = u;
            tprsort(v, gr, tpr, par);
        }
    }
    tpr.push_back(u);*/
    stack<int> st;
    st.push(u);
    bool vis[2000020];
    for (int i = 0; i <= gr.size(); i++) {
        vis[i] = false;
    }
    while (!st.empty()) {
        int v = st.top();
        if (!vis[v]) {
            vis[v] = true;
            for (int p : gr[v]) {
                if (par[p] == -1) {
                    par[p] = v;
                    st.push(p);
                }
            }
        } else {
            tpr.push_back(v);
            st.pop();
        }
    }
}

Vi sccResolve(const VVi& gr)
{
    int N = gr.size();
    Vi tpr;
    Vi par(N, -1);
    for (int i = 0; i < N; i++) {
        if (par[i] == -1) {
            tprsort(i, gr, tpr, par);
        }
    }
    Vi ret(N, -1);
    int now = 0;
    for (int i = N - 1; i >= 0; i--) {
        int u = tpr[i];
        if (ret[u] != -1) {
            continue;
        }
        ret[u] = now;
        stack<int> st;
        st.push(u);
        while (!st.empty()) {
            int v = st.top();
            st.pop();
            for (int p : gr[v]) {
                if (ret[p] == -1) {
                    st.push(p);
                    ret[p] = now;
                }
            }
        }
        now++;
    }
    return ret;
}

ll gcd(ll a, ll b)
{
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll mpow(ll x, ll n, ll m)
{
    ll ret = 1;
    while (n) {
        if (n % 2) {
            ret *= x;
            ret %= m;
        }
        x = (x * x) % m;
        n /= 2;
    }
    return ret;
}
ll comb(int n, int r, const Vl& kai, const Vl& fkai, ll m)
{
    if (n < 0 || r < 0 || n < r) {
        return 0;
    }
    ll ret = kai[n];
    ret *= fkai[r];
    ret %= m;
    ret *= fkai[n - r];
    ret %= m;
    return ret;
}

int LCA(const VVi& par, const Vi& depth, int a, int b)
{
    if (depth[a] < depth[b]) {
        swap(a, b);
    }
    int dis = depth[a] - depth[b];
    for (int i = 19; i >= 0; i--) {
        if ((dis >> i) & 1) {
            a = par[i][a];
        }
    }
    if (a == b) {
        return a;
    }
    for (int i = 19; i >= 0; i--) {
        if (par[i][a] != par[i][b]) {
            a = par[i][a];
            b = par[i][b];
        }
    }
    return par[0][a];
}

void dijkstra(const VVi& gr, const VVl& cost, Vl& dist, int s)
{
    ll INF = (ll)1e18;
    dist.assign(gr.size(), INF);
    dist[s] = 0;
    PQmin<Pl> pque;
    pque.push(make_pair(0, s));
    while (!pque.empty()) {
        auto [L, u] = pque.top();
        pque.pop();
        while (L != dist[u] && !pque.empty()) {
            tie(L, u) = pque.top();
            pque.pop();
        }
        for (int i = 0; i < gr[u].size(); i++) {
            int v = gr[u][i];
            ll c = cost[u][i];
            if (dist[v] > c + L) {
                dist[v] = c + L;
                pque.push(make_pair(dist[v], v));
            }
        }
    }
    return;
}
0