bzoj 3196: Tyvj 1730 二逼平衡树

3196: Tyvj 1730 二逼平衡树

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1415   Solved: 604
[ Submit][ Status][ Discuss]

Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:
1.查询k在区间内的排名
2.查询区间内排名为k的值
3.修改某一位值上的数值
4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)
5.查询k在区间内的后继(后继定义为大于x,且最小的数)

Input

第一行两个数 n,m 表示长度为n的有序序列和m个操作
第二行有n个数,表示有序序列
下面有m行,opt表示操作标号
若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名
若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数
若opt=3 则为操作3,之后有两个数pos,k 表示将pos位置的数修改为k
若opt=4 则为操作4,之后有三个数l,r,k 表示查询区间[l,r]内k的前驱
若opt=5 则为操作5,之后有三个数l,r,k 表示查询区间[l,r]内k的后继

Output

对于操作1,2,4,5各输出一行,表示查询结果

Sample Input

9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5

Sample Output

2
4
3
4
9

HINT

1.n和m的数据范围:n,m<=50000


2.序列中每个数的数据范围:[0,1e8]


3.虽然原题没有,但事实上5操作的k可能为负数


思路:
分块可以水过去

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

#define PB push_back

const int N = 1005;
const int INF = 0x3f3f3f3f;

int SZ;
int B_cnt;
vector<int> B[N];
int a[N * N];

int query_rank(int L, int R, int x) {
	int ret = 0;
	--L, --R;
	int LB = L / SZ + 1;
	int RB = R / SZ;
	if(LB < RB) {
		for(int i = L; i < LB * SZ; ++i) {
			if(a[i] < x) ++ret;
		}
		for(int i = RB * SZ; i <= R; ++i) {
			if(a[i] < x) ++ret;
		}
		for(int i = LB; i < RB; ++i) {
			ret += lower_bound(B[i].begin(), B[i].end(), x) - B[i].begin();
		}
	} else {
		for(int i = L; i <= R; ++i) {
			if(a[i] < x) ++ret;
		}
	}
	return ret + 1;
}

bool ok(int x, int L, int R, int rank) {
	return query_rank(L, R, x + 1) - 1 >= rank;
}

void query_x(int L, int R, int rank) {
	int l = 0, r = 1e8 + 5;
	while(l < r) {
		int mid = (l + r) >> 1;
		if(ok(mid, L, R, rank)) r = mid;
		else l = mid + 1;
	}
	printf("%d\n", r);
}

void update(int p, int x) {
	--p;
	int tmp = a[p];
	a[p] = x;
	int bl = p / SZ;
	int pos = lower_bound(B[bl].begin(), B[bl].end(), tmp) - B[bl].begin();
	B[bl][pos] = x;
	sort(B[bl].begin(), B[bl].end());
}

int query_pre(int L, int R, int x) {
	int ret = 0;
	--L, --R;
	int LB = L / SZ + 1;
	int RB = R / SZ;
	if(LB < RB) {
		for(int i = L; i < LB * SZ; ++i) {
			if(a[i] < x) ret = max(ret, a[i]);
		}
		for(int i = RB * SZ; i <= R; ++i) {
			if(a[i] < x) ret = max(ret, a[i]);
		}
		for(int i = LB; i < RB; ++i) {
			int pos = lower_bound(B[i].begin(), B[i].end(), x) - B[i].begin() - 1;
			if(pos >= 0) ret = max(ret, B[i][pos]);
		}
	} else {
		for(int i = L; i <= R; ++i) {
			if(a[i] < x) ret = max(ret, a[i]);
		}
	}
	return ret;	
}

int query_nxt(int L, int R, int x) {
	int ret = INF;
	--L, --R;
	int LB = L / SZ + 1;
	int RB = R / SZ;
	if(LB < RB) {
		for(int i = L; i < LB * SZ; ++i) {
			if(a[i] > x) ret = min(ret, a[i]);
		}
		for(int i = RB * SZ; i <= R; ++i) {
			if(a[i] > x) ret = min(ret, a[i]);
		}
		for(int i = LB; i < RB; ++i) {
			int pos = upper_bound(B[i].begin(), B[i].end(), x) - B[i].begin();
			if(pos < B[i].size()) ret = min(ret, B[i][pos]);
		}
	} else {
		for(int i = L; i <= R; ++i) {
			if(a[i] > x) ret = min(ret, a[i]);
		}
	}
	return ret;	
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	SZ = sqrt(n) + 1;
	B_cnt = n / SZ;
	for(int i = 0 ; i < n; ++i) {
		int x;
		scanf("%d", &a[i]);
		int bl = i / SZ;
		B[bl].PB(a[i]);
	}
	for(int i = 0; i <= B_cnt; ++i) {
		sort(B[i].begin(), B[i].end());
	}
	for(int i = 0; i < m; ++i) {
		int op, a, b, c;
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d%d%d", &a, &b, &c);
			printf("%d\n", query_rank(a, b, c));
		} else if(op == 2) {
			scanf("%d%d%d", &a, &b, &c);
			query_x(a, b, c);
		} else if(op == 3) {
			scanf("%d%d", &a, &b);
			update(a, b);
		} else if(op == 4) {
			scanf("%d%d%d", &a, &b, &c);
			printf("%d\n", query_pre(a, b, c));
		} else if(op == 5) {
			scanf("%d%d%d", &a, &b, &c);
			printf("%d\n", query_nxt(a, b, c));
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值