分块练习C. interval

题目描述

\(N\)个数\(a_i\),\(m\)个操作

\(1\). 从第一个数开始,每隔\(k_i\)个的位置上的数增加\(x_i\)

\(2\). 查询\(l\)到\(r\)的区间和

输入格式

第一行两个整数\(n\),\(m\)

第二行\(n\)个数,\(a_i\)

接下来\(m\)行,每行三个整数,\(a\),\(b\),\(c\)

如果\(a=1\),表示修改操作

否则表示查询 \(b\)到\(c\)的区间和

输出格式

依次输出每个查询

样例

样例输入

10 6

5 1 4 2 3 6 4 1 2 3

1 2 4

2 6 8

1 1 4

2 3 6

1 5 4

2 2 9

样例输出

15

27

51

数据范围与提示

数据均随机生成,保证合法

对于\(50\%\)的数据 \(n,m<=10000\)

对于\(100\%\)的数据,\(n,m<=100000\)

分析

由于数据水到一定境界,所以暴力即可通过本题

但是,怀着务实求真的心态,我们还是要探究一下本题的分块解法

分块的核心是大段维护,局部朴素

因此我们考虑怎么对一个大段整体打上标记

题目中的修改操作是每间隔固定的长度加上一个值

因此我们可以对每一个块开一个\(vector\)记录每次修改时该块内被改动的第一个元素,改动的间隔以及增加的价值

对于间隔小于 $ \sqrt{n} $的修改,我们用上面的方式去打标记

对于间隔大于 $\sqrt{n} $的修改,我们暴力去维护会更优

查询时,我们将区间两端的散点,暴力去加,同时把标记下放

对于中间的大区间,我们直接维护一个\(sum\)加上即可

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
const int maxn = 1e5 + 5;
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 << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int n, m, shuyu[maxn], blo, sum[maxn], a[maxn];
struct asd {
int wz, ad, jz;
asd() {}
asd(int aa, int bb, int cc) { wz = aa, ad = bb, jz = cc; }
};
std::vector<asd> g[maxn];
void xg(int jg, int val) {
if (jg >= blo) {
for (int i = 1; i <= n; i += jg) {
a[i] += val;
sum[shuyu[i]] += val;
}
} else {
int beg = 1;
for (int i = 1; i <= shuyu[n]; i++) {
if (shuyu[beg] == i && beg <= n)
g[i].push_back(asd(beg, jg, val));
int ed = std::min(i * blo, n);
int cz = (ed - beg) / jg;
sum[i] += (cz + 1) * val;
beg += (cz + 1) * jg;
}
}
}
void qk(int id) {
for (int i = 0; i < g[id].size(); i++) {
int beg = g[id][i].wz, jg = g[id][i].ad, val = g[id][i].jz;
for (int j = beg; j <= id * blo; j += jg) {
a[j] += val;
}
}
g[id].clear();
}
int cx(int l, int r) {
int ans = 0;
qk(shuyu[l]);
for (int i = l; i <= std::min(r, shuyu[l] * blo); i++) {
ans += a[i];
}
if (shuyu[l] == shuyu[r])
return ans;
qk(shuyu[r]);
for (int i = r; i >= (shuyu[r] - 1) * blo + 1; i--) {
ans += a[i];
}
for (int i = shuyu[l] + 1; i <= shuyu[r] - 1; i++) {
ans += sum[i];
}
return ans;
}
int main() {
n = read(), m = read();
blo = sqrt(n);
for (int i = 1; i <= n; i++) {
a[i] = read();
shuyu[i] = (i - 1) / blo + 1;
sum[shuyu[i]] += a[i];
}
for (int i = 1; i <= m; i++) {
int aa, bb, cc;
aa = read(), bb = read(), cc = read();
if (aa == 1) {
bb++;
xg(bb, cc);
} else {
printf("%d\n", cx(bb, cc));
}
}
return 0;
}

分块练习C. interval的更多相关文章

  1. P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?LCT?...FAQ orz

    好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...

  2. P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?

    好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...

  3. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  4. HDU 4391 Paint The Wall(分块+延迟标记)

    Paint The Wall Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. POJ 3468 A Simple Problem with Integers (分块)

    Description You have \(N\) integers, \(A_1, A_2, ... , A_N\). You need to deal with two kinds of ope ...

  6. PHP搭建大文件切割分块上传功能

    背景 在网站开发中,文件上传是很常见的一个功能.相信很多人都会遇到这种情况,想传一个文件上去,然后网页提示"该文件过大".因为一般情况下,我们都需要对上传的文件大小做限制,防止出现 ...

  7. Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx

    问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...

  8. POJ2104 K-th Number [分块做法]

    传送:主席树做法http://www.cnblogs.com/candy99/p/6160704.html 做那倒带修改的主席树时就发现分块可以做,然后就试了试 思想和教主的魔法差不多,只不过那个是求 ...

  9. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

随机推荐

  1. 菜鸟教程的 mysql-connector 基础

    安装驱动 python -m pip install mysql-connector 导包 import mysql.connector mydb = mysql.connector.connect( ...

  2. 给定两个列表,转换为 DataFrame 类型

    import pandas as pd def get_data(): q1 = [] q2 = [] p1 = input("list 1:") p2 = input(" ...

  3. AJAX 是什么?

    AJAX 简介 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX 是什么? AJAX = Asynchronous JavaScript and XML. AJAX ...

  4. PHP array_values() 函数

    实例 返回数组中所有的值(不保留键名): <?php$a=array("Name"=>"Peter","Age"=>&qu ...

  5. PHP juliantojd() 函数

    ------------恢复内容开始------------ 实例 把儒略历法的日期转换为儒略日计数,然后再转换回儒略历法的日期: <?php$jd=juliantojd(6,20,2007); ...

  6. Skill 中的通用输出格式规范

    https://www.cnblogs.com/yeungchie/ Skill中的通用输出格式规范 Common Output Format Specifications Format Specif ...

  7. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  8. 5.4 省选模拟赛 修改 线段树优化dp 线段树上二分

    LINK:修改 题面就不放了 大致说一下做法.不愧是dls出的题 以前没见过这种类型的 不过还是自己dp的时候写丑了. 从这道题中得到一个结论 dp方程要写的优美一点 不过写的过丑 优化都优化不了. ...

  9. loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd

    LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...

  10. 小程序中 radio 的一个坑,到底 checked 该赋什么值?

    https://blog.csdn.net/henryhu712/article/details/83059365