[BZOJ 1901]Dynamic Rankings


整体二分模板题


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std;
int n, m;
struct opt{
	int tp, l, r, k, id;
}q[maxn], q1[maxn], q2[maxn];

int a[maxn], ans[maxn];

namespace BIT{
	int t[maxn], vis[maxn], tim, n;
	#define lowbit(i) (i & ((~ i) + 1))
	void update(int pos, int val){
		if(pos == 0)return;
		for(int i = pos; i <= n; i += lowbit(i)){
			if(vis[i] != tim){
				vis[i] = tim;
				t[i] = 0;
			}
			t[i] += val;
		}
	}
	
	int ask(int pos){
		int ans = 0;
		for(int i = pos; i; i -= lowbit(i))
			if(vis[i] == tim)
				ans += t[i];
		return ans;
	}
}
int tmp[maxn];
void solve(int head, int tail, int l, int r){
	if(head > tail)return;
	if(l == r){
		for(int i = head; i <= tail; i ++)
		    if(q[i].tp == 3)
		        ans[q[i].id] = l;
		return;
	}
	BIT::tim ++;
	int mid = l + r >> 1;
	for(int i = head; i <= tail; i ++){
		if(q[i].tp == 3){
			tmp[i] = BIT::ask(q[i].r) - BIT::ask(q[i].l - 1);
			continue;
		}
		if(q[i].k > mid)continue;
		if(q[i].tp == 1)
		    BIT::update(q[i].l, 1);
		else if(q[i].tp == 2)
		    BIT::update(q[i].l, -1);
	}
	int l1 = 0, l2 = 0;
	for(int i = head; i <= tail; i ++){
		if(q[i].tp == 3){
            if(q[i].k <= tmp[i])
                q1[++ l1] = q[i];
			else{
				q[i].k -= tmp[i];
				q2[++ l2] = q[i];
			}
		}
		else{
			if(q[i].k <= mid)
			    q1[++ l1] = q[i];
			else
				q2[++ l2] = q[i];
		}
	}
	int cnt = head;
	for(int i = 1; i <= l1; i ++)
	    q[cnt ++] = q1[i];
    for(int i = 1; i <= l2; i ++)
	    q[cnt ++] = q2[i];
	solve(head, head + l1 - 1, l, mid);
	solve(head + l1, tail, mid + 1, r);
}

const int inf = 0x7fffffff;
//int h[maxn << 1], cnt;
int main(){
    int test, u, v, d;
	scanf("%d", &test);

	while(test --){
		int tot = 0, id = 0, mx = -inf, mn = inf;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i ++){
			scanf("%d", &a[i]);
			q[++ tot].tp = 1;
			q[tot].l = i;
			q[tot].k = a[i];
			mx = max(mx, a[i]);
			mn = min(mn, a[i]);
		}
		
		for(int i = 1; i <= m; i ++){
			char ch = getchar();
			for(; ch < '!'; ch = getchar());
			if(ch == 'C'){
				scanf("%d%d", &u, &v);
				q[++ tot].tp = 2;
				q[tot].l = u;
				q[tot].k = a[u];

				q[++ tot].tp = 1;
				q[tot].l = u;
				a[u] = q[tot].k = v;
				mx = max(mx, d);
				mn = min(mn, d);
			}
			else{
				scanf("%d%d%d", &u, &v, &d);
				q[++ tot].tp = 3;
				q[tot].id = ++ id;
				q[tot].l = u;
				q[tot].r = v;
				q[tot].k = d;
			}
		}
	/*	sort(h + 1, h + 1 + cnt);
		cnt = unique(h + 1, h + 1 + cnt) - h - 1;*/
		BIT::n = n;
	/*	for(int i = 1; i <= tot; i ++)
		    if(q[i].tp != 3)
		        q[i].pos = lower_bound(h + 1, h + 1 + cnt) - h;*/
		solve(1, tot, mn, mx);
		for(int i = 1; i <= id; i ++)
		    printf("%d\n", ans[i]);
	}
    return 0;
}



其实还有树套树(树状数组 + 主席树,线段树 + 平衡树)

待填坑。。

发布了142 篇原创文章 · 获赞 4 · 访问量 8万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览