题意:

有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. 10.Python中print函数中中逗号和加号的区别

    先看看print中逗号和加号分别打印出来的效果.. 这里以Python3为例 1 print("hello" + "world") helloworld 1 p ...

  2. list绑定

    /** * 首页信息查询的回调函数 * @param 返回值 */ M_Main.ajaxCallBack = function (data){ var dataSource = data.resiC ...

  3. codevs 3981 动态最大子段和(线段树)

    题目传送门:codevs 3981 动态最大子段和 题目描述 Description 题目还是简单一点好... 有n个数,a[1]到a[n]. 接下来q次查询,每次动态指定两个数l,r,求a[l]到a ...

  4. PTC热敏电阻的应用

      PTC热敏电阻应用举例 PTC热敏电阻可用于计算机及其外部设备.移动电话.电池组.远程通讯和网络装备.变压器.工业控制设备.汽车及其它电子产品中,作为开关类的PTC陶瓷元件,具有开发功能.使电器设 ...

  5. 微信小程序---自定义三级联动

    在开发的很多电商类型的项目中,免不了会遇到三级联动选择地址信息,如果单纯的使用文本框给用户选择,用户体检可能就会差很多.今天我给大家整理了关于小程序开发利用picker-view组件和animatio ...

  6. python报错:not supported between instances of 'str' and 'int'

    not supported between instances of 'str' and 'int' : 意思就是字符串不能和int类型的数值比较

  7. xtrabackup备份还原mariadb数据库

    一.xtrabackup 简介 xtrabackup 是由percona公司开源免费的数据库热备软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份,对于myisam的备份同样需要 ...

  8. 求1-n 中与 m 互质的素因子 (容斥原理)

    ll prime[100]; ll cnt; void getprime(){ cnt = 0; ll num = m; for(ll i = 2; i*i <= m; i++){ // sqr ...

  9. PTA - dfs

    地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...

  10. Pycharm 中的翻译工具

    对于开发来说,大多数哥们英文欠缺,比如在下,我们大多数使用的开发工具是IDEA,IDEA 很强大,开发起来顺手. 废话不多说,让我们看一下如何使用翻译器. 打开Pycharm 的setting 设置, ...