跳到正文

个人记录

狡猾的分块(板子很好吃(吧?))

Gu123456aaa约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年11月6日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <cmath>
#define int long long
using namespace std;
const int N=3e5+5;
int n,a[N],l[N],r[N],pos[N],lazy[N],cnt;
void build(){
	int k=sqrt(n);
	while(1){
		cnt++;
		l[cnt]=(cnt-1)*k+1;
		r[cnt]=k*cnt;
		if(r[cnt]>=n){
			r[cnt]=n;
			break;
		}
	}
	for (int i=1;i<=cnt;i++){
		for (int j=l[i];j<=r[i];j++){
			pos[j]=i;
		}
	}
}
void upd(int L,int R,int x){
	if(pos[L]==pos[R]){
		for (int i=L;i<=R;i++){
			a[i]+=x;
		}
		return;
	}
	for (int i=L;i<=r[pos[L]];i++){
		a[i]+=x;
	}
	for (int i=l[pos[R]];i<=R;i++){
		a[i]+=x;
	}
	for (int i=pos[L]+1;i<pos[R];i++){
		lazy[i]+=x;
	}
	return;
}
int query(int x){
	return a[x]+lazy[pos[x]];
}
signed main()
{
	cin >> n;
	for (int i=1;i<=n;i++){
		cin >> a[i];
	}
	build();
	for (int i=1;i<=n;i++){
		int op,p,q,c;
		cin >> op >> p >> q >> c;
		if(op==0){
			upd(p,q,c);
		}
		else {
			cout << query(q) << endl;
		}
	}
	return 0;
}
原文