Ural 2062:Ambitious Experiment(树状数组 || 分块)
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(树状数组 || 分块)的更多相关文章
- ural Ambitious Experiment 树状数组
During several decades, scientists from planet Nibiru are working to create an engine that would all ...
- BZOJ_2141_排队_树状数组+分块
BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...
- 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分
题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...
- ural 2062 Ambitious Experiment
2062. Ambitious Experiment Time limit: 3.0 secondMemory limit: 128 MB During several decades, scient ...
- bzoj 4765 普通计算姬(树状数组 + 分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=4765 很nice的一道题啊(可能是因为卡了n久终于做出来了 题意就是给你一棵带点权的有根树,sum( ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- 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 ...
- 【BZOJ2141】排队 树状数组+分块
[BZOJ2141]排队 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备 ...
- 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 ...
随机推荐
- Java之nio性能比较
结果:used time:53574684used time:1800077620used time:12563690可见MappedByteBuffer读写数据是最快的, 其次是FileChanne ...
- 数据在数组中存储的顺序:小端 OR 大端模式 详解
大端模式,是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放: 小端模式,是指数据的高 ...
- iOS UITableView动态隐藏或显示Item
通过改变要隐藏的item的高度实现隐藏和显示item 1.创建UITableView #import "ViewController.h" @interface ViewContr ...
- dotnet core 跨平台编译发布
vs2017 建立的项目,在项目目录 ,执行 dotnet publish -r ubuntu.15.04-x64 dotnet publish -r linux-x64 dotnet publish ...
- Android开发参考资料
Android入门第九篇之AlertDialog: http://blog.csdn.net/hellogv/article/details/5955959 Android的AlertDialog详解 ...
- Advanced Installer 安装前卸载旧版本的办法
原文:Advanced Installer 安装前卸载旧版本的办法 Advanced Installer这个工具百度出来的资料太少了. 在我们平常打包的工作中,经常遇到的一个问题是,如何能在安装新版本 ...
- Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案
原文:Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案 一打开虚拟机电脑就立马蓝屏重启,新建虚拟机也没用,然后就开始 ...
- extjs grid 复选框选择事件
开发中需求是统计选择的行数,所以要监控checkbox的选择事件包括表头的全选事件 遇到的问题就不赘述了 方案是监控grid的复选框和行加载时绑定事件 baseView: DBEN.controls. ...
- SQLite的使用(包括编译安装的步骤)
SQLite官网http://www.sqlite.org/ SQLite简介 SQLite是一款轻型的数据库,是遵守ACID(原子性.一致性.隔离性和持久性)的关系式数据库管理系统.SQLite实现 ...
- 如何把zip文件直接解压到内存里?
解压到硬盘再读进来耽误时间. var LZip: TZipFile; LMem: TMemoryStream; LBytes: TBytes;begin LZip := TZipFile.Cr ...