URAL 2062 树状数组
一个长度为n的数组 每次对lr区间进行修改 如果要修改i 则对i i*2 i*3...都修改 最后单点查询值
思想是利用树状数组维护每一个区间的更新值 查询的时候得出这个点的所有因子的查询值的和 加上这个点的最初值
因为对树状数组理解不深再次错过绝杀...
由于是维护每个点的修改量 所以每次修改 都进行add(l,val) add(r+1,-val)
这时候 如果要求单点的修改值 需要sum(x) 而非sum(x)-sum(x-1)
因为没有将初值进行add 所以c数组存放的其实不是一个前缀和 而是一个差分 差分的前缀和就是差分值
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define L long long
#define pb push_back
L n ;
L a[300050] ;
vector<L >v[1000050] ;
L c[3000050] ;
L lowbit(L x){
return (x&-x);
}
void add(L x,L w)
{
while(x<=n)
{
c[x]+=w;
x+=lowbit(x);
}
}
L sum(L x)
{
L tot=0;
while(x)
{
tot+=c[x];
x-=lowbit(x);
}
return tot;
}
int main(){
scanf("%lld",&n);
for(L i = 1; i <= n ; i ++ )
scanf("%lld",&a[i]);
for(L i = 1; i <= n ; i ++ ){
for(L j = 1; j * i <= n ; j ++) {
v[j*i].pb(i) ;
}
}
memset(c,0,sizeof(c));
L q;
scanf("%lld",&q);
while(q--){
L op;
scanf("%lld",&op);
if(op==2){
L l , r;
L val;
scanf("%lld%lld%lld",&l,&r,&val);
add(l,val);
add(r+1,-val);
}
else {
L w;
scanf("%lld",&w);
L ans=0;
for(L i=0;i<v[w].size();i++){
L x=v[w][i];
ans += sum(x) ;
}
printf("%lld\n",ans+a[w]);
}
}
}
..我的绝杀..
URAL 2062 树状数组的更多相关文章
- ural 1989(树状数组+多项式hash)
题意:给出一个字符串.有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串.还有一个操作 c a b,把字符串位置a的字符替换为b. 题解:由于字符串长度为1e5且问的次数也 ...
- Ural 2062:Ambitious Experiment(树状数组 || 分块)
http://acm.timus.ru/problem.aspx?space=1&num=2062 题意:有n个数,有一个值,q个询问,有单点询问操作,也有对于区间[l,r]的每个数i,使得n ...
- ural Ambitious Experiment 树状数组
During several decades, scientists from planet Nibiru are working to create an engine that would all ...
- HDU1541 树状数组
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- POJ 2352Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42898 Accepted: 18664 Descripti ...
- POJ-2352 Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
- hdu 1541/poj 2352:Stars(树状数组,经典题)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Stars(树状数组或线段树)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...
- POJ2352Stars【树状数组】
Stars Description Astronomers often examine star maps where stars are represented by points on a pla ...
随机推荐
- Android系统中Parcelable和Serializable的区别,自动化实现Parcelable接口的插件
Parcelable和Serializable的区别 参考地址:http://greenrobot.me/devpost/android-parcelable-serializable/ 由于最终的区 ...
- Codeforces Round #324 (Div. 2) (快速判断素数模板)
蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后C题这么简单的思路却一直卡到死,期间看了下D然后随便猜了下,暴力了下就过了. A.找一个能被t整除的n位数,那么除了<=10以外,其他都 ...
- 【BZOJ2819】Nim 树状数组+LCA
[BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...
- E - Rails (栈)
E - Rails Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description The ...
- Xmind 8 update 4 破解教程(破解补丁+破解步骤+下载地址)
一.原始教程 原教程很详细,直接参考: http://www.voidcn.com/article/p-yyybmcqq-bnz.html 若无法访问请点击:http://df1551e3.wiz03 ...
- (转)IOS崩溃 异常处理(NSSetUncaughtExceptionHandler)
iOS已发布应用中对异常信息捕获和处理 代码下载地址:http://download.csdn.net/detail/daiyelang/6740205 iOS开发中我们会遇到程序抛出异常退出的情况, ...
- 实用的 集合工具类 和 String工具类
集合工具类:CollectionUtil method: 1.isNotEmpty() 不为空 2.isEmpty() 为空 举例:map集合 Map<String,String ...
- 我的Java开发学习之旅------>Eclipse 项目有红感叹号解决之道
今天一个读者问我关于Android通过调用Webservice实现天气预报这篇文章的源码下载后出现的错误 Could not find class 'org.ksoap2.transport.Http ...
- 命令行查看mysql的安装目录
方法: 进入mysql命令行输入:show variables like "%char%"; 结果如下: 红色框框就是安装目录
- Eclipse中执行maven命令(转载)
转载自:http://blog.csdn.net/u011939453/article/details/43017865 1.如下图,右击需要执行maven命令的工程,选择"Debug As ...