http://acm.timus.ru/problem.aspx?space=1&num=2062

题意:有n个数,有一个值,q个询问,有单点询问操作,也有对于区间[l,r]的每个数i,使得num[i] + w, num[i*2] + w, num[i*3] + w……

思路:有树状数组和分块两种做法,对比后分块的速度比较快(暴力的艺术)。线段树会超时(可能写法不好)。

1、树状数组用到的是单点询问区间修改,我要注意一下其写法...修改为:Add(l, w), Add(r + 1, -w); 查询为:Sum(w);

bit[]维护增加的值,每次询问的时候只要去查询该位置的所有因子的位置增加的值,还有加上本来的数字就可以了。

2、分块的做法也是类似,因为单点修改的时候增加的值不能直接加在原来的num上,所以要多开一个every数组来记录单点修改。

查询的时候每个位置就是every[i] + block[belong[i]]。

也要注意一下分块的写法= =。

先判断是否在同一块,如果在同一块就暴力扫而不是直接block+w,因为L和R不一定在边界。。。

分块:

 #include <bits/stdc++.h>
using namespace std;
#define N 300010
typedef long long LL;
LL cnt, sz, block[N], every[N], l[N], r[N], belong[N], num[N], n, q; void Build() {
sz = sqrt(n);
cnt = n / sz; if(n % sz) cnt++;
for(int i = ; i <= cnt; i++)
l[i] = (i - ) * sz + , r[i] = i * sz;
r[cnt] = n;
for(int i = ; i <= n; i++)
belong[i] = (i - ) / sz + ;
} void Update(int L, int R, int w) {
if(belong[L] == belong[R])
for(int i = L; i <= R; i++) every[i] += w;
else {
for(int i = L; i <= r[belong[L]]; i++) every[i] += w;
for(int i = belong[L] + ; i <= belong[R] - ; i++) block[i] += w;
for(int i = l[belong[R]]; i <= R; i++) every[i] += w;
}
} LL solve(int n) {
int tmp = sqrt(n); LL ans = ;
for(int i = ; i <= tmp; i++) {
if(n % i == ) {
ans += block[belong[i]] + every[i];
if(i != n / i) ans += block[belong[n/i]] + every[n/i];
}
}
return ans;
} int main() {
ios::sync_with_stdio(false);
cin >> n;
for(int i = ; i <= n; i++) cin >> num[i];
Build();
cin >> q;
while(q--) {
int l, r, w, kind;
cin >> kind;
if(kind == ) {
cin >> w;
cout << solve(w) + num[w] << '\n';
cout.flush();
} else {
cin >> l >> r >> w;
Update(l, r, w);
}
}
}

树状数组:

 #include <bits/stdc++.h>
using namespace std;
#define N 300010
typedef long long LL;
LL bit[N], num[N];
int n, q;
// 区间更新,单点查询
int lowbit(int x) { return x & (-x); }
void Add(int x, int w) { for(int i = x; i <= n; i += lowbit(i)) bit[i] += w; }
LL Sum(int x) { LL ans = ; for(int i = x; i; i -= lowbit(i)) ans += bit[i]; return ans; }
LL solve(int n) {
int tmp = sqrt(n); LL ans = ;
for(int i = ; i <= tmp; i++) {
if(n % i == ) {
ans += Sum(i);
if(i != n / i) ans += Sum(n / i);
}
}
return ans;
} int main() {
ios::sync_with_stdio(false);
cin >> n;
for(int i = ; i <= n; i++) cin >> num[i];
cin >> q;
while(q--) {
int kind, l, r, w;
cin >> kind;
if(kind == ) {
cin >> w;
cout << solve(w) + num[w] << '\n';
cout.flush();
} else {
cin >> l >> r >> w;
Add(l, w); Add(r + , -w);
}
}
return ;
}

Ural 2062:Ambitious Experiment(树状数组 || 分块)的更多相关文章

  1. ural Ambitious Experiment 树状数组

    During several decades, scientists from planet Nibiru are working to create an engine that would all ...

  2. BZOJ_2141_排队_树状数组+分块

    BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...

  3. 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分

    题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...

  4. ural 2062 Ambitious Experiment

    2062. Ambitious Experiment Time limit: 3.0 secondMemory limit: 128 MB During several decades, scient ...

  5. bzoj 4765 普通计算姬(树状数组 + 分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=4765 很nice的一道题啊(可能是因为卡了n久终于做出来了 题意就是给你一棵带点权的有根树,sum( ...

  6. COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)

    题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树状数组+分块)

    Given a rooted tree ( the root is node 1 ) of N nodes. Initially, each node has zero point. Then, yo ...

  8. 【BZOJ2141】排队 树状数组+分块

    [BZOJ2141]排队 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备 ...

  9. Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块

    题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...

随机推荐

  1. Ubuntu 15.04 clang++ 3.6 编译boost 1.59/1.55

    Ubuntu 15.04已经可以直接通过apt-get insall 安装clang 3.6, 并且预装的gcc版本是4.9.2.这些安装过程在这里介绍. 首先下载boost源码 wget -O bo ...

  2. 通通玩blend美工(6)下——仿iPhone滚动选择器的ListBox(交互逻辑)

    原文:通通玩blend美工(6)下--仿iPhone滚动选择器的ListBox(交互逻辑) 上一篇我们已经把界面画出来了,这篇我们就来制作交互的逻辑吧.上一篇的电梯: http://www.cnblo ...

  3. WPF 验证错误模板

    <Window x:Class="BindingExam.MainWindow"        xmlns="http://schemas.microsoft.co ...

  4. jquery 点谁谁哭-隐式迭代

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  5. DOM解析xml实现读、写、增、删、改

    qt提供了三种方式解析xml,不过如果想实现对xml文件进行增.删.改等操作,还是DOM方式最方便. 项目配置 pro文件里面添加QT+=xml include <QtXml>,也可以in ...

  6. github中README.md文件写法解析,git指令速查表

    http://blog.csdn.net/u012234115/article/details/41778701 http://blog.csdn.net/u012234115/article/det ...

  7. Win8 Metro(C#)数字图像处理--2.66FloodFill算法

    原文:Win8 Metro(C#)数字图像处理--2.66FloodFill算法  [函数名称]   洪水填充算法函数 WriteableBitmap FloodfillProcess(Write ...

  8. Win8 Metro(C#)数字图像处理--2.63图像指数增强

    原文:Win8 Metro(C#)数字图像处理--2.63图像指数增强  [函数名称]   指数增强      WriteableBitmap IndexenhanceProcess(Writea ...

  9. Win8 Metro(C#) 数字图像处理--1 图像打开,保存

    原文:Win8 Metro(C#) 数字图像处理--1 图像打开,保存 作为本专栏的第一篇,必不可少的需要介绍一下图像的打开与保存,一便大家后面DEMO的制作.   Win8Metro编程中,图像相关 ...

  10. QML中实现setTimeout和setInterval

    Qt的QML中,js未提供setTimeout和setInterval,可以通过下面的代码实现. Timer {id: timer} function setTimeout(cb,delayTime) ...