Submission #48257

#TimeUsernameProblemLanguageResultExecution timeMemory
48257DrSwadPoklon (COCI17_poklon)C++11
140 / 140
1390 ms20848 KiB
// Implementation practice (Have seen the editorial)
#include <bits/stdc++.h>
using namespace std;
#define debug(a) cout << #a << ": " << (a) << "\n"
#define RUNTIME prinf("%lf\n",(double)clock()/CLOCKS_PER_SEC);

typedef long long ll;

const int N = int(5e5) + 10;

int n, sq, q;
int a[N];
int cnt[N];
int answer[N];

struct query {
	int id, l, r, ans;
	query() {scanf("%d %d", &l, &r); l--; r--;}
	bool operator < (query& q2) {
		return r < q2.r;
	}
};

vector<query> blk[1000];

int main() {
	/*freopen("out", "w", stdout);
	freopen("in", "r", stdin);*/
	//ios::sync_with_stdio(false);cin.tie(NULL);

	scanf("%d %d", &n, &q);
	sq = (int)sqrt(n + 0.5);
	vector<int> tmp(n);
	for (int i = 0; i < n; i++) scanf("%d", a+i), tmp[i] = a[i];
	sort(tmp.begin(), tmp.end());
	for (int i = 0; i < n; i++)
		a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
	for (int i = 0; i < q; i++) {
		query qq; qq.id = i;
		blk[qq.l/sq].push_back(qq);
	}
	for (int i = 0; i < 1000; i++)
		sort(blk[i].begin(), blk[i].end());

	for (int bi = 0; bi < 1000; bi++) {
		vector<query>& blkq = blk[bi];
		int l = sq*bi, r = sq*bi, qi = 0, totq = blkq.size();
		int ans = 0;
		memset(cnt, 0, sizeof cnt);
		for (; r < n; r++) {
			cnt[a[r]]++;
			if (cnt[a[r]] == 2) ans++;
			if (cnt[a[r]] == 3) ans--;
			while (qi < totq && r == blkq[qi].r) {
				while (l != blkq[qi].l) {
					if (l < blkq[qi].l) {
						cnt[a[l]]--;
						if (cnt[a[l]] == 1) ans--;
						else if (cnt[a[l]] == 2) ans++;
						l++;
					}
					else {
						l--;
						cnt[a[l]]++;
						if (cnt[a[l]] == 2) ans++;
						else if (cnt[a[l]] == 3) ans--;
					}
				}
				answer[blkq[qi].id] = ans;
				qi++;
			}
		}
	}
	for (int i = 0; i < q; i++) printf("%d\n", answer[i]);

	//RUNTIME
	return 0;
}

Compilation message (stderr)

poklon.cpp: In function 'int main()':
poklon.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:34:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < n; i++) scanf("%d", a+i), tmp[i] = a[i];
                              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
poklon.cpp: In constructor 'query::query()':
poklon.cpp:18:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  query() {scanf("%d %d", &l, &r); l--; r--;}
           ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...