题意:

有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. 从 posix_spawn() 函数窥探漏洞逃逸

    posix_spawn() 函数是用来在Linux上创建子进程的,头文件是 #include <spawn.h> ,语法如下: #include <spawn.h> int p ...

  2. JUnit 5和Selenium基础(一)

    Gradle.JUnit 5和Jupiter Selenium Selenium是一组支持浏览器自动化的工具,主要用于Web应用程序测试.Selenium的组件之一是Selenium WebDrive ...

  3. Django之Session与Cookie

    目录 一.cookie Cookie的由来 什么是Cookie Cookie的原理 查看Cookie cookie与session的作用 二.Django中操作Cookie 获取Cookie 设置Co ...

  4. bzoj_1036 树链剖分套线段树

    bzoj_1036 ★★★★   输入文件:bzoj_1036.in   输出文件:bzoj_1036.out   简单对比时间限制:1 s   内存限制:162 MB [题目描述] 一棵树上有n个节 ...

  5. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  6. 外网主机远程ssh局域网Linux

    最近,公司有台配置极高的主机盒子(i7,32G,512G),组长让我装上kali,平时渗透测试时可以用,其余时间归我了,这么高配置的机器,怎么舍得让它吃灰呢 .所以我就去研究了一下,如何远程访问局域网 ...

  7. 关于idea修改当前使用的git账户的问题

    原文地址:https://www.cnblogs.com/xuxiaojian/p/8890656.html 1.问题描述: 由于前一段时间公司迁移git,就是将项目代码等迁移到另一个git服务器上, ...

  8. jQuery-01-jQuery选择器及元素操作

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. centos MySQL安装与卸载

    1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:https://dev.mysql.com/downloads/repo/yum/ wget http://dev.mysql.com/g ...

  10. kafka for centos7

    https://blog.csdn.net/wqh8522/article/details/79163467