算法&数据结构总结——平衡树&莫队
program_xwl · · 个人记录
莫队+平衡树=能解决大部分区间问题
T1 [POI 2008] KLO-Building blocks
思路
在一段柱砖中,选那个高度是最优秀的呢?答案是柱砖高度的中位数,我是这么想到的(和杨老师的方法有点不一样):
先把这些柱砖的高度映射到数轴上,方便演示。如果只有两个点
如果是四个点,那么就是在中部的两点构成的区间最优,更多的点同理。正好是中位数的定义,所以,最优点为中位数。
看到长度为
能同时完成这些操作的,只有平衡树+线段树,直接计算即可。
代码
#include <bits/stdc++.h>
using namespace std;
struct node {int lson,rson;long long val,key,siz,sum;} treap[500005];
long long n,k,a[500005],treap_root = 0,ans,anspos,the_mid_val;
stack<int>memory;
void push_up(int pos)
{
treap[pos].siz = treap[treap[pos].lson].siz+1+treap[treap[pos].rson].siz;
treap[pos].sum = treap[treap[pos].lson].sum+treap[pos].val+treap[treap[pos].rson].sum;
return;
}
void split_treap(int root,long long val,int &rootl,int &rootr)
{
if(root == 0)
{
rootl = rootr = 0;
return;
}
if(treap[root].val <= val)
{
rootl = root;
split_treap(treap[root].rson,val,treap[root].rson,rootr);
}else
{
rootr = root;
split_treap(treap[root].lson,val,rootl,treap[root].lson);
}
push_up(root);
return;
}
int merge_treap(int rootl,int rootr)
{
if(rootl == 0 || rootr == 0) return rootl^rootr;
if(treap[rootl].key <= treap[rootr].key)
{
treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
push_up(rootl);
return rootl;
}else
{
treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
push_up(rootr);
return rootr;
}
return 0;
}
void init(void)
{
srand(time(0));
for(int i = n;i >= 1;i--) memory.push(i);
return;
}
void add_node(long long val)
{
int rootl,rootr,the_node = memory.top();
memory.pop();
treap[the_node] = {0,0,val,rand(),1,val};
split_treap(treap_root,val,rootl,rootr);
treap_root = merge_treap(merge_treap(rootl,the_node),rootr);
return;
}
void del_node(long long val)
{
int rootl,middle,rootr;
split_treap(treap_root,val,rootl,rootr);
split_treap(rootl,val-1,rootl,middle);
memory.push(middle);
middle = merge_treap(treap[middle].lson,treap[middle].rson);
treap_root = merge_treap(merge_treap(rootl,middle),rootr);
return;
}
long long get_ans(long long mid_val)
{
int rootl,middle,rootr;
long long the_ans;
split_treap(treap_root,mid_val,rootl,rootr);
split_treap(rootl,mid_val-1,rootl,middle);
the_ans = mid_val*treap[rootl].siz-treap[rootl].sum+treap[rootr].sum-mid_val*treap[rootr].siz;
treap_root = merge_treap(merge_treap(rootl,middle),rootr);
return the_ans;
}
long long kth(int root,int k)
{
if(k <= treap[treap[root].lson].siz) return kth(treap[root].lson,k);
if(k == treap[treap[root].lson].siz+1) return treap[root].val;
return kth(treap[root].rson,k-treap[treap[root].lson].siz-1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
init();
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= k;i++) add_node(a[i]);
long long mid_val = the_mid_val = kth(treap_root,(k+1)/2);
ans = get_ans(mid_val);
anspos = k;
for(int i = k+1;i <= n;i++)
{
add_node(a[i]);
del_node(a[i-k]);
mid_val = kth(treap_root,(k+1)/2);
long long the_ans = get_ans(mid_val);
if(the_ans < ans)
{
ans = the_ans;
anspos = i;
the_mid_val = mid_val;
}
}
cout << ans << '\n';
for(int i = 1;i <= anspos-k;i++) cout << a[i] << '\n';
for(int i = 1;i <= k;i++) cout << the_mid_val << '\n';
for(int i = anspos+1;i <= n;i++) cout << a[i] << '\n';
return 0;
}
T2 曼哈顿交易
思路
用莫队维护
代码
#include <bits/stdc++.h>
using namespace std;
struct problem {int l,r,k,id;} problems[100005];
struct block {int l,r,cnt;} blocks[505];
int n,m,a[100005],block_siz,ans[100005],cnt[100005],buc[100005],tmp[100005],block_id[100005];
map<int,int>to;
bool cmp(problem x,problem y)
{
if(x.l/block_siz != y.l/block_siz) return x.l < y.l;
if((x.l/block_siz)&1) return x.r < y.r;
return x.r > y.r;
}
void add(int x)
{
buc[cnt[a[x]]]--;
blocks[block_id[cnt[a[x]]]].cnt--;
cnt[a[x]]++;
buc[cnt[a[x]]]++;
blocks[block_id[cnt[a[x]]]].cnt++;
return;
}
void del(int x)
{
buc[cnt[a[x]]]--;
blocks[block_id[cnt[a[x]]]].cnt--;
cnt[a[x]]--;
buc[cnt[a[x]]]++;
blocks[block_id[cnt[a[x]]]].cnt++;
return;
}
void lisanhua(void)
{
for(int i = 1;i <= n;i++) tmp[i] = a[i];
sort(tmp+1,tmp+n+1);
int rank = 0;
for(int i = 1;i <= n;i++)
{
if(tmp[i-1] < tmp[i]) rank++;
to[tmp[i]] = rank;
}
for(int i = 1;i <= n;i++) a[i] = to[a[i]];
return;
}
void init()
{
block_siz = sqrt(n);
for(int i = 1;i <= block_siz;i++)
{
blocks[i].l = blocks[i-1].r+1;
blocks[i].r = blocks[i-1].r+block_siz;
for(int j = blocks[i].l;j <= blocks[i].r;j++) block_id[j] = i;
}
if(blocks[block_siz].r < n)
{
block_siz++;
blocks[block_siz].l = blocks[block_siz-1].r+1;
blocks[block_siz].r = n;
for(int i = blocks[block_siz].l;i <= blocks[block_siz].r;i++) block_id[i] = block_siz;
block_siz--;
}
return;
}
int kth(int k)
{
int cnt = 0,pos = -1;
for(int i = 1;i <= block_siz+1;i++)
{
cnt += blocks[i].cnt;
if(cnt >= k)
{
cnt -= blocks[i].cnt;
k -= cnt;
pos = i;
break;
}
}
if(pos == -1) return -1;
for(int i = blocks[pos].l;i <= blocks[pos].r;i++)
{
k -= buc[i];
if(k <= 0) return i;
}
return -1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
init();
for(int i = 1;i <= n;i++) cin >> a[i];
lisanhua();
for(int i = 1;i <= m;i++) cin >> problems[i].l >> problems[i].r >> problems[i].k;
for(int i = 1;i <= m;i++) problems[i].id = i;
sort(problems+1,problems+m+1,cmp);
int l = problems[1].l,r = problems[1].l-1;
for(int i = 1;i <= m;i++)
{
int pl = problems[i].l,pr = problems[i].r;
while(pl < l) add(--l);
while(r < pr) add(++r);
while(l < pl) del(l++);
while(pr < r) del(r--);
ans[problems[i].id] = kth(problems[i].k);
}
for(int i = 1;i <= m;i++) cout << ans[i] << '\n';
return 0;
}