题目链接:https://loj.ac/problem/6280

加一个数组保存块的和。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<set>
#include<cstdio>
#include<string>
#include<deque>
using namespace std;
typedef long long LL;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define maxn 50010
/*struct point{
int u,w;
};
bool operator <(const point &s1,const point &s2)
{
if(s1.w!=s2.w)
return s1.w>s2.w;
else
return s1.u>s2.u;
}*/
LL n,m,k,t,block;
LL a[maxn],sum[maxn],tag[maxn];
int lump[maxn];
void add(LL l,LL r,LL w)
{
for(int i=l;i<=min(lump[l]*block,r);i++)//这里错了n次...这里是r表示n,是为了防止l和r在一个块里面的情况,要引以为戒
{
a[i]+=w;
sum[lump[l]]+=w;
}
if(lump[l]!=lump[r])
{
for(int i=(lump[r]-)*block+;i<=r;i++)
{
a[i]+=w;
sum[lump[r]]+=w;
}
}
for(int i=lump[l]+;i<=lump[r]-;i++)
{
tag[i]+=w;
sum[i]+=w*block;
}
}
LL get_sum(LL l,LL r)
{
LL ans=;
for(int i=l;i<=min(lump[l]*block,r);i++)
ans+=a[i]+tag[lump[l]];
if(lump[l]!=lump[r])
{
for(int i=block*(lump[r]-)+;i<=r;i++)
ans+=a[i]+tag[lump[r]];
}
for(int i=lump[l]+;i<=lump[r]-;i++)
ans+=sum[i];
return ans;
}
int main()
{
scanf("%lld",&n);
block=sqrt(n);
fill(sum,sum+maxn-,);
fill(tag,tag+maxn-,);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
lump[i]=(i-)/block+;
sum[lump[i]]+=a[i];
}
for(int i=;i<=n;i++)
{
LL op,l,r,w;
scanf("%lld%lld%lld%lld",&op,&l,&r,&w);
if(op==)
add(l,r,w);
else
printf("%lld\n",get_sum(l,r)%(w+));
}
return ;
}

LibreOJ 6280 . 数列分块入门 4的更多相关文章

  1. LibreOJ 6280 数列分块入门 4(分块区间加区间求和)

    题解:分块的区间求和比起线段树来说实在是太好写了(当然,复杂度也高)但这也是没办法的事情嘛.总之50000的数据跑了75ms左右还是挺优越的. 比起单点询问来说,区间询问和也没有复杂多少,多开一个su ...

  2. LOJ #6280. 数列分块入门 4-分块(区间加法、区间求和)

    #6280. 数列分块入门 4 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论   题目描述 给出一个 ...

  3. LibreOJ 6277. 数列分块入门 1 题解

    题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 \( ...

  4. LibreOJ 6277 数列分块入门 1(分块)

    题解:感谢hzwer学长和loj让本蒟蒻能够找到如此合适的入门题做. 这是一道非常标准的分块模板题,本来用打标记的线段树不知道要写多少行,但是分块只有这么几行,极其高妙. 代码如下: #include ...

  5. LibreOJ 6278. 数列分块入门 2 题解

    题目链接:https://loj.ac/problem/6278 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,询问区间内小于某个值 \(x\) 的元素个数. ...

  6. LibreOJ 6285. 数列分块入门 9

    题目链接:https://loj.ac/problem/6285 其实一看到是离线,我就想用莫队算法来做,对所有询问进行分块,但是左右边界移动的时候,不会同时更新数字最多的数,只是后面线性的扫了一遍, ...

  7. LibreOJ 6282. 数列分块入门 6

    题目链接:https://loj.ac/problem/6282 参考博客:http://www.cnblogs.com/stxy-ferryman/p/8560551.html 这里如果用数组的话元 ...

  8. LibreOJ 6277. 数列分块入门 2

    题目链接:https://loj.ac/problem/6278 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 这题我 ...

  9. LibreOJ 6277. 数列分块入门 1

    题目链接:https://loj.ac/problem/6277 参考博客:https://www.cnblogs.com/stxy-ferryman/p/8547731.html 两个操作,区间增加 ...

随机推荐

  1. <基础> PHP 字符串操作

    explode — 使用一个字符串分割另一个字符串 array explode ( string $delimiter , string $string [, int $limit ] ) implo ...

  2. linux驱动开发第一步hello

    先查看Ubuntu的版本 cat /etc/issue lin@lin-virtual-machine:~$ cat /etc/issue Ubuntu 12.04 LTS \n \l 或者使用 li ...

  3. element(vue.js)+django 整合

    近期开始接触Python,从web开发入门.尝试Django与vue整合,大概分3个阶段: 1.基于Django开发web后端 2.基于element开发好前端 3.前后端整合 参考:https:// ...

  4. JavaScript:今天是今年第几周?

    用js实现,今天是今年第几周? 基本思路: 1.当前时间 - 今年1月1日0时,拿到时间差(毫秒数) 2.时间差/7天毫秒数,向上取整 var d = new Date('2018-01-01 00: ...

  5. 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量

    1.tf.Variable([[1, 2]])  # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...

  6. hive,分桶,内外部表,分区

    简单的word-count操作: [root@master test-map]# head -10 The_Man_of_Property.txt    #先看看数据Preface“The Forsy ...

  7. jqGrid基本使用

    jqGrid: 参照网址:官网地址http://www.trirand.com/ http://blog.mn886.net/jqGrid/(快速获取demo) http://www.guriddo. ...

  8. Windows 8 禁用强制驱动签名

    打开管理员模式的命令行,运行命令: bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS bcdedit/set testsigning on ...

  9. ETC2 区别于ETC的重要点

    ETC2 主要是对于NPOT却是4的倍数的贴图有较大压缩,比如一个1920X1080RGB的Loading图,ETC4压缩下不管用,大小5.9M,ETC2下压缩为1M

  10. C#转成时间格式

    public static string GetDatetime()        {            System.Globalization.DateTimeFormatInfo myDTF ...