LOJ#6281. 数列分块入门 5

内存限制:256 MiB时间限制:500 ms标准输入输出

题目类型:传统评测方式:文本比较

上传者: hzwer

提交提交记录统计讨论

1

测试数据

题目描述

给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间开方,区间求和。

输入格式

第一行输入一个数字 nnn。

第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开。

接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔开。

若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字都开方。

若 opt=1\mathrm{opt} = 1opt=1,表示询问位于 [l,r][l, r][l,r] 的所有数字的和。

输出格式

对于每次询问,输出一行一个数字表示答案。

样例

样例输入

4
1 2 2 3
0 1 3 1
1 1 4 4
0 1 2 2
1 1 2 4

样例输出

6
2

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤50000,−231≤others 1 \leq n \leq 50000, -2^{31} \leq \mathrm{others}1≤n≤50000,−2?31??≤others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans≤2?31??−1。

这道题的难点在于如何维护开根这个神奇的操作

我自己测的是1e7的数差不多开五六次根就会变成1,所以我们直接维护整个块内的数是否变成了1就可以了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define int long long
using namespace std;
const int MAXN=1e5+10;
const int INF=1e8+10;
inline char nc()
{
    static char buf[MAXN],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    char c=nc();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=nc();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=nc();}
    return x*f;
}
int N;
int a[MAXN],block,L[MAXN],R[MAXN],belong[MAXN],sum[MAXN],flag[MAXN];

void Sqrt(int l,int r)
{
	for(int i=l;i<=min(r,R[l]);i++)
	{
		sum[belong[i]]-=a[i];
		a[i]=sqrt(a[i]);
		sum[belong[i]]+=a[i];
	}
	if(belong[l]!=belong[r])
		for(int i=L[r];i<=r;i++)
			sum[belong[i]]-=a[i],a[i]=sqrt(a[i]),sum[belong[i]]+=a[i];
	for(int i=belong[l]+1;i<=belong[r]-1;i++)
	{
		if(flag[i]) {continue;}
		flag[i]=1;
		for(int j=L[i*block];j<=R[i*block];j++)
		{
			sum[i]-=a[j];
			a[j]=sqrt(a[j]);
			sum[i]+=a[j];
			if(a[j]>1) flag[i]=0;
		}
	}
}
int Query(int l,int r)
{
	int ans=0;
	for(int i=l;i<=min(r,R[l]);i++)
		ans+=a[i];
	if(belong[l]!=belong[r])
		for(int i=L[r];i<=r;i++)
			ans+=a[i];
	for(int i=belong[l]+1;i<=belong[r]-1;i++)
		ans+=sum[i];
	return ans;
}
main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
   // freopen("b.out","w",stdout);
    #else
    #endif
	N=read();block=sqrt(N);
	for(int i=1;i<=N;i++) a[i]=read();
	for(int i=1;i<=N;i++) belong[i]=(i-1)/block+1,L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block;
	for(int i=1;i<=N;i++) sum[belong[i]]+=a[i];
	for(int i=1;i<=N;i++)
	{
		int opt=read(),l=read(),r=read(),c=read();
		if(opt==0)
			Sqrt(l,r);
		else
			printf("%d\n",Query(l,r));
	}
    return 0;
}

  

原文地址:https://www.cnblogs.com/zwfymqz/p/8445967.html

时间: 2024-08-30 12:09:08

LOJ#6281. 数列分块入门 5的相关文章

LOJ.6281.数列分块入门5(分块 区间开方)

题目链接 int内的数(也不非得是int)最多开方4.5次就变成1了,所以还不是1就暴力,是1就直接跳过. #include <cmath> #include <cstdio> #include <cctype> #include <algorithm> #define gc() getchar() typedef long long LL; const int N=5e4+5; int n,size,bel[N],A[N],tm[N]; LL sum[N]

LOJ#6284. 数列分块入门 8

#6284. 数列分块入门 8 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 1 测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间询问等于一个数 ccc 的元素,并将这个区间的所有元素改为 ccc. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入三个数字 ll

Loj 6279. 数列分块入门 3

题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的前驱(比其小的最大元素). 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_iai?,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字

Loj 6277 数列分块入门 1

题目链接:https://loj.ac/problem/6277 题面: 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l,

loj#6285 数列分块入门 9 ( 回 滚 )

题目 :  链接 :https://loj.ac/problem/6285 题意:给出一个长为 n的数列,以及 n个操作,操作涉及询问区间的最小众数. 思路:虽然这不是一道 回滚莫队题,就是 暴力分块 的题, 但是 还是 可以用回滚莫队 写滴,好像大部分题解都是 暴力分块. #include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define rep(i,j,k) for(int i=j;i<

LOJ#6278. 数列分块入门 2

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的元素个数. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,

#6280. 数列分块入门 4 #6281. 数列分块入门 5

题目描述 给出一个长为 n 的数列,以及 n 个操作,操作涉及区间加法,区间求和. 输入格式 第一行输入一个数字 n. 第二行输入 n 个数字,第 i 个数字为 ai?,以空格隔开. 接下来输入 n 行询问,每行输入四个数字 opt.l.r.c,以空格隔开. 若 opt=0,表示将位于 [l,r]的之间的数字都加 c. 若 opt=1,表示询问位于 [l,r]的所有数字的和 ?mod(c+1). 输出格式 对于每次询问,输出一行一个数字表示答案. 样例 样例输入 4 1 2 2 3 0 1 3

LOJ#6282. 数列分块入门 6

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及单点插入,单点询问,数据随机生成. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 

LOJ#6283. 数列分块入门 7

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间乘法,区间加法,单点询问. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 op