望月哀叹第一分块
说实话,我没有看过未来日记。但是我有一个很好的朋友看过了,他非常喜欢未来日记。现在的他已经退役了,本着帮他完成心愿的目的,我来做了这道大分块。
时间复杂度是对的,但是实现的不好,所以怎么卡都差 0.05 秒,姑且也算是成功了吧其实只是作者懒得重构代码罢了\笑。
Part 1
我妻由乃给了你一个长为
- 把区间
[l,r] 内所有的x 变成y 。 - 查询区间
[l,r] 内第k 小值。
这个修改是一个很强的操作。但是在贯穿突刺第二分块中,我们已经积累了相当的经验了。
现在我们来想想怎么在
我们想想看如果没有这个修改操作我们怎么查询。显然的,我们有一个很直接做法,就是二分+线段树,时间复杂度
怎么时间复杂度还倒退了呢\恼。我们有一个平衡的办法,就是对序列和值域一同分块。对于每一个块,开一个等同于值域大小的数组
修改也很简单,只修一个数,一路修过去就完了,也是
来看代码,把
#include<iostream>
using namespace std;
const int N = 1e5+5;
const int M = 200;
const int siz_of_series = 630;
const int siz_of_val = 260;
const int VAL = 1e5;
int n, m, a[N];
int blk[N], bel[N];
int lb[N], rb[N];
int rt[M][N], cnt[M][N], cnt1[M][N], cnt2[M][N], tmp2[M][N];
int tot1, tot2;
int fa[N], v[N];
inline int find(int x){
return x == fa[x] ? fa[x] : fa[x] = find(fa[x]);
}
inline void init(int x){
for(int i=lb[x]; i<=rb[x]; i++){
if(rt[x][a[i]]){
fa[i] = rt[x][a[i]];
} else {
fa[i] = rt[x][a[i]] = i; v[i] = a[i];
}
cnt[x][a[i]] += 1;
}
}
inline void push(int x){
for(int i=lb[x]; i<=rb[x]; i++) {
rt[x][v[find(i)]] = cnt[x][v[find(i)]] = 0;
}
for(int i=lb[x]; i<=rb[x]; i++) a[i] = v[find(i)];
for(int i=lb[x]; i<=rb[x]; i++) fa[i] = v[i] = 0;
}
inline int merge(int x, int A, int B){
int res = cnt[x][A];
if(cnt[x][A] == 0) return 0;
if(rt[x][B]) {
fa[rt[x][A]] = rt[x][B];
} else {
rt[x][B] = rt[x][A];
v[rt[x][A]] = B;
}
cnt[x][B] += cnt[x][A];
cnt[x][A] = rt[x][A] = 0;
return res;
}
int sta[N], tmp0[N];
inline void update(int x, int qlt, int qrt, int ft, int to){
int tmp, idx;
rt[x][ft]= rt[x][to] = tmp = idx = 0;
for(int i=lb[x]; i <= rb[x]; i++) {
a[i] = v[find(i)];
if(a[i] == ft || a[i] == to) sta[++idx] = i;
}
for(int i=qlt; i <= qrt; i++){
if(a[i] == ft) a[i]=to, ++tmp;
}
// for(int i=lb[x]; i<=rb[x]; i++){
// if(rt[x][a[i]]){
// fa[i] = rt[x][a[i]];
// } else {
// fa[i] = rt[x][a[i]] = i; v[i] = a[i];
// }
// }
for(int j=1; j<=idx; j++){
int i = sta[j];
if(rt[x][a[i]]){
fa[i] = rt[x][a[i]];
} else {
fa[i] = rt[x][a[i]] = i; v[i] = a[i];
}
}
cnt[x][ft] -= tmp; cnt[x][to] += tmp;
tmp0[x] = tmp;
}
inline void update(int qlt, int qrt, int ft, int to){
int p = blk[qlt], q = blk[qrt];
// for(int i=p; i<=q; i++){
// tmp2[i][bel[ft]] -= cnt[i][ft];
// tmp2[i][bel[to]] -= cnt[i][to];
// }
if(p == q){
update(p, qlt, qrt, ft, to);
} else {
for(int i=p+1; i<=q-1; i++) tmp0[i] = merge(i, ft, to);
update(p, qlt, rb[p], ft, to);
update(q, lb[q], qrt, ft, to);
}
// for(int i=p; i<=q; i++){
// tmp2[i][bel[ft]] += cnt[i][ft];
// tmp2[i][bel[to]] += cnt[i][to];
// }
for(int i=p; i<=tot1; i++) {
tmp0[i] += tmp0[i-1]; tmp0[i-1] = 0;
cnt1[i][ft] = cnt1[i-1][ft] + cnt[i][ft];
cnt1[i][to] = cnt1[i-1][to] + cnt[i][to];
// cnt2[i][bel[ft]] = cnt2[i-1][bel[ft]] + tmp2[i][bel[ft]];
// cnt2[i][bel[to]] = cnt2[i-1][bel[to]] + tmp2[i][bel[to]];
cnt2[i][bel[ft]] -= tmp0[i];
cnt2[i][bel[to]] += tmp0[i];
}
}
int c1[N], c2[N];
inline int query(int qlt, int qrt, int k){
int p = blk[qlt], q = blk[qrt];
int res = 1;
if(p == q){
for(int i=qlt; i<=qrt; i++){
c1[v[find(i)]] += 1;
c2[bel[v[find(i)]]] += 1;
}
for(;res<=VAL && k; res += siz_of_val){
if(k <= c2[bel[res]]) break;
k -= c2[bel[res]];
}
for(int BD=res+siz_of_val; res<=BD && k; ++res){
if(k <= c1[res]) break;
k -= c1[res];
}
for(int i=qlt; i<=qrt; i++){
c1[v[find(i)]] -= 1;
c2[bel[v[find(i)]]] -= 1;
}
} else {
for(int i=qlt; i<=rb[p]; ++i){
c1[v[find(i)]] += 1;
c2[bel[v[find(i)]]] += 1;
}
for(int i=lb[q]; i<=qrt; ++i){
c1[v[find(i)]] += 1;
c2[bel[v[find(i)]]] += 1;
}
for(;res<=VAL && k; res += siz_of_val){
if(k <= c2[bel[res]] + cnt2[q-1][bel[res]] - cnt2[p][bel[res]]) break;
k -= c2[bel[res]] + cnt2[q-1][bel[res]] - cnt2[p][bel[res]];
}
for(int BD=res+siz_of_val; res<=BD && k; ++res){
if(k <= c1[res] + cnt1[q-1][res] - cnt1[p][res]) break;
k -= c1[res] + cnt1[q-1][res] - cnt1[p][res];
}
for(int i=qlt; i<=rb[p]; ++i){
c1[v[find(i)]] -= 1;
c2[bel[v[find(i)]]] -= 1;
}
for(int i=lb[q]; i<=qrt; ++i){
c1[v[find(i)]] -= 1;
c2[bel[v[find(i)]]] -= 1;
}
}
return res;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
lb[1] = 1;
for(int i=1, num = 0; i<=n; i++){
num += 1;
blk[i] = tot1+1;
if(num == siz_of_series || i == n){
num -= siz_of_series;
tot1 += 1;
rb[tot1] = i; lb[tot1+1] = i+1;
init(tot1);
}
}
for(int i=1, num=0; i<=VAL; i++){
num += 1;
bel[i] = tot2+1;
if(num == siz_of_val || i == VAL){
tot2 += 1; num -=siz_of_val;
}
}
for(int i=1; i<=tot1; i++){
for(int j=1; j<=VAL; j++){
cnt1[i][j] += cnt1[i-1][j] + cnt[i][j];
}
}
for(int i=1; i<=tot1; i++){
for(int j=1; j<=tot2; j++){
cnt2[i][j] = cnt2[i-1][j];
}
for(int j=1; j<=VAL; j++){
// tmp2[i][bel[j]] += cnt[i][j];
cnt2[i][bel[j]] += cnt[i][j];
}
}
for(int i=1; i<=m; i++){
int opt; cin>>opt;
if(opt == 1){
int x, y, f, t; cin>>x>>y>>f>>t;
if(f != t) update(x, y, f, t);
// for(int j=1; j<=n; j++) cout<<v[find(j)]<<" ";
// cout<<"\n";
} else {
int x, y, k; cin>>x>>y>>k;
cout<<query(x, y, k)<<"\n";
}
}
// for(int i=1; i<=n; i++) cout<<query(i, i, 1)<<" ";
return 0;
}
/*
*
9 9
1 2 3 3 2 1 1 2 3
2 1 4 2
1 3 5 3 2
2 1 5 1
2 3 9 5
1 4 8 2 4
2 3 9 5
2 1 9 6
1 3 9 2 1
2 1 9 5
*/