跳到正文

个人记录

分块基础

Hhy140516约 18 分钟
原文

这是 2026年3月15日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年3月15日
更新
2026年5月8日
篇幅
约 18 分钟

版本

  1. v2当前
  2. v1

基础

分块就是把一个序列分成很多块,然后查询、修改时,对于整个块进行修改。

需要注意的是,分块只是一种思想,而不是一种算法。

写分块时,一般把长度设为 n\sqrt{n},当然,也有的题卡这个数,所以需要一些调整。

发现如果一整块都要修改时,如果都修改,时间很慢,所以用懒标记记录整个块的加减情况。

例题

P3372 【模板】线段树 1

这道题就是模版,注意要开 long long。

C++
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int kMaxN = 1e5 + 5 ;
int n , m ;
ll a[kMaxN] ;
struct blk
{
	int t , siz ;
	int L[1005] , R[1005] , pos[kMaxN] ;
	ll sum[1005] , lazy[1005] ;
	void init()
	{
		t = sqrt(n) ;
		siz = sqrt(n) ;
		for( int i = 1 ; i <= t ; i++ )
		{
			L[i] = (i - 1) * siz + 1 ;
			R[i] = i * siz ; 
		}
		if(R[t] < n)
		{
			t++ ;
			L[t] = R[t - 1] + 1 ;
			R[t] = n ;
		}
		for( int i = 1 ; i <= t ; i++ )
		{
			for( int j = L[i] ; j <= R[i] ; j++ )
			{
				pos[j] = i ;
				sum[i] += a[j] ;
			}
		}
	}
	void update(int x , int y , ll k)
	{
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				a[i] += k ;
				sum[X] += k ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				lazy[i] += k ;
			}
			for( int i = x ; i <= R[X] ; i++ )
			{
				a[i] += k ;
				sum[X] += k ;
			}
			for( int i = L[Y] ; i <= y ; i++ )
			{
				a[i] += k ;
				sum[Y] += k ;
			}
		}
	}
	ll query(int x , int y)
	{
		ll ans = 0 ;
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				ans += a[i] + lazy[X] ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				ans += sum[i] + (R[i] - L[i] + 1) * lazy[i] ;
			}
			for( int i = x ; i <= R[X] ; i++ )
			{
				ans += a[i] + lazy[X] ;
			}
			for( int i = L[Y] ; i <= y ; i++ )
			{
				ans += a[i] + lazy[Y] ;
			}
		}
		return ans ;
	}
}K ;
void solve()
{
	cin >> n >> m ;
	for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
	K.init() ;
	while(m--)
	{
		int op ;
		cin >> op ;
		if(op == 1)
		{
			int x , y ;
			ll k ;
			cin >> x >> y >> k ;
			K.update(x , y , k) ;
		}
		else
		{
			int x , y ;
			cin >> x >> y ;
			cout << K.query(x , y) << "\n" ;
		}
	}
}
int main()
{
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
	int oT_To = 1 ;
//	cin >> oT_To ;
	while(oT_To--) solve() ;
	return 0 ;
}

P3870 [TJOI2009] 开关

和上题相似,在对于全部块的操作时,进行标记,查询时看是否是标记过,标记就是 R[i]-L[i]+1-sum[i],否则是 sum[i]。

C++
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int kMaxN = 1e5 + 5 ;
int n , m ;
int a[kMaxN] ;
struct blk
{
	int t , siz ;
	int L[1005] , R[1005] , pos[kMaxN] ;
	int sum[1005] ;
	int lazy[1005] ;
	void init()
	{
		t = sqrt(n) ;
		siz = sqrt(n) ;
		for( int i = 1 ; i <= t ; i++ )
		{
			L[i] = (i - 1) * siz + 1 ;
			R[i] = i * siz ; 
		}
		if(R[t] < n)
		{
			t++ ;
			L[t] = R[t - 1] + 1 ;
			R[t] = n ;
		}
		for( int i = 1 ; i <= t ; i++ )
		{
			for( int j = L[i] ; j <= R[i] ; j++ )
			{
				pos[j] = i ;
			}
		}
	}
	void update(int x , int y)
	{
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				sum[X] -= a[i] ;
				a[i] ^= 1 ;
				sum[X] += a[i] ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				lazy[i] ^= 1 ;
			}
			for( int i = x ; i <= R[X] ; i++ )
			{
				sum[X] -= a[i] ;
				a[i] ^= 1 ;
				sum[X] += a[i] ;
			}
			for( int i = L[Y] ; i <= y ; i++ )
			{
				sum[Y] -= a[i] ;
				a[i] ^= 1 ;
				sum[Y] += a[i] ;
			}
		}
	}
	int query(int x , int y)
	{
		int ans = 0 ;
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				ans += a[i] ^ lazy[X] ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				if(lazy[i]) ans += (R[i] - L[i] + 1) - sum[i] ;
				else ans += sum[i] ;
			}
			for( int i = x ; i <= R[X] ; i++ )
			{
				ans += a[i] ^ lazy[X] ;
			}
			for( int i = L[Y] ; i <= y ; i++ )
			{
				ans += a[i] ^ lazy[Y] ;
			}
		}
		return ans ;
	}
}K ;
void solve()
{
	cin >> n >> m ;
	K.init() ;
	while(m--)
	{
		int op ;
		cin >> op ;
		if(op == 0)
		{
			int x , y ;
			cin >> x >> y ;
			K.update(x , y) ;
		}
		else
		{
			int x , y ;
			cin >> x >> y ;
			cout << K.query(x , y) << "\n" ;
		}
	}
}
int main()
{
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
	int oT_To = 1 ;
//	cin >> oT_To ;
	while(oT_To--) solve() ;
	return 0 ;
}

P13977 数列分块入门 2

这道题就可以进行排序找,但是注意,只有两边的要排序。

C++
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int kMaxN = 2e5 + 5 ;
int n , m ;
ll a[kMaxN] ;
struct blk
{
	int t , siz ;
	int L[1005] , R[1005] , pos[kMaxN] ;
	bool vis[1005] ;
	vector<ll> G[1005] ;
	ll lazy[1005] ;
	void init()
	{
		t = sqrt(n) ;
		siz = sqrt(n) ;
		for( int i = 1 ; i <= t ; i++ )
		{
			L[i] = (i - 1) * siz + 1 ;
			R[i] = i * siz ; 
		}
		if(R[t] < n)
		{
			t++ ;
			L[t] = R[t - 1] + 1 ;
			R[t] = n ;
		}
		for( int i = 1 ; i <= t ; i++ )
		{
			for( int j = L[i] ; j <= R[i] ; j++ )
			{
				pos[j] = i ;
				G[i].push_back(a[j]) ;
			}
			sort(G[i].begin() , G[i].end()) ;
		}
	}
	void update(int x , int y , ll c)
	{
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			vis[X] = 1 ;
			for( int i = x ; i <= y ; i++ )
			{
				a[i] += c ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				lazy[i] += c ;
			}
			vis[X] = 1 ;
			for( int i = x ; i <= R[X] ; i++ )
			{
				a[i] += c ;
			}
			vis[Y] = 1 ;
			for( int i = L[Y] ; i <= y ; i++ )
			{
				a[i] += c ;
			}
		}
	}
	int get(int x , int y , ll c)
	{
		int X = pos[x] , Y = pos[y] ;
		int ans = 0 ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				ans += (a[i] + lazy[X] < c * c) ;
			}
			return ans ;
		}
		for( int i = X + 1 ; i < Y ; i++ ) 
		{
			if(vis[i])
			{
				G[i].clear() ;
				for( int j = L[i] ; j <= R[i] ; j++ )
				{
					G[i].push_back(a[j]) ;
				}
				sort(G[i].begin() , G[i].end()) ;
				vis[i] = 0 ;
			}
			if(G[i][G[i].size() - 1] + lazy[i] < c * c) 
			{
				ans += R[i] - L[i] + 1 ;
				continue ;
			}
			int P = lower_bound(G[i].begin() , G[i].end() , c * c - lazy[i]) - G[i].begin() ;
//			cout << P << " " ;
			ans += P ;
		}
		for( int i = x ; i <= R[X] ; i++ )
		{
			ans += (a[i] + lazy[X] < c * c) ;
		}
		for( int i = L[Y] ; i <= y ; i++ )
		{
			ans += (a[i] + lazy[Y] < c * c) ;
		}
		return ans ;
	}
}K ;
void solve()
{
	cin >> n ;
	for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
	K.init() ;
	while(n--)
	{
		int op ;
		cin >> op ;
		if(op == 0)
		{
			int x , y ;
			ll c ;
			cin >> x >> y >> c ;
			K.update(x , y , c) ;
		}
		else
		{
			int x , y ;
			ll c ;
			cin >> x >> y >> c ;
			cout << K.get(x , y , c) << "\n" ;
		}
	}
}
int main()
{
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
	int oT_To = 1 ;
//	cin >> oT_To ;
	while(oT_To--) solve() ;
	return 0 ;
}

P2801 教主的魔法

就是上一道题,稍微改一点。

C++
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int kMaxN = 1e6 + 5 ;
int n , m ;
ll a[kMaxN] ;
struct blk
{
	int t , siz ;
	int L[1005] , R[1005] , pos[kMaxN] ;
	bool vis[1005] ;
	vector<ll> G[1005] ;
	ll lazy[1005] ;
	void init()
	{
		t = sqrt(n) ;
		siz = sqrt(n) ;
		for( int i = 1 ; i <= t ; i++ )
		{
			L[i] = (i - 1) * siz + 1 ;
			R[i] = i * siz ; 
		}
		if(R[t] < n)
		{
			t++ ;
			L[t] = R[t - 1] + 1 ;
			R[t] = n ;
		}
		for( int i = 1 ; i <= t ; i++ )
		{
			for( int j = L[i] ; j <= R[i] ; j++ )
			{
				pos[j] = i ;
				G[i].push_back(a[j]) ;
			}
			sort(G[i].begin() , G[i].end()) ;
		}
	}
	void update(int x , int y , ll c)
	{
		int X = pos[x] , Y = pos[y] ;
		if(X == Y)
		{
			vis[X] = 1 ;
			for( int i = x ; i <= y ; i++ )
			{
				a[i] += c ;
			}
		}
		else
		{
			for( int i = X + 1 ; i < Y ; i++ )
			{
				lazy[i] += c ;
			}
			vis[X] = 1 ;
			for( int i = x ; i <= R[X] ; i++ )
			{
				a[i] += c ;
			}
			vis[Y] = 1 ;
			for( int i = L[Y] ; i <= y ; i++ )
			{
				a[i] += c ;
			}
		}
	}
	int get(int x , int y , ll c)
	{
		int X = pos[x] , Y = pos[y] ;
		int ans = 0 ;
		if(X == Y)
		{
			for( int i = x ; i <= y ; i++ )
			{
				ans += (a[i] + lazy[X] >= c) ;
			}
			return ans ;
		}
		for( int i = X + 1 ; i < Y ; i++ ) 
		{
			if(vis[i])
			{
				G[i].clear() ;
				for( int j = L[i] ; j <= R[i] ; j++ )
				{
					G[i].push_back(a[j]) ;
				}
				sort(G[i].begin() , G[i].end()) ;
				vis[i] = 0 ;
			}
			if(G[i][0] + lazy[i] >= c) 
			{
				ans += R[i] - L[i] + 1 ;
				continue ;
			}
			int P = lower_bound(G[i].begin() , G[i].end() , c - lazy[i]) - G[i].begin() ;
//			cout << P << " " ;
			ans += G[i].size() - P ;
		}
		for( int i = x ; i <= R[X] ; i++ )
		{
//			cout << a[i] + lazy[X] << " " ;
			ans += (a[i] + lazy[X] >= c) ;
		}
		for( int i = L[Y] ; i <= y ; i++ )
		{
//			cout << a[i] + lazy[X] << " " ;
			ans += (a[i] + lazy[Y] >= c) ;
		}
		return ans ;
	}
}K ;
void solve()
{
	cin >> n >> m ;
	for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
	K.init() ;
	while(m--)
	{
		char op ;
		cin >> op ;
		if(op == 'M')
		{
			int x , y ;
			ll c ;
			cin >> x >> y >> c ;
			K.update(x , y , c) ;
		}
		else
		{
			int x , y ;
			ll c ;
			cin >> x >> y >> c ;
			cout << K.get(x , y , c) << "\n" ;
		}
	}
}
int main()
{
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
	int oT_To = 1 ;
//	cin >> oT_To ;
	while(oT_To--) solve() ;
	return 0 ;
}