Submission #41154823
Source Code Expand
#include <atcoder/all>
using namespace atcoder;
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
const double pi = 3.14159265359;
const ll INF = 1LL << 60;
template<class T> inline bool chmin(T& a, T b){ if (a > b){a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if (a < b){a = b; return true;} return false;}
//using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint; // mint::set_mod(p); later
//using mint = static_modint<1000000009>;
int B;
bool func(const pair<int, int>& lhs, const pair<int, int>& rhs){
return ((lhs.first / B < rhs.first / B) ||
(lhs.first / B == rhs.first / B && lhs.second < rhs.second));
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
// Input
ll N, Q;
cin >> N >> Q;
vector<int> a(N);
for (auto &t : a) { cin >> t; t--; }
vector<int> L(Q), R(Q);
vector<pair<int, int>> qs;
for (int i = 0; i < Q; i++){
cin >> L[i] >> R[i];
L[i]--; R[i]--;
qs.emplace_back(L[i], R[i]);
}
// Solve: Mo のアルゴリズム
B = max((int) sqrt(N*N/Q), 1);
sort(qs.begin(), qs.end(), func);
map<pair<int, int>, ll> mp;
vector<ll> hist(200000);
ll ans = 0; // 最後に6でわる
auto[l, r] = *qs.begin();
for (int i = l; i <= r; i++){
hist[a[i]]++;
}
for (int i = 0; i < 200000; i++){
if (hist[i] >= 3){
ans += hist[i] * (hist[i] - 1) * (hist[i] - 2);
}
}
for (auto p : qs){
auto [nl, nr] = p;
// 伸ばす方を先にやらないとおかしくなっちゃうかな
// 左を伸ばす
while (nl < l){
l--;
if (hist[a[l]] >= 3){
ans -= hist[a[l]] * (hist[a[l]] - 1) * (hist[a[l]] - 2);
}
hist[a[l]]++;
if (hist[a[l]] >= 3){
ans += hist[a[l]] * (hist[a[l]] - 1) * (hist[a[l]] - 2);
}
}
// 右を伸ばす
while (nr > r){
r++;
if (hist[a[r]] >= 3){
ans -= hist[a[r]] * (hist[a[r]] - 1) * (hist[a[r]] - 2);
}
hist[a[r]]++;
if (hist[a[r]] >= 3){
ans += hist[a[r]] * (hist[a[r]] - 1) * (hist[a[r]] - 2);
}
}
// 左を縮める
while (nl > l){
if (hist[a[l]] >= 3){
ans -= hist[a[l]] * (hist[a[l]] - 1) * (hist[a[l]] - 2);
}
hist[a[l]]--;
if (hist[a[l]] >= 3){
ans += hist[a[l]] * (hist[a[l]] - 1) * (hist[a[l]] - 2);
}
l++;
}
// 右を縮める
while (nr < r){
if (hist[a[r]] >= 3){
ans -= hist[a[r]] * (hist[a[r]] - 1) * (hist[a[r]] - 2);
}
hist[a[r]]--;
if (hist[a[r]] >= 3){
ans += hist[a[r]] * (hist[a[r]] - 1) * (hist[a[r]] - 2);
}
r--;
}
l = nl;
r = nr;
mp[p] = ans / 6;
}
// Print
for (int i = 0; i < Q; i++){
ll ans = mp[make_pair(L[i], R[i])];
cout << ans << "\n";
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | G - Triple Index |
| User | unnohideyuki |
| Language | C++ (GCC 9.2.1) |
| Score | 600 |
| Code Size | 2978 Byte |
| Status | AC |
| Exec Time | 656 ms |
| Memory | 21408 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 | 72 ms | 7908 KiB |
| 001.txt | AC | 64 ms | 7840 KiB |
| 002.txt | AC | 67 ms | 7848 KiB |
| 003.txt | AC | 327 ms | 20976 KiB |
| 004.txt | AC | 231 ms | 21116 KiB |
| 005.txt | AC | 239 ms | 21148 KiB |
| 006.txt | AC | 239 ms | 21036 KiB |
| 007.txt | AC | 81 ms | 9504 KiB |
| 008.txt | AC | 222 ms | 21116 KiB |
| 009.txt | AC | 266 ms | 21028 KiB |
| 010.txt | AC | 338 ms | 18764 KiB |
| 011.txt | AC | 127 ms | 10128 KiB |
| 012.txt | AC | 60 ms | 6344 KiB |
| 013.txt | AC | 270 ms | 16512 KiB |
| 014.txt | AC | 194 ms | 16924 KiB |
| 015.txt | AC | 509 ms | 20972 KiB |
| 016.txt | AC | 656 ms | 21080 KiB |
| 017.txt | AC | 477 ms | 20980 KiB |
| 018.txt | AC | 443 ms | 21056 KiB |
| 019.txt | AC | 435 ms | 21088 KiB |
| 020.txt | AC | 432 ms | 21040 KiB |
| 021.txt | AC | 427 ms | 21088 KiB |
| 022.txt | AC | 431 ms | 21060 KiB |
| 023.txt | AC | 433 ms | 21120 KiB |
| 024.txt | AC | 435 ms | 20980 KiB |
| 025.txt | AC | 431 ms | 21060 KiB |
| 026.txt | AC | 428 ms | 21084 KiB |
| 027.txt | AC | 424 ms | 21108 KiB |
| 028.txt | AC | 429 ms | 21116 KiB |
| 029.txt | AC | 428 ms | 21212 KiB |
| 030.txt | AC | 425 ms | 21220 KiB |
| 031.txt | AC | 434 ms | 21224 KiB |
| 032.txt | AC | 449 ms | 21408 KiB |
| 033.txt | AC | 448 ms | 21368 KiB |
| 034.txt | AC | 446 ms | 21328 KiB |
| example0.txt | AC | 9 ms | 4800 KiB |
| example1.txt | AC | 4 ms | 4644 KiB |