hdu4417 Super Mario(主席树)

题目链接: Super Mario

大致题意

给你一个序列, 从0 ~ n - 1编号, 每次询问给出l, r, h, 表示询问[l, r]有多少个数≤h.

解题思路

主席树. 相当于是询问一个区间中h是第几小的数字. 模板题

AC代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define debug(a) cout << #a << " = " << a << endl;
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
int w[N];
vector<int> v;
int find(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }


struct node {
	int l, r;
	int cou;
}t[N << 5];
int root[N], ind;
int build(int a, int tl, int tr, int p) {
	int x = ++ind; t[x] = t[p];
	t[x].cou++;
	if (tl == tr) return x;
	int mid = tl + tr >> 1;
	if (a <= mid) t[x].l = build(a, tl, mid, t[p].l);
	else t[x].r = build(a, mid + 1, tr, t[p].r);
	return x;
}

int ask(int c, int tl, int tr, int p, int x) {
	if (c >= tr) return t[x].cou - t[p].cou;
	int mid = tl + tr >> 1;
	if (c <= mid) return ask(c, tl, mid, t[p].l, t[x].l);
	return t[t[x].l].cou - t[t[p].l].cou + ask(c, mid + 1, tr, t[p].r, t[x].r);
}
int main()
{
	int T; cin >> T;
	rep(tt, T) {
		ind = 0;
		v.clear(); v.push_back(-0x3f3f3f3f);

		int n, m; scanf("%d %d", &n, &m);
		rep(i, n) scanf("%d", &w[i]), v.push_back(w[i]);

		sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());

		int len = v.size() - 1;
		rep(i, n) {
			w[i] = find(w[i]);
			root[i] = build(w[i], 1, len, root[i - 1]);
		}

		v.push_back(0x3f3f3f3f);
		printf("Case %d:\n", tt);
		rep(i, m) { //由于比较懒, 询问的h没有进行离散化, 因此我们可以做点小改变.
			int l, r, c; scanf("%d %d %d", &l, &r, &c); l++, r++;
			int temp = find(c);
			if (v[temp] > c) temp--;
			if (!temp) printf("%d\n", 0);
			else printf("%d\n", ask(temp, 1, len, root[l - 1], root[r]));
		}
	}
	return 0;
}

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值