Submission #39624764
Source Code Expand
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
int n, m, a[maxn], bl[maxn];
ll ans[maxn], sum, cnt[maxn];
struct qry {
int l, r, id;
} q[maxn];
void add(int x) {
sum -= cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
cnt[x]++;
sum += cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
}
void del(int x) {
sum -= cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
cnt[x]--;
sum += cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
}
int main() {
#ifdef DEBUG
freopen("1.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int B = sqrt(n);
for(int i = 1; i <= n; i++) {
cin >> a[i];
bl[i] = (i - 1) / B + 1;
}
for(int i = 1; i <= m; i++) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + 1 + m, [&](auto x, auto y) {
return bl[x.l] != bl[y.l] ? x.l < y.l : x.r < y.r;
});
int l = 1, r = 0;
for(int i = 1; i <= m; i++) {
while(r < q[i].r) {
add(a[++r]);
}
while(l > q[i].l) {
add(a[--l]);
}
while(r > q[i].r) {
del(a[r--]);
}
while(l < q[i].l) {
del(a[l++]);
}
ans[q[i].id] = sum;
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
// while(q--) {
// int l, r;
// cin >> l >> r;
// ll ans = 0;
// for(int i = 1; i <= 2e5; i++) {
// ll x = 0;
// for(int j = l; j <= r; j++) {
// x += a[j] == i;
// }
// ans += (x * x * x - x * x * 3 + x * 2) / 6;
// }
// cout << ans << '\n';
// }
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | G - Triple Index |
| User | yanchengzhi |
| Language | C++ (GCC 9.2.1) |
| Score | 600 |
| Code Size | 1881 Byte |
| Status | AC |
| Exec Time | 620 ms |
| Memory | 10508 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 600 / 600 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, example0.txt, example1.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 52 ms | 7336 KiB |
| 001.txt | AC | 47 ms | 7336 KiB |
| 002.txt | AC | 47 ms | 7252 KiB |
| 003.txt | AC | 337 ms | 10368 KiB |
| 004.txt | AC | 76 ms | 8924 KiB |
| 005.txt | AC | 79 ms | 8956 KiB |
| 006.txt | AC | 76 ms | 8876 KiB |
| 007.txt | AC | 64 ms | 9260 KiB |
| 008.txt | AC | 77 ms | 8912 KiB |
| 009.txt | AC | 143 ms | 8968 KiB |
| 010.txt | AC | 362 ms | 9500 KiB |
| 011.txt | AC | 178 ms | 6916 KiB |
| 012.txt | AC | 150 ms | 6144 KiB |
| 013.txt | AC | 276 ms | 8780 KiB |
| 014.txt | AC | 121 ms | 8400 KiB |
| 015.txt | AC | 620 ms | 10508 KiB |
| 016.txt | AC | 379 ms | 8976 KiB |
| 017.txt | AC | 337 ms | 8892 KiB |
| 018.txt | AC | 334 ms | 8908 KiB |
| 019.txt | AC | 336 ms | 8992 KiB |
| 020.txt | AC | 336 ms | 8896 KiB |
| 021.txt | AC | 338 ms | 8960 KiB |
| 022.txt | AC | 337 ms | 8812 KiB |
| 023.txt | AC | 339 ms | 8920 KiB |
| 024.txt | AC | 338 ms | 9016 KiB |
| 025.txt | AC | 339 ms | 8904 KiB |
| 026.txt | AC | 345 ms | 8956 KiB |
| 027.txt | AC | 342 ms | 9016 KiB |
| 028.txt | AC | 342 ms | 8960 KiB |
| 029.txt | AC | 353 ms | 8908 KiB |
| 030.txt | AC | 350 ms | 8888 KiB |
| 031.txt | AC | 364 ms | 8876 KiB |
| 032.txt | AC | 401 ms | 8956 KiB |
| 033.txt | AC | 404 ms | 8992 KiB |
| 034.txt | AC | 403 ms | 8920 KiB |
| example0.txt | AC | 6 ms | 3432 KiB |
| example1.txt | AC | 2 ms | 3488 KiB |