题目链接:https://nanti.jisuanke.com/t/31460

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge $a[i]$.

Unfortunately, the longer he learns, the fewer he gets.

That means, if he reads books from ll to rr, he will get $a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]$ ($L$ is the length of [ $l$, $r$ ] that equals to $r - l + 1$).

Now Ryuji has qq questions, you should answer him:

1. If the question type is 1, you should answer how much knowledge he will get after he reads books [ $l$, $r$ ].

2. If the question type is 2, Ryuji will change the ith book's knowledge to a new value.

Input
First line contains two integers $n$ and $q$ ($n$, $q \le 100000$).

The next line contains n integers represent $a[i]$($a[i] \le 1e9$).

Then in next qq line each line contains three integers $a,b,c$, if $a = 1$, it means question type is $1$, and $b$, $c$ represents [ $l$ , $r$ ].

If $a = 2$, it means question type is $2$ , and $b$, $c$ means Ryuji changes the bth book' knowledge to $c$.

Output
For each question, output one line with one integer represent the answer.

样例输入

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出

10
8

题意:

给出 $n$ 本书编号 $1$ 到 $n$,每本书权值为 $w[i]$,给出 $q$ 个操作,

操作 $1$,给出区间 $[l,r]$,则区间长度为 $L = r - l + 1$,查询的答案应为 $a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]$,

操作 $2$,把在编号为 $b$ 的书的权值改成 $c$。

题解:

线段树维护两个和:

一个是普通的区间和 $\sum\limits_{i = l}^r {w[i]} = w[l] + \cdots + w[r]$;

另一个是 $\sum\limits_{i = l}^r {\left[ {w[i] \times \left( {n - i + 1} \right)} \right]} = w[l] \times \left( {n - l + 1} \right) + \cdots + w[r] \times \left( {n - r + 1} \right)$。

那么,对于所有的查询:

$\begin{array}{l}
Q\left( {l,r} \right) \\
= w\left[ l \right] \times \left( {r - l + 1} \right) + w\left[ {l + 1} \right] \times \left( {r - l} \right) + \cdots + w\left[ r \right] \times 1 \\
= \sum\limits_{i = l}^r {\left[ {w\left[ i \right] \times \left( {r - i + 1} \right)} \right]} \\
= \sum\limits_{i = l}^r {\left[ {w\left[ i \right] \times \left( {n - i + 1 - n + r} \right)} \right]} \\
{\rm{ = }}\sum\limits_{i = l}^r {\left[ {w\left[ i \right] \times \left( {n - i + 1} \right) - w\left[ i \right] \times \left( {n - r} \right)} \right]} \\
= \sum\limits_{i = l}^r {\left[ {w\left[ i \right] \times \left( {n - i + 1} \right)} \right]} - \left( {n - r} \right)\sum\limits_{i = l}^r {w\left[ i \right]} \\
\end{array}$

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+; int n,q;
ll a[maxn]; /********************************* Segment Tree - st *********************************/
struct Node{
int l,r;
ll val,sum;
}node[*maxn];
void pushup(int root)
{
node[root].val=node[root*].val+node[root*+].val;
node[root].sum=node[root*].sum+node[root*+].sum;
}
void build(int root,int l,int r)
{
if(l>r) return;
node[root].l=l; node[root].r=r;
node[root].val=; node[root].sum=;
if(l==r)
{
node[root].val=a[l];
node[root].sum=a[l]*(n-l+);
}
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
void update(int root,int pos,ll val)
{
if(node[root].l==node[root].r)
{
node[root].val=val;
node[root].sum=val*(n-pos+);
return;
}
int mid=node[root].l+(node[root].r-node[root].l)/;
if(pos<=mid) update(root*,pos,val);
if(pos>mid) update(root*+,pos,val);
pushup(root);
}
ll askval(int root,int st,int ed)
{
if(st>node[root].r || ed<node[root].l) return ;
if(st<=node[root].l && node[root].r<=ed) return node[root].val;
else return askval(root*,st,ed)+askval(root*+,st,ed);
}
ll asksum(int root,int st,int ed)
{
if(st>node[root].r || ed<node[root].l) return ;
if(st<=node[root].l && node[root].r<=ed) return node[root].sum;
else return asksum(root*,st,ed)+asksum(root*+,st,ed);
}
/********************************* Segment Tree - ed *********************************/ int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
build(,,n);
for(int i=;i<=q;i++)
{
int type;
scanf("%d",&type);
if(type==)
{
int a,b;
scanf("%d%d",&a,&b);
ll A=asksum(,a,b);
ll B=askval(,a,b);
//cout<<A<<" "<<B<<endl;
printf("%lld\n",A-(n-b)*B);
}
else
{
int a; ll b;
scanf("%d%lld",&a,&b);
update(,a,b);
}
}
}

计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]的更多相关文章

  1. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  2. 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...

  3. 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...

  4. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...

  5. 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)

    J. Sum 26.87% 1000ms 512000K   A square-free integer is an integer which is indivisible by any squar ...

  6. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  7. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  8. 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]

    题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...

  9. 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]

    题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...

随机推荐

  1. NHibernate 数据查询之Linq to NHibernate

    刚学NHibernate的时候觉得,HQL挺好用的,但是终归没有与其他技术相关联,只有NHibernate用到,一来容易忘记,二来没有智能提示,排除错误什么的都不给力,直到看到一个同事用Linq to ...

  2. SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定

    springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...

  3. Ansible 远程执行脚本

    1. 先在服务端创建一个 shell 脚本 [root@localhost ~]$ cat /tmp/test.sh #!/bin/bash echo "hello world" ...

  4. oracle nvl,having的用法

    select oi.order_id,opl.payment_no,opl.back_no, oi.commit_time, oi.receive_mobile, oi.receive_user, n ...

  5. api 25 PopupWindow会占据整个屏幕

    解决方法:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.x中,PopupWindow高度为match_pa ...

  6. c语言指针笔记

    一.int a[20]1. 数组名代表数组首元素的地址,不代表数组的地址2. 对数组名取地址代表整个数组的地址.a和&a代表的数据类型不一样 a代表数组首元素的地址 &a数组类型 in ...

  7. codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/268/A题意:足球比赛中如果主场球队的主场球衣和客队的客队球衣颜色一样,那么要求主队穿上他们的可对 ...

  8. [转]java中判断字符串是否为数字的三种方法

    1用JAVA自带的函数public static boolean isNumeric(String str){  for (int i = str.length();--i>=0;){      ...

  9. 《转》python学习(11)-表达式和语句

    转自 http://www.cnblogs.com/BeginMan/p/3164600.html 一.Python语句 if语句.else语句.elif语句.条件表达式.while语句.for语句. ...

  10. liunx trac 安装记录

    1,下载地址 http://trac.edgewall.org/   2.安装 apache,python, mysql  3,安装trac (我的是0.12)  tar -zxvf  你下载的安装包 ...