萌新刚学OI,主席树TLE求调
  • 板块CF840D Destiny
  • 楼主ShwStone
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/4/7 17:10
  • 上次更新2023/10/28 04:22:18
查看原帖
萌新刚学OI,主席树TLE求调
491204
ShwStone楼主2022/4/7 17:10
#include <bits/stdc++.h>
using namespace std;

int n, m, k;

const int maxn = 3e5 + 5;

struct node;
typedef node* pos;
struct node {
	pos ls, rs;
	int cnt;
	node() {
		ls = rs = NULL;
		cnt = 0;
	}
};
node buf[maxn * 19];
pos root[maxn];
int buf_cnt;

inline pos new_node() {
	pos p = buf + (++buf_cnt);
	p -> ls = p -> rs = buf;
	return p;
}

pos update(pos p, int pl, int pr, int val) {
	if (pl > pr) return buf;
	pos q = new_node();
	*q = *p; //copy
	q -> cnt++;
	if (pl == pr) return q;
	int mid = (pl + pr) >> 1;
	if (val <= mid) q -> ls = update(q -> ls, pl, mid, val);
	else q -> rs = update(q -> rs, mid + 1, pr, val);
	return q;
}

int ask(int l, int r, pos pl, pos pr, int cnt) {
	if (pl == buf && pr == buf) return -1;
	if (l == r) return l;
	int mid = (l + r) >> 1, res;
	if ((pr -> ls -> cnt - pl -> ls -> cnt) * k > cnt) {
		res = ask(l, mid, pl -> ls, pr -> ls, cnt);
		if (res != -1) return res;
	}
	if ((pr -> rs -> cnt - pl -> rs -> cnt) * k > cnt) return ask(mid + 1, r, pl -> rs, pr -> rs, cnt);
	return -1;
}

int main() {
	buf -> ls = buf -> rs = buf;
	scanf("%d %d", &n, &m);
	root[0] = buf;
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d", &x);
		root[i] = update(root[i - 1], 1, n, x);
	}
	while (m--) {
		int ll, rr;
		scanf("%d %d %d", &ll, &rr, &k);
		printf("%d\n", ask(1, n, root[ll - 1], root[rr], rr - ll + 1));
	}
	return 0;
}
2022/4/7 17:10
加载中...