답안 #371003

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
371003 2021-02-25T13:26:59 Z FatihSolak Poklon (COCI17_poklon) C++17
140 / 140
3138 ms 19960 KB
#include <bits/stdc++.h>
#define N 500005
using namespace std;
int arr[N];
int ans[N];
int cnt[N];
map<int,int> mp;
int block = 700;
bool comp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
	if(a.first.first/block == b.first.first/block){
		return a.first.second < b.first.second;
	}
	return a.first.first/block < b.first.first/block;
}
int t;
int sum;
void upd(int pos,int val){
	sum -= (cnt[arr[pos]] == 2);
	cnt[arr[pos]] += val;
	sum += (cnt[arr[pos]] == 2);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n,q;
	cin >> n >> q;
	for(int i=1;i<=n;i++){
		int a;
		cin >> a;
		if(!mp[a])mp[a] = ++t;
		arr[i] = mp[a];
	}
	vector<pair<pair<int,int>,int>> query;
	for(int i=0;i<q;i++){
		int l,r;
		cin >> l >> r;
		query.push_back({{l,r},i});
	}
	sort(query.begin(), query.end(),comp);
	int l = 0,r = -1;
	for(auto u: query){
		int lq = u.first.first;
		int rq = u.first.second;
		while(l > lq){
			upd(--l,1);
		}
		while(r < rq){
			upd(++r,1);
		}
		while(l < lq){
			upd(l++,-1);
		}
		while(r > rq){
			upd(r--,-1);
		}
		ans[u.second] = sum;
	}
	for(int i=0;i<q;i++){
		cout << ans[i] << endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 4 ms 364 KB Output is correct
4 Correct 18 ms 620 KB Output is correct
5 Correct 410 ms 4064 KB Output is correct
6 Correct 412 ms 4068 KB Output is correct
7 Correct 954 ms 8288 KB Output is correct
8 Correct 1605 ms 12636 KB Output is correct
9 Correct 2307 ms 16208 KB Output is correct
10 Correct 3138 ms 19960 KB Output is correct