Submission #39690323
Source Code Expand
#include <bits/stdc++.h>
std::vector<long long> Mo(int N,int Q,std::vector<std::pair<int,int>> X, std::function<void(int,long long&)> lm, std::function<void(int,long long&)> lp, std::function<void(int,long long&)> rm, std::function<void(int,long long&)> rp){
/*
Copyright (c) 2023 0214sh7
https://github.com/0214sh7/library/
*/
int rQ;
std::map<std::pair<int,int>,std::vector<int>> index;
std::vector<int> block(N,0);
std::vector<long long> Ans(Q);
std::function<bool(std::pair<int,int>, std::pair<int,int>)> comp = [&](std::pair<int,int> A,std::pair<int,int> B){
if(block[A.first]!=block[B.first]){
return (block[A.first]<block[B.first]);
}
if(A.second != B.second){
return (A.second < B.second);
}
return (A.first < B.first);
};
for(int i=0;i<Q;i++){
index[X[i]].push_back(i+1);
}
int q = index.size();
rQ = 1;
while((rQ+1)*(rQ+1)<=q)rQ++;
int now = 0;
for(int i=0;i<rQ-1;i++){
now += ((N+rQ-1-i)/rQ);
block[now]++;
}
for(int i=0;i<N-1;i++){
block[i+1] += block[i];
}
std::vector<std::pair<int,int>> Y(q);
now = 0;
for(auto e:index){
Y[now] = e.first;
now++;
}
std::sort(Y.begin(),Y.end(),[&](auto a, auto b){return comp(a, b);});
int L = 0,R = 0;
long long Result = 0;
for(int i=0;i<q;i++){
while(Y[i].first < L){
lm(L,Result);
L--;
}
while(R < Y[i].second){
rp(R,Result);
R++;
}
while(L < Y[i].first){
lp(L,Result);
L++;
}
while(Y[i].second < R){
rm(R,Result);
R--;
}
for(int k:index[Y[i]]){
Ans[k-1] = Result;
}
}
return Ans;
//lm(i): [i,r)を[i-1,r)にする
//lp(i): [i,r)を[i+1,r)にする
//rm(i): [i,r)を[i,r-1)にする
//rp(i): [i,r)を[i,r+1)にする
}
int main(void){
//cin.tie(nullptr);
//ios::sync_with_stdio(false);
int N,Q;
std::cin >> N >> Q;
std::vector<long long> A(N);
for(int i=0;i<N;i++){
std::cin >> A[i];
}
std::vector<std::pair<int,int>> LR(Q);
for(int i=0;i<Q;i++){
long long l,r;
std::cin >> l >> r;
l--;
LR[i] = {l,r};
}
std::vector<long long> R(200001,0);
std::function<void(int,long long&)> lm = [&](int i,long long& res){
R[A[i-1]]++;
long long t = R[A[i-1]];
res += (t-1)*(t-2)/2;
};
std::function<void(int,long long&)> lp = [&](int i,long long& res){
R[A[i]]--;
long long t = R[A[i]];
res -= t*(t-1)/2;
};
std::function<void(int,long long&)> rm = [&](int i,long long& res){
R[A[i-1]]--;
long long t = R[A[i-1]];
res -= t*(t-1)/2;
};
std::function<void(int,long long&)> rp = [&](int i,long long& res){
R[A[i]]++;
long long t = R[A[i]];
res += (t-1)*(t-2)/2;
};
std::vector<long long> Ans = Mo(N,Q,LR,lm,lp,rm,rp);
for(int i=0;i<(int)Ans.size();i++){
std::cout << Ans[i] << std::endl;
}
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Triple Index |
User |
x0214sh7 |
Language |
C++ (GCC 9.2.1) |
Score |
600 |
Code Size |
3381 Byte |
Status |
AC |
Exec Time |
1339 ms |
Memory |
35200 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 |
339 ms |
10920 KiB |
001.txt |
AC |
336 ms |
10812 KiB |
002.txt |
AC |
337 ms |
10880 KiB |
003.txt |
AC |
1080 ms |
35024 KiB |
004.txt |
AC |
632 ms |
34984 KiB |
005.txt |
AC |
615 ms |
35136 KiB |
006.txt |
AC |
604 ms |
35200 KiB |
007.txt |
AC |
387 ms |
13192 KiB |
008.txt |
AC |
610 ms |
35020 KiB |
009.txt |
AC |
615 ms |
35100 KiB |
010.txt |
AC |
958 ms |
30612 KiB |
011.txt |
AC |
355 ms |
14432 KiB |
012.txt |
AC |
152 ms |
7812 KiB |
013.txt |
AC |
768 ms |
26600 KiB |
014.txt |
AC |
581 ms |
26924 KiB |
015.txt |
AC |
1339 ms |
35048 KiB |
016.txt |
AC |
1088 ms |
35048 KiB |
017.txt |
AC |
1066 ms |
35096 KiB |
018.txt |
AC |
1048 ms |
35048 KiB |
019.txt |
AC |
1041 ms |
35196 KiB |
020.txt |
AC |
1028 ms |
35052 KiB |
021.txt |
AC |
1026 ms |
35044 KiB |
022.txt |
AC |
1030 ms |
35020 KiB |
023.txt |
AC |
1043 ms |
35040 KiB |
024.txt |
AC |
1058 ms |
34968 KiB |
025.txt |
AC |
1053 ms |
35132 KiB |
026.txt |
AC |
1069 ms |
35100 KiB |
027.txt |
AC |
1029 ms |
35196 KiB |
028.txt |
AC |
1024 ms |
35196 KiB |
029.txt |
AC |
1031 ms |
35108 KiB |
030.txt |
AC |
1026 ms |
35052 KiB |
031.txt |
AC |
1026 ms |
34964 KiB |
032.txt |
AC |
1019 ms |
35036 KiB |
033.txt |
AC |
1041 ms |
35112 KiB |
034.txt |
AC |
1048 ms |
34984 KiB |
example0.txt |
AC |
7 ms |
4624 KiB |
example1.txt |
AC |
4 ms |
4688 KiB |