Submission #346739

# Submission time Handle Problem Language Result Execution time Memory
346739 2021-01-10T21:13:31 Z guka415 OGLEDALA (COI15_ogledala) C++14
41 / 100
4000 ms 200716 KB
#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define ff first
#define ss second

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int sz = 1e5 + 5;
ll m, n, q;
ll a[sz], b[sz];
int segsSize, lensSize;
pll segs[sz];
vector<pair<pll, ll>> lensAll; //len, -idx, amt

void input() {
	scanf("%lld %lld %lld", &m, &n, &q);
	foru(i, 0, n) {
		scanf("%lld", &a[i]); a[i]--;
	}
	foru(i, 0, q) {
		scanf("%lld", &b[i]); b[i]--;
	}
	int sgid = 0;
	if (a[0] != 0)segs[sgid++]={ a[0],-1 };
	foru(i, 0, n - 1) {
		if (a[i + 1] - a[i] != 1)segs[sgid++]= { a[i + 1] - a[i] - 1,i };
	}
	if (a[n - 1] != m - 1)segs[sgid++] = { m - 1 - a[n - 1],n - 1 };
	segsSize = sgid;
}

void interpretSegs() {
	ll bigsz = -1, bigamt = 0, smallamt = 0;
	ll cntsmalls[5];
	foru(idx, 0, segsSize) {
		foru(iter, 0, 5)cntsmalls[iter] = 0;
		bigsz = segs[idx].ff;
		bigamt = 1;
		smallamt = 0;
		while (bigsz) {
			if(bigsz>=5)lensAll.pb({ {bigsz, -idx}, bigamt });
			else cntsmalls[bigsz] += bigamt;
			if (smallamt) {
				if(bigsz-1>=5)lensAll.pb({ {bigsz - 1,-idx},smallamt });
				else cntsmalls[bigsz - 1] += smallamt;
			}
			ll tmpsz = bigsz >> 1, tba = 0, tsa = 0;
			if (bigsz & 1) {
				tba = (bigamt * 2 + smallamt);
				tsa = smallamt;
			}
			else {
				tba = bigamt;
				tsa = (bigamt + 2 * smallamt);
			}
			bigsz = tmpsz;
			bigamt = tba;
			smallamt = tsa;
		}
		for (int smlen = 1; smlen <= 4; smlen++)lensAll.pb({ {smlen,-idx},cntsmalls[smlen] });
	}
}

ll countSegAmount(ll initialLen, ll val) {
	if (initialLen <= val)return (initialLen == val);
	unordered_map<ll, ll> mem, mem2;
	mem[initialLen] = 1;
	ll ret = 0;
	while (!mem.empty()) {
		for (pll x : mem) {
			if (x.ff == val)ret += x.ss;
			else if (x.ff > val) {
				mem2[x.ff >> 1] += x.ss;
				mem2[(x.ff - 1) >> 1] += x.ss;
			}
		}
		mem = mem2;
		mem2.clear();
	}
	return ret;
}

inline ll findIndex(ll serialNum, ll val, ll lseg, ll rseg) {
	ll len = rseg - lseg + 1, mid = (lseg + rseg) / 2;
	if (len == val)return mid;
	ll cntLeft = countSegAmount((len - 1) / 2, val);
	if (cntLeft >= serialNum) return findIndex(serialNum, val, lseg, mid - 1);
	else return findIndex(serialNum - cntLeft, val, mid + 1, rseg);
}

inline pll findLR(int id) {
	int aid = segs[id].ss;
	if (aid == -1)return { 0,a[0] - 1 };
	else if (aid == n - 1)return { a[aid] + 1,m - 1 };
	else return { a[aid] + 1,a[aid + 1] - 1 };
}

int main() {
	fast;
	input();
	interpretSegs();
	sort(lensAll.begin(), lensAll.end(), greater<pair<pll, ll>>());
	lensSize = lensAll.size();
	int qid = 0, lenid = 0;
	ll crid = n;
	while (qid < q) {
		if (b[qid] < n)printf("%lld\n", a[b[qid++]] + 1);
		else {
			while (crid <= b[qid] && lenid < lensSize) {
				if (crid + lensAll[lenid].ss <= b[qid]) crid += lensAll[lenid++].ss;
				else {
					pll fs = findLR(-lensAll[lenid].ff.ss);
					printf("%lld\n", findIndex(b[qid++] - crid + 1, lensAll[lenid].ff.ff, fs.ff, fs.ss) + 1);
					break;
				}
			}
		}
	}
	return 0;
}

Compilation message

ogledala.cpp: In function 'void input()':
ogledala.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   30 |  scanf("%lld %lld %lld", &m, &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ogledala.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   32 |   scanf("%lld", &a[i]); a[i]--;
      |   ~~~~~^~~~~~~~~~~~~~~
ogledala.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   35 |   scanf("%lld", &b[i]); b[i]--;
      |   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 82 ms 2916 KB Output is correct
4 Correct 49 ms 3040 KB Output is correct
5 Correct 88 ms 15448 KB Output is correct
6 Correct 89 ms 15704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 684 KB Output is correct
2 Correct 47 ms 684 KB Output is correct
3 Correct 934 ms 200588 KB Output is correct
4 Correct 913 ms 200716 KB Output is correct
5 Correct 1006 ms 200652 KB Output is correct
6 Correct 1004 ms 200588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2496 ms 102128 KB Output is correct
2 Correct 2409 ms 102128 KB Output is correct
3 Execution timed out 4043 ms 200624 KB Time limit exceeded
4 Halted 0 ms 0 KB -