對於區間操作的問題,部分可以用分塊來做
事實上分塊還是蠻簡單的,主要是看分塊之後怎麼樣進行處理,它很靈活,沒有固定的模式
還是拿題目來舉例吧
bzoj 2002bzoj 3065
bzoj 2002 分塊版
#include"cstdio"
#include"cmath"
#define maxn 200005
int st[maxn],pt[maxn],belong[maxn],k[maxn];
int l[1000],r[1000];
inline int read()
while (ch<='9' && ch>='0')
return x*f;
}void init(int n,int block)
int cnt;
if (n%block) cnt = n/block + 1; else
cnt = n/block;
for (int i = 1;i <= cnt;i ++)
r[cnt] = n;
for (int i = n;i > 0;i --)
if (i+k[i] > n) st[i] = 1; else
if (belong[i] == belong[i+k[i]])
st[i] = st[i+k[i]]+1,pt[i] = pt[i+k[i]]; else
st[i] = 1,pt[i] = i+k[i];
}int cal(int x)
return ans;
}int main()
}return 0;
}
bzoj 3065 自己模大佬的分塊版
#include"cstdio"
#include"algorithm"
#include"cmath"
using namespace std;
int a[1005][1005],b[1005][1005],inum[100002];
int idx,ipos,tot[1005],nxt[1005];
int cnt,n,b,lastans;
typedef pairabcd;
abcd lp,rp;
inline void shuchu()
printf("\n");
}inline char read()
inline abcd kth(int k)
}bool check(int mid,int k)
for(int i = lp.second;i <= tot[lp.first];i ++)
if (a[lp.first][i] <= mid) ret++;
for(int i= 1;i <= rp.second;i ++)
if (a[rp.first][i] <= mid) ret++;
for (int i = nxt[lp.first];i != rp.first;i = nxt[i])
ret+=upper_bound(b[i]+1,b[i]+tot[i]+1,mid) - b[i] - 1;
return ret>=k;
}void query()
void modify()
void depth(int idx)
void insert()
int main()
for (int i = 1;i <= cnt;i ++)
sort(b[i]+1,b[i]+tot[i]+1),nxt[i] = i+1;
nxt[cnt] = 0;
scanf("%d",&p);
while (p--)
return 0;
}
演算法 又快又簡單的排序 桶排序
在我們生活的這個世界中到處都是被排序過的。站隊的時候會按照身高排序,考試的名次需要按照分數排序,網上購物的時候會按照 排序,電子郵箱中的郵件按照時間排序 總之很多東西都需要排序,可以說排序是無處不在。現在我們舉個具體的例子來介紹一下排序演算法。首先出場的我們的主人公小哼,上面這個可愛的娃就是啦。期末...
分塊的複雜度證明
我們都知道,分塊的時間複雜度是 o n sqrt n o n n 不過不知大家有沒有想過這個複雜度是如何得出的?如果你只關心分塊演算法的實現及其應用,請忽略這篇文章 如果你早就知道了證明方法,請忽略這篇文章 如果你是神犇,請務必忽略以下內容 設定在給出證明之前,先給出一些符號宣告 設原序列的長度為 ...
簡單的演算法複雜度
常見的演算法複雜度 堆排序演算法 構建堆 有比較 交換過程,構建堆 時間複雜度 o n 完全二叉樹某個節點 i 到根節點 距離,logi 1 需要取 n 1 次堆頂記錄,重建堆的時間複雜度為 o nlogn 總的,堆排序的時間複雜度是 o nlogn 效能上遠遠好過 冒泡,簡單選擇,直接插入的o n...