Submission #610888

# Submission time Handle Problem Language Result Execution time Memory
610888 2022-07-28T17:15:01 Z racsosabe Poklon (COCI17_poklon) C++14
140 / 140
420 ms 30052 KB
#include<bits/stdc++.h>
using namespace::std;

const int N = 500000 + 5;

int n;
int q;
int a[N];
int L[N];
int ft[N];
int ans[N];
int last1[N];
int last2[N];
int last3[N];
vector<int> Q[N];

void compress(){
	set<int> S;
	for(int i = 1; i <= n; i++) S.emplace(a[i]);
	int len = 0;
	map<int, int> id;
	for(int x : S){
		id[x] = len++;
	}
	for(int i = 1; i <= n; i++) a[i] = id[a[i]];
}

void update(int pos, int val){
	while(pos <= n){
		ft[pos] += val;
		pos += (-pos) & pos;
	}
}

int get_sum(int pos){
	int res = 0;
	while(pos > 0){
		res += ft[pos];
		pos &= pos - 1;
	}
	return res;
}

void fix(int i){
	int x = a[i];
	int l1 = last1[x], l2 = last2[x], l3 = last3[x];
	if(l2){
		update(l3 + 1, -1);
		update(l2 + 1, 1);
	}
	if(l1){
		update(l2 + 1, 1);
		update(l1 + 1, -1);
	}
	last3[x] = l2;
	last2[x] = l1;
	last1[x] = i;
}

void download(int i){
	for(int at : Q[i]){
		ans[at] = get_sum(L[at]);
	}
}

void solve(){
	for(int i = 1; i <= n; i++){
		fix(i);
		download(i);
	}
}

int main(){
	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);
	compress();
	for(int i = 1; i <= q; i++){
		int r;
		scanf("%d %d", L + i, &r);
		Q[r].emplace_back(i);
	}
	solve();
	for(int i = 1; i <= q; i++) printf("%d\n", ans[i]);
	return 0;
}

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:75:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |  for(int i = 1; i <= n; i++) scanf("%d", a + i);
      |                              ~~~~~^~~~~~~~~~~~~
poklon.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   scanf("%d %d", L + i, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 12088 KB Output is correct
3 Correct 8 ms 12056 KB Output is correct
4 Correct 10 ms 12244 KB Output is correct
5 Correct 65 ms 15848 KB Output is correct
6 Correct 79 ms 15832 KB Output is correct
7 Correct 177 ms 19544 KB Output is correct
8 Correct 240 ms 22980 KB Output is correct
9 Correct 340 ms 26480 KB Output is correct
10 Correct 420 ms 30052 KB Output is correct