General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
165293098 Practice:
sahaun
86D - 36 C++17 (GCC 7-32) Accepted 4086 ms 14100 KB 2022-07-22 12:50:10 2022-07-22 12:50:12
→ Source
#include <bits/stdc++.h>

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

const int X = 2e5+5;
const int Y = 1e6+5;
const int BLOCK_SIZE = sqrt(2e5);
ll a[X], freq[Y];
ll ANS;

struct Query {
    int i, l, r, block;

    Query() {}
    Query(int i, int l, int r) : i(i), l(l), r(r) {
        block = l/BLOCK_SIZE;
    }

    bool operator < (const Query& other) {
        if (block == other.block) return ((block & 1) ? (r < other.r) : (r > other.r));
        return (block < other.block);
    }
};

void reset() {
    ANS = 0;
}

void include(int i) {
    ll& x = a[i];
    ANS -= freq[x]*freq[x]*x;
    ++freq[x];
    ANS += freq[x]*freq[x]*x;
}

void exclude(int i) {
    ll& x = a[i];
    ANS -= freq[x]*freq[x]*x;
    --freq[x];
    ANS += freq[x]*freq[x]*x;
}

ll get_ans() {
    return ANS;
}

int main() {
    FAST;

    int n, m, i, x, l, r, ql, qr, qi;
    cin >> n >> m;
    reset();

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

    vector<Query> q(m);
    vector<ll> ans(m);
    for (i = 0; i < m; ++i) {
        cin >> l >> r;
        --l; --r;
        q[i] = Query(i, l, r);
    }
    sort(q.begin(), q.end());

    l = 0; r = -1;
    for (i = 0; i < m; ++i) {
        qi = q[i].i;
        ql = q[i].l;
        qr = q[i].r;

        while (l > ql) include(--l);
        while (r < qr) include(++r);
        while (l < ql) exclude(l++);
        while (r > qr) exclude(r--);

        ans[qi] = get_ans();
    }

    for (i = 0; i < m; ++i) {
        cout << ans[i] << "\n";
    }

    return 0;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details