【线段树套平衡树】 ZOJ 2112 Dynamic Rankings

序列可修改。划分树不可做。。对于求第k大第k小,划分树很方便。。。但是划分树不支持修改。。所以这道题之后用线段树套平衡树来做了。其中找k小的操作可用二分做。。但是这道题对内存卡的相当严。。。刚开始用动态内存做。。MLE了。。后来改成用完树销毁树的办法还是MLE。。。最后只好改成静态模拟了。。。但是就算是静态,内存也相当不好开。。最后卡着内存飘过。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#define maxn 50005
#define eps 1e-10
#define mod 1000000009
#define INF 1e9  
#define lowbit(x) (x&(-x))  
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
using namespace std;

int n, m, ql, qr;
int loc, value, kk;
char s[10];
int num[maxn];
struct node
{
	int key, v, s;
	node *ch[2];
	void maintain(void)
	{
		s = ch[0]->s + ch[1]->s + 1;
	}
	int cmp(int x) const {
		if(x == v) return -1;
		return x < v ? 0 : 1;
	}
}*root[maxn<<2], *null, C[maxn*18], *top;

void rotate(node* &o, int d)
{
	node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d], k->ch[d] = o;
	o->maintain(), k->maintain(), o = k;
}
void insert(node* &o, int x)
{
	if(o == null) {
		o = top++;
		o->s = 1, o->v = x, o->key = rand();
		o->ch[0] = o->ch[1] = null;
		return;
	}
	int d = x < o->v ? 0 : 1;
	insert(o->ch[d], x);
	if(o->ch[d]->key > o->key) rotate(o, d^1);
	o->maintain();
}
void remove(node* &o, int x)
{
	int d = o->cmp(x);
	if(d == -1) {
		if(o->ch[0] != null && o->ch[1] != null) {
			int d2 = o->ch[0]->key > o->ch[1]->key ? 1 : 0;
			rotate(o, d2), remove(o->ch[d2], x);
		}
		else {
			//node *u = o;
			if(o->ch[0] == null) o = o->ch[1];
			else o = o->ch[0];
			//delete u;
		}
	}
	else remove(o->ch[d], x);
	if(o != null) o->maintain();
}
int kth(node *o, int k)
{
	if(o == null) return 0;
	if(o->v <= k) return o->ch[0]->s + 1 + kth(o->ch[1], k);
	else return kth(o->ch[0], k);
}
void build(int o, int L, int R)
{
	root[o] = null;
	for(int i = L; i <= R; i++) insert(root[o], num[i]);
	if(L == R) return;
	int mid = (L+R)>>1;
	build(lson);
	build(rson);
}
void updata(int o, int L, int R)
{
	remove(root[o], num[loc]);
	insert(root[o], value);
	if(L == R) return;
	int mid = (L+R)>>1;
	if(loc <= mid) updata(lson);
	else updata(rson);
}
int query(int o, int L, int R, int k)
{
	if(ql <= L && qr >= R) return kth(root[o], k);
	int mid = (L+R)>>1, ans = 0;
	if(ql <= mid) ans += query(lson, k);
	if(qr > mid) ans += query(rson, k);
	return ans;
}
void search(void)
{
	int bot = 0, top = 1e9, mid, res;
	while(top >= bot) {
		mid = (top+bot)>>1;
		if(query(1, 1, n, mid) >= kk) top = mid-1, res = mid;
		else bot = mid+1;
	}
	printf("%d\n", res);
}
void read(void)
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
		scanf("%d", &num[i]);
}
/*
void Delete(node* &o)
{
	if(o == null) return;
	if(o->ch[0] != null) Delete(o->ch[0]);
	if(o->ch[1] != null) Delete(o->ch[1]);
	delete o;
}
void DD(int o, int L, int R)
{
	Delete(root[o]);
	if(L == R) return;
	int mid = (L+R)>>1;
	DD(lson);
	DD(rson);
}
*/
void init(void)
{
	top = C;
	null = top++;
	null->v = null->s = 0;
	null->ch[0] = null->ch[1] = NULL;
}
void work(void)
{
	while(m--) {
		scanf("%s", s);
		if(s[0] == 'Q') {
			scanf("%d%d%d", &ql, &qr, &kk);
			search();
		}
		else {
			scanf("%d%d", &loc, &value);
			updata(1, 1, n);
			num[loc] = value;
		}
	}
}
int main(void)
{
	int _;
	while(scanf("%d", &_)!=EOF) {
		while(_--) {
			read();
			init();
			build(1, 1, n);
			work();
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值