Submission #40984

#TimeUsernameProblemLanguageResultExecution timeMemory
40984IvanCPoklon (COCI17_poklon)C++14
140 / 140
362 ms15492 KiB
#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;
int bit[MAXN],vetor[MAXN],resposta[MAXN],N,Q,ultimo[MAXV],prox[MAXN],ptr,marcado[MAXV];
vector<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];
		ultimo[x] = -1;
	}
	for(int i = N;i>=1;i--){
		int x = vetor[i];
		prox[i] = ultimo[x];
		ultimo[x] = i;
	}
	for(int i =1;i<=N;i++){
		int x = vetor[i];
		if(marcado[x]) continue;
		marcado[x] = 1;
		if(prox[i] != -1){
			update(prox[i],1);
			if(prox[prox[i]] != -1){
				update(prox[prox[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(ptr < Q && get<0>(perguntas[ptr]) == i){
			resposta[get<2>(perguntas[ptr])] = read(get<1>(perguntas[ptr]));
			ptr++;
		}
		if(ptr == Q) break;
		if(prox[i] != -1){
			update(prox[i],-1);
			if(prox[prox[i]] != -1){
				update(prox[prox[i]],2);
				if(prox[prox[prox[i]]] != -1){
					update(prox[prox[prox[i]]],-1);
				}
			}
		}
	}
	for(int i = 1;i<=Q;i++) printf("%d\n",resposta[i]);
	return 0;
}

Compilation message (stderr)

poklon.cpp: In function 'int main()':
poklon.cpp:24:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&Q);
                      ^
poklon.cpp:26:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&vetor[i]);
                        ^
poklon.cpp:48:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a,&b);
                       ^
#Verdict Execution timeMemoryGrader output
Fetching results...