LOJ #6277 数列分块1题解 && 2023年最后一篇题解

Part #0 . 前言 \text{Part \#0 . 前言} Part #0 . 前言

分块是一种优雅的暴力。


Part #1 . 数列分块入门1 \text{Part \#1 . 数列分块入门1} Part #1 . 数列分块入门1

传送门

这题是一个基础的分块,块外的暴力,块内做标记,块长 n \sqrt{n} n ,块数 n \sqrt{n} n ,注意查询的时候要加上标记。

cpp 复制代码
/*
1. sqrt(m*2/3)
*/
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define fi first
#define se second
#define rg register
#define il inline
#define forz(i,a,b) for(register int i((a));i<=(b);++i)
#define forn(i,a,b) for(register int i((a));i>=(b);--i)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}

inline void write(int x)
{
    if(x<0){putchar('-');x=-x;}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

const int maxn=5e4+100;
const int mod=993244853;
const int inf=9e18;

int a[maxn];
int fk[maxn];
int st[maxn],ed[maxn],lenn[maxn];
int block[maxn];
int num,len;

inline void init(int n)
{
	len=sqrt(2*n/3);
	num=n%len?n/len+1:n/len;
	forz(i,1,num)
	{
		st[i]=(i-1)*len+1;
		ed[i]=min(n,i*len);
		lenn[i]=ed[i]-st[i]+1;
	}
	forz(i,1,num) forz(j,st[i],ed[i]) block[j]=i;
}

inline void solve()
{
	int n=read();
	forz(i,1,n) a[i]=read();
	init(n);
	while (n--)
	{
		int op=read(),l=read(),r=read(),c=read();
		if(!op)
		{
			if(block[l]==block[r]) forz(i,l,r) a[i]+=c;
			else
			{
				forz(i,l,ed[block[l]]) a[i]+=c;
				forz(i,block[l]+1,block[r]-1) fk[i]+=c;
				forz(i,st[block[r]],r) a[i]+=c;
			}
		}
		else
		{
			write(a[r]+fk[block[r]]);
			putchar('\n');
		}
	}
	putchar('\n');
}

signed main()
{
//    IOS;
	int _=1;
	while (_--) solve();
	return 0;
}

明天就是2024年了,祝大家在2024年一帆风顺,前途似锦,期末取得佳绩。

相关推荐
「QT(C++)开发工程师」10 小时前
C++11 新特性 正则表达式、随机数库、元组
c++·正则表达式
ATAOL10 小时前
数据结构一
数据结构·算法
zyq99101_110 小时前
Python日期处理实战代码
python·算法·蓝桥杯
夏星印11 小时前
argparse解析器参数详解
经验分享·笔记·python·学习·argparse
free-elcmacom11 小时前
C++ 默认参数详解:用法、规则与避坑指南
开发语言·c++
Albert Edison11 小时前
【ProtoBuf 语法详解】Any 类型
服务器·开发语言·c++·protobuf
无忧.芙桃11 小时前
C++11的部分内容(上)
c++
小璐资源网12 小时前
C++中如何正确区分`=`和`==`的使用场景?
java·c++·算法
N1_WEB12 小时前
HDU:杭电 2018 复试真题汇总
算法
AMoon丶12 小时前
C++模版-函数模版,类模版基础
java·linux·c语言·开发语言·jvm·c++·算法