Submission #636638

# Submission time Handle Problem Language Result Execution time Memory
636638 2022-08-29T20:37:00 Z MilosMilutinovic Abracadabra (CEOI22_abracadabra) C++14
0 / 100
583 ms 144604 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const int N = 200200;
int n, q;
int a[N];
int pos[N];
vector<pair<int, int>> Qs[N];
int ANS[N];
int r[N];
set<int> setik;

struct Node {
	int l, r, sum;

	Node() {}
	Node(int _l, int _r, int _sum) : l(_l), r(_r), sum(_sum) {}
};
const int M = (1 << 21);
Node tree[2 * M + 5];

Node merge(Node L, Node R) {
	return Node(L.l, R.r, L.sum + R.sum);
}
void build() {
	for (int i = 0; i < M; i++)
		tree[M + i] = Node(i, i, 0);
	for (int i = M - 1; i > 0; i--)
		tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);
}
void update(int p, int i, int v) {
	if (tree[p].l > i || tree[p].r < i) return;
	if (tree[p].l == tree[p].r) {
		tree[p].sum += v;
		return;
	}
	update(p * 2, i, v);
	update(p * 2 + 1, i, v);
	tree[p] = merge(tree[p * 2], tree[p * 2 + 1]);
}
int getPos(int p, int v) {
	//eprintf("segtree: %d %d %d\n", tree[p].l, tree[p].r, v);
	if (tree[p].l == tree[p].r) return tree[p].l;
	if (tree[p * 2].sum < v)
		return getPos(p * 2 + 1, v - tree[p * 2].sum);
	else
		return getPos(p * 2, v);
}
int getSum(int p, int r) {
	if (tree[p].r <= r) return tree[p].sum;
	if (tree[p].l > r) return 0;
	return getSum(p * 2, r) + getSum(p * 2 + 1, r);
}

void add(int x, int l) {
	eprintf("adding %d %d\n", x, l);
	setik.insert(x);
	update(1, x, l);
}
void go(int i) {
	i = pos[i];
	eprintf("OMFG GO FROM %d\n", i);
	while(i <= n && setik.find(a[i]) == setik.end()) {
		add(a[i], r[i] - i);
		i = r[i];
	}
}
int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		pos[a[i]] = i;
	}
	vector<int> stk(1, n + 1);
	for (int i = n; i >= 1; i--) {
		while(stk.size() > 1 && a[stk.back()] <= a[i])
			stk.pop_back();
		r[i] = stk.back();
		stk.push_back(i);
	}
	build();
	go(a[1]);
	for (int i = 0; i < q; i++) {
		int t, x;
		scanf("%d%d", &t, &x);
		Qs[min(t, n)].push_back(mp(x, i));
	}
	for (int i = 0; i <= n; i++) {
		for (auto& p : Qs[i]) {
			int el = getPos(1, p.first);
			eprintf("p.first = %d el = %d\n", p.first, el);
			if (p.first == 5)
				eprintf("hIIIIIII sz = %d\n", setik.size());
			ANS[p.second] = a[pos[el] + p.first - getSum(1, el - 1) - 1];
		}
		int id = getPos(1, n / 2 + 1);
		eprintf("ID = %d\n", id);
		if (getSum(1, id - 1) == n / 2)
			continue;
		int el = a[pos[id] + n / 2 - getSum(1, id - 1)];
		int new_v = n / 2 - getSum(1, id - 1) - (getSum(1, id) - getSum(1, id - 1));
		eprintf("WTF %d %d and %d\n", id, new_v, el);
		update(1, id, new_v);
		go(el);
	}
	for (int i = 0; i < q; i++)
		printf("%d\n", ANS[i]);
	return 0;
}

Compilation message

Main.cpp: In function 'void add(int, int)':
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:106:2: note: in expansion of macro 'eprintf'
  106 |  eprintf("adding %d %d\n", x, l);
      |  ^~~~~~~
Main.cpp: In function 'void go(int)':
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:112:2: note: in expansion of macro 'eprintf'
  112 |  eprintf("OMFG GO FROM %d\n", i);
      |  ^~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:146:4: note: in expansion of macro 'eprintf'
  146 |    eprintf("p.first = %d el = %d\n", p.first, el);
      |    ^~~~~~~
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:148:5: note: in expansion of macro 'eprintf'
  148 |     eprintf("hIIIIIII sz = %d\n", setik.size());
      |     ^~~~~~~
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:152:3: note: in expansion of macro 'eprintf'
  152 |   eprintf("ID = %d\n", id);
      |   ^~~~~~~
Main.cpp:26:23: warning: statement has no effect [-Wunused-value]
   26 |  #define eprintf(...) 42
      |                       ^~
Main.cpp:157:3: note: in expansion of macro 'eprintf'
  157 |   eprintf("WTF %d %d and %d\n", id, new_v, el);
      |   ^~~~~~~
Main.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   scanf("%d", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~
Main.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |   scanf("%d%d", &t, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 271 ms 132752 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 583 ms 144604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 160 ms 62552 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 271 ms 132752 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -