您的位置:首页 > 产品设计 > UI/UE

HDU 5412 CRB and Queries && BZOJ 3110: [Zjoi2013]K大数查询 (整体二分+树状数组/线段树)

2017-08-10 08:33 537 查看

题目传送门们

HDU 5412

BZOJ 3110

题目分析

什么叫整体二分

①假如给你一个区间,让你求区间第K小,你怎么做?

=。=

排序。。或者二分答案然后检验。

②假如给你很多区间呢?

主席树。

③假如带修呢?

树套树!线段树套树状数组/treap。

其实没必要,直接上整体二分。

整体二分是普通二分的进阶版,二分答案的同时,依据与答案的关系将所有的修改和询问分成左右两边再递归求解。

整体二分可以解决求动态区间第K小的一类问题。

回到题目,两道题都是差不多的,都是修改一个或一段,然后求区间的第K小(大)的问题。解决此类问题,整体二分比树套树更好写(主要是不会写树套树…)。

整体二分的一些细节

关于整体二分具体如何实现,我就不多说了,网上有很多很好的文章。但在下面引用一个很好的框架(来自Dirge的博客)



整体二分的写法大概就是如上。有一些细节:

①对于修改操作,将其改成一次删除和一次插入。例如将x出的值A[x]改为v,则操作为Update(x,A[x],−1)和Update(x,v,1)。

②在整体二分前,所有操作将按时间顺序排好序,整体二分中修改与询问的相对顺序不能改变。注意有些修改可能对一些询问不再有影响(或影响已消除),这些修改和询问的相对顺序当然就不用管。

③我们知道整体二分一定要求离线算法,这点跟CDQ分治类似。如果题目强制要求在线就只能去码可持久化的树套树了。

④整体二分过程中,有一步当K>query()时,将K−=query(),这是为了消除左边修改对右边询问的影响,或者类比平衡树上的二叉查找,直接理解也不难。

对数据结构的清空的时间复杂度不能与序列长度线性相关,只能与当前队列长度相关,不然会退化到O(n2)级别。这里我们直接对修改操作取反。当然,整体二分的时间就是O(nlog(n)log(L)),其中L是二分答案区间的长度。

⑥对于操作的域,由于修改与询问不同,一些域可以代表不同含义重复使用。

⑦题目中一个位置有几个数没有关系。我们的队列放的是所有修改与操作,真正转化出来的是这个。队列里放的只是操作,与原序列下标无关,与原序列有关的是坐标线段树或树状数组等维护区间信息的数据结构。

⑧注意递归下去之前,要事先计算好排序后的操作在原序列的位置,方便下次调用,或者直接交换操作本身。

⑨题目要求第K大的话,就将区间改为[L,mid−1],[mid,R]或者求n−K+1小,或者改成相反数。

⑩。。。

程序

HDU 5412

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#define N 100010
#define MAXR 1000000010

using namespace std;

int n, m, tot;
struct Data{
int type, x, y, K;
}op[N*3];

int qL[N*3], qR[N*3], q[N*3];
int A
, BIT
, ans[N*3];
bool Left[N*3];

int lowbit(int x){
return x & (-x);
}

void Add(int x, int v){
for(int i = x; i <= n; i += lowbit(i))  BIT[i] += v;
}

int Sum(int x){
int res = 0;
for(int i = x; i > 0; i -= lowbit(i))  res += BIT[i];
return res;
}

void Binary(int L, int R, int he, int ta){
if(he > ta)  return;
if(L == R){
for(int i = he; i <= ta; i++){
int tmp = q[i];
if(op[tmp].type)  ans[tmp] = L;
}
return;
}

int mid = (L + R) >> 1;

for(int i = he; i <= ta; i++){
int tmp = q[i];
if(!op[tmp].type && op[tmp].y <= mid){
Add(op[tmp].x, op[tmp].K);
Left[i] = true;
}
else if(op[tmp].type){
int res = Sum(op[tmp].y) - Sum(op[tmp].x-1);
if(op[tmp].K <= res)  Left[i] = true;
else  op[tmp].K -= res;
}
}
for(int i = he; i <= ta; i++){
int tmp = q[i];
if(!op[tmp].type && Left[i])  Add(op[tmp].x, -op[tmp].K);
}

int cnt1 = 0, cnt2 = 0;
for(int i = he; i <= ta; i++){
int tmp = q[i];
if(Left[i])  qL[++cnt1] = tmp;
else  qR[++cnt2] = tmp;
Left[i] = false;
}

for (int i = 1; i <= cnt1; i++)  q[he+i-1] = qL[i];
for (int i = 1; i <= cnt2; i++)  q[he+cnt1+i-1] = qR[i];

Binary(L, mid, he, he+cnt1-1);
Binary(mid+1, R, he+cnt1, ta);
}

int main(){

while(~ scanf("%d", &n)){
tot = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &A[i]);
op[++tot].type = 0;
op[tot].x = i;
op[tot].y = A[i];
op[tot].K = 1;
}
scanf("%d", &m);
int x, a, b;
for(int i = 1; i <= m; i++){
scanf("%d", &x);
if(x == 1){
scanf("%d%d", &a, &b);
op[++tot].type = 0;
op[tot].x = a;
op[tot].y = A[a];
op[tot].K = -1;

A[a] = b;
op[++tot].type = 0;
op[tot].x = a;
op[tot].y = A[a];
op[tot].K = 1;
}
else{
op[++tot].type = 1;
scanf("%d%d%d", &op[tot].x, &op[tot].y, &op[tot].K);
}
}
for(int i = 1; i <= tot; i++)  q[i] = i;
Binary(1, MAXR, 1, tot);
for(int i = 1; i <= tot; i++)
if(op[i].type)  printf("%d\n", ans[i]);
}
return 0;
}


BZOJ 3110

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cstring>
#define N 50010

using namespace std;

int n, m, tot;

struct Data{
int type, x, y;
long long K, v;
}op
;

struct Tnode{
long long sum, add;
}tree[N<<2];

int ans
, qL
, qR
, q
;
bool Left
;

void Down(int root, int L, int R, long long &add){
if(!add)  return;
int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;
tree[Lson].sum += (mid - L + 1) * add;
tree[Lson].add += add;
tree[Rson].sum += (R - mid) * add;
tree[Rson].add += add;
add = 0;
}

void update(int root, int L, int R, int x, int y, long long v){
if(x > R || y < L)  return;
if(x <= L && y >= R){
tree[root].sum += (R - L + 1) * v;
tree[root].add += v;
return;
}

Down(root, L, R, tree[root].add);
int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;
update(Lson, L, mid, x, y, v);
update(Rson, mid+1, R, x, y, v);
tree[root].sum = tree[Lson].sum + tree[Rson].sum;
}

long long query(int root, int L, int R, int x, int y){
if(x > R || y < L)  return 0;
if(x <= L && y >= R)  return tree[root].sum;
Down(root, L, R, tree[root].add);
int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;
long long temp1 = query(Lson, L, mid, x, y);
long long temp2 = query(Rson, mid+1, R, x, y);
return temp1 + temp2;
}

void Binary(int L, int R, int he, int ta){
if(he > ta)  return;
if(L == R){
for(int i = he; i <= ta; i++){
int tmp = q[i];
if(op[tmp].type)  ans[tmp] = L;
}
return;
}

int mid = (L + R) >> 1;
if((mid << 1) < L + R)  mid ++;

for(int i = he; i <= ta; i++){
int tmp = q[i];
if(op[tmp].type){
long long res = query(1, 1, n, op[tmp].x, op[tmp].y);
if(op[tmp].K <= res)  Left[i] = true;
else  op[tmp].K -= res;
}
else if(op[tmp].K >= mid){
update(1, 1, n, op[tmp].x, op[tmp].y, op[tmp].v);
Left[i] = true;
}
}
for(int i = he; i <= ta; i++){
int tmp = q[i];
if(!op[tmp].type && Left[i])  update(1, 1, n, op[tmp].x, op[tmp].y, -op[tmp].v);
}

int cnt1 = 0, cnt2 = 0;

for(int i = he; i <= ta; i++){
int tmp = q[i];
if(!Left[i])  qL[++cnt1] = tmp;
else  qR[++cnt2] = tmp;
Left[i] = false;
}

for(int i = 1; i <= cnt1; i++)  q[he+i-1] = qL[i];
for(int i = 1; i <= cnt2; i++)  q[he+cnt1+i-1] = qR[i];

Binary(L, mid-1, he, he+cnt1-1);
Binary(mid, R, he+cnt1, ta);
}

int main(){

scanf("%d%d", &n, &m);

int x, a, b;
long long c;
for(int i = 1; i <= m; i++){
scanf("%d%d%d%lld", &x, &a, &b, &c);
op[++tot].type = x - 1;
op[tot].x = a;
op[tot].y = b;
op[tot].K = c;
op[tot].v = 1ll;
}

for(int i = 1; i <= tot; i++)  q[i] = i;
Binary(-N, N, 1, tot);

for(int i = 1; i <= tot; i++)
if(op[i].type)  printf("%d\n", ans[i]);

return 0;
}




我依是懵懂无知的孩子 彷徨迷失在曙光不再的黑夜
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: