# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40981 | IvanC | Poklon (COCI17_poklon) | C++14 | 5183 ms | 524288 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define LSOne(S) (S & (-S))
using namespace std;
typedef tuple<int,int,int> trinca;
const int MAXN = 5*1e5 + 10;
const int MAXV = 1e6 + 10;
deque<int> aparicoes[MAXV];
int bit[MAXV],vetor[MAXN],resposta[MAXN],N,Q;
deque<trinca> perguntas;
void update(int idx,int delta){
while(idx < MAXN){
bit[idx] += delta;
idx += LSOne(idx);
}
}
int read(int idx){
int ans = 0;
while(idx > 0){
ans += bit[idx];
idx -= LSOne(idx);
}
return ans;
}
int main(){
scanf("%d %d",&N,&Q);
for(int i = 1;i<=N;i++){
scanf("%d",&vetor[i]);
int x = vetor[i];
aparicoes[x].push_back(i);
if(aparicoes[x].size() == 2) update(i,1);
else if(aparicoes[x].size() == 3) update(i,-1);
}
for(int i = 1;i<=Q;i++){
int a,b;
scanf("%d %d",&a,&b);
perguntas.push_back(make_tuple(a,b,i));
}
sort(perguntas.begin(),perguntas.end());
for(int i = 1;i<=N;i++){
while(!perguntas.empty() && get<0>(perguntas.front()) == i){
resposta[get<2>(perguntas.front())] = read(get<1>(perguntas.front()));
perguntas.pop_front();
}
int x = vetor[i];
aparicoes[x].pop_front();
if(aparicoes[x].size() >= 1) update(aparicoes[x][0],-1);
if(aparicoes[x].size() >= 2) update(aparicoes[x][1],2);
if(aparicoes[x].size() >= 3) update(aparicoes[x][2],-1);
}
for(int i = 1;i<=Q;i++) printf("%d\n",resposta[i]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |