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 ...
随机推荐
- 网络摄像机进行互联网视频直播录像方案的选择,EasyNVS or EasyCloud or EasyGBS?
背景需求 互联网视频直播越来越成为当前大势:直播的需求往往都伴随在录像的需求,对于录像,不同的场景又有不同的方案选择: 本篇博客将会介绍对应的几种录像方案,可以帮助有互联网录像需求的用户进行对应的录像 ...
- POCO c++ 使用例子
.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...
- 内置函数:min 用法
内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...
- python生成器&迭代器
列表生成式 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 里每个值都加一 普通做法 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]for index,i in e ...
- python面向对象(二)
属性查找 类有两种属性:数据属性和函数属性 1. 类的数据属性是所有对象共享的 2. 类的函数属性是绑定给对象用的 class BeijingStudent: school='Beijing' ...
- Linux中的流程控制语句
if语句 if [ 条件判断式 ] then 程序elif [ 条件判断式 ] then 程序else 程序fi 注意: a.使用fi结尾 b.条件判断式和中括号之间需要有空格 [root@local ...
- win10笔记本触摸板手势大全
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- github 上 python 的优秀库推荐列表
awesome-python: https://github.com/vinta/awesome-python
- spring cloud 使用feign 遇到问题
spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...