题意:

有n个数的一个数组a,有两个操作:

1 l r:查询区间[l,r]内$a[l]*(r-l+1)+a[l+1]*(r-l)+a[l+2]*(r-l-1)+\cdots+a[r-1]*2+a[r]$

2 l r:将a[l]修改为r

n<=1e5,  a[i]<=1e9

思路:

预处理出前缀和s[i], 则操作1变为查询$s[l]+s[l+1]+..+s[r]-(r-l+1)*s[l-1]$

为防止爆ll(其实也不会爆的)可以在查询操作力就提前减去s[l-1]

坑点来了。。操作2即区间s(l,n)加上r-a[l],由于我们线段树里的一些标记操作,实际上a[i]和s[i]数组并没有变!

所以我们每次需要用s或a数组的时候都要query。。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);
ll sum[maxn<<];
ll s[maxn];
ll a[maxn];
int ii;
void build(int l, int r, int root){
int mid = l + ((r - l) >> );//位运算TMD优先级!
if(l == r){
sum[root] = s[l];
return;
}
build(lson);
build(rson);
sum[root] = sum[lc]+sum[rc];
} ll addv[maxn << ];
//将root的信息传到左右节点上
void pushup(int root){
sum[root] = sum[lc] + sum[rc];
return;
}
void pushdown(int l, int r, int root){
if(addv[root]){
addv[lc] += addv[root];
addv[rc] += addv[root];
int mid = l + ((r-l)>>);
sum[lc] += addv[root]*(mid-l+);
sum[rc] += addv[root]*(r-mid);
addv[root] = ;
}
return;
}
void update(int ql, int qr, ll add, int l, int r, int root){
if(ql <= l && qr >= r){
addv[root] += add;
sum[root] += add*(r-l+);
return;
}
pushdown(l, r, root);
int mid = l + ((r-l)>>);
if(ql <= mid) update(ql, qr, add, lson);
if(qr > mid) update(ql, qr, add, rson);
pushup(root);
return;
}
ll query(int ql, int qr, int l, int r, int root){
if(ql==)return ;
if(ql <= l && qr >= r) return sum[root];//(sum[root]-(ll)((ll)r-l+1)*s[ii-1]);
pushdown(l, r, root);
int mid = l + ((r-l)>>);
ll ans = ;
if(ql <= mid) ans += query(ql, qr, lson);
if(qr > mid) ans += query(ql, qr, rson);
return ans;
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for(int i =; i <= n ; i++){
scanf("%lld", &a[i]);
}s[] = ;
mem(s, );
for(int i = ; i <= n; i++){
s[i] = a[i]+s[i-];
}
mem(addv, );
mem(sum, );
build(, n, );
while(q--){
int c,l,r;
scanf("%d %d %d", &c, &l, &r);
if(c==){
ii = l;
printf("%lld\n", query(l, r, , n, ) -(r-l+)*query(l-,l-,,n,));
}
else if(c==){
ll tmp = (ll)r-(query(l, l, , n, ) -query(l-, l-, , n, ));
update(l, n, tmp, , n, );
}A
}
return ;
}
/*
5 10
1000000000 1000000000 1000000000 1000000000 1000000000
1 2 4
2 1 0
1 2 4
*/

2018icpc徐州网络赛-H Ryuji doesn't want to study(线段树)的更多相关文章

  1. 2018徐州网络赛H. Ryuji doesn't want to study

    题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1], ...

  2. ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解

    题意:给你数组a,有两个操作 1 l r,计算l到r的答案:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)

    Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...

  4. 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]

    题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study (线段树)

    Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...

  6. 南昌网络赛 I. Max answer (单调栈 + 线段树)

    https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...

  7. ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study

    死于update的一个long long写成int了 真的不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/ ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study (树状数组差分)

    https://nanti.jisuanke.com/t/31460 题意 两个操作.1:查询区间[l,r]的和,设长度为L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+...+a[ ...

随机推荐

  1. 深入 Create React App 核心概念

    本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...

  2. bash shell-linux的预设shell

    1.bash 是GNU 计划中重要的工具软件之一,目前也是Linux distributions 的标准shell.其主要功能如下: (1)命令记忆和历史功能,可以通过history查询,存储位置在~ ...

  3. POJ 2318 TOYS(叉积+二分)

    题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...

  4. 【linux学习笔记】

    网上看一个两小时突击linux的教程,就想补充一下linux的知识.想着一天抽出俩小时立马就能学完呢,结果乱七八糟的事情拖了四五天,实际完成某项任务的时间超出预期完成任务的两部不止.好了," ...

  5. Python - 线性回归(Linear Regression) 的 Python 实现

    背景 学习 Linear Regression in Python – Real Python,前面几篇文章分别讲了"regression怎么理解","线性回归怎么理解& ...

  6. vue 移动端在div上绑定click事件 失效

    在.vue的文件中使用了better-scroll,在div标签上绑定click事件后,无效. 原因:使用了better-scroll,默认它会阻止touch事件.所以在配置中需要加上click: t ...

  7. 通过例子进阶学习C++(四)计算2的64次方,不服写写看

    ​ 本文是通过例子学习C++的第四篇,通过这个例子可以快速入门c++相关的语法. 1.乍一看题目非常简单,简单思考一下,可以通过for循环实现: #include <iostream> u ...

  8. MapGIS注记文字无损转入ArcGIS软件

    在GIS软件中,注释是一种十分特殊的对象,虽然各类软件都支持注释,但它却不属于GIS的基本对象.因此通常的格式转换软件,都不对注释对象做特别的支持,我们最常见的Shape文件格式就只有点.线.面要素, ...

  9. docker使用阿里云加速器

    1 登录阿里云获得地址 登录https://cr.console.aliyun.com ,点击"镜像加速器",会给我一个地址. 2 写入/etc/docker/daemon.jso ...

  10. Spring Boot2 系列教程 (三) | 使用 LomBok 提高开发效率

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 上周去了开年会,去的地方是温泉度假村.老实说,我是无感的,90% 是因为没中奖(老板太抠,两百人只抽三个奖),10 ...