题目链接: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.

样例输入

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

样例输出

  1. 10
  2. 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代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn=1e5+;
  5.  
  6. int n,q;
  7. ll a[maxn];
  8.  
  9. /********************************* Segment Tree - st *********************************/
  10. struct Node{
  11. int l,r;
  12. ll val,sum;
  13. }node[*maxn];
  14. void pushup(int root)
  15. {
  16. node[root].val=node[root*].val+node[root*+].val;
  17. node[root].sum=node[root*].sum+node[root*+].sum;
  18. }
  19. void build(int root,int l,int r)
  20. {
  21. if(l>r) return;
  22. node[root].l=l; node[root].r=r;
  23. node[root].val=; node[root].sum=;
  24. if(l==r)
  25. {
  26. node[root].val=a[l];
  27. node[root].sum=a[l]*(n-l+);
  28. }
  29. else
  30. {
  31. int mid=l+(r-l)/;
  32. build(root*,l,mid);
  33. build(root*+,mid+,r);
  34. pushup(root);
  35. }
  36. }
  37. void update(int root,int pos,ll val)
  38. {
  39. if(node[root].l==node[root].r)
  40. {
  41. node[root].val=val;
  42. node[root].sum=val*(n-pos+);
  43. return;
  44. }
  45. int mid=node[root].l+(node[root].r-node[root].l)/;
  46. if(pos<=mid) update(root*,pos,val);
  47. if(pos>mid) update(root*+,pos,val);
  48. pushup(root);
  49. }
  50. ll askval(int root,int st,int ed)
  51. {
  52. if(st>node[root].r || ed<node[root].l) return ;
  53. if(st<=node[root].l && node[root].r<=ed) return node[root].val;
  54. else return askval(root*,st,ed)+askval(root*+,st,ed);
  55. }
  56. ll asksum(int root,int st,int ed)
  57. {
  58. if(st>node[root].r || ed<node[root].l) return ;
  59. if(st<=node[root].l && node[root].r<=ed) return node[root].sum;
  60. else return asksum(root*,st,ed)+asksum(root*+,st,ed);
  61. }
  62. /********************************* Segment Tree - ed *********************************/
  63.  
  64. int main()
  65. {
  66. scanf("%d%d",&n,&q);
  67. for(int i=;i<=n;i++) scanf("%d",&a[i]);
  68. build(,,n);
  69. for(int i=;i<=q;i++)
  70. {
  71. int type;
  72. scanf("%d",&type);
  73. if(type==)
  74. {
  75. int a,b;
  76. scanf("%d%d",&a,&b);
  77. ll A=asksum(,a,b);
  78. ll B=askval(,a,b);
  79. //cout<<A<<" "<<B<<endl;
  80. printf("%lld\n",A-(n-b)*B);
  81. }
  82. else
  83. {
  84. int a; ll b;
  85. scanf("%d%lld",&a,&b);
  86. update(,a,b);
  87. }
  88. }
  89. }

计蒜客 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. 条件独立(conditional independence) 结合贝叶斯网络(Bayesian network) 概率有向图 (PRML8.2总结)

    本文会利用到上篇,博客的分解定理,需要的可以查找上篇博客 D-separation对任何用有向图表示的概率模型都成立,无论随机变量是离散还是连续,还是两者的结合. 部分图为手写,由于本人字很丑,望见谅 ...

  2. swoole的进程模型架构

    swoole的强大之处就在与其进程模型的设计,既解决了异步问题,又解决了并行. 主线程MainReactor swoole启动后主线程会负责监听server socket,如果有新的连接accept, ...

  3. photoshop制作简单ico图标

    新建16 * 16透明画布 字体20px 半径4px

  4. 【视频】ffmpeg mov mp4 m3u8 ts

    1.https://ffmpeg.zeranoe.com/builds/ 2.https://blog.csdn.net/psh18513234633/article/details/79312607 ...

  5. PostgreSQL存储过程(1)-基于SQL的存储过程

    什么是SQL函数? SQL函数包体是一些可执行的SQL语言.同时包含1条以上的查询,但是函数只返回最后一个查询(必须是SELECT)的结果. 除非SQL函数声明为返回void,否则最后一条语句必须是S ...

  6. 【摘抄】C++程序员练级攻略

    摘抄自互联网文章 作为C++程序员,或者说程序员一定要提升自己: 专访李运华:程序员如何在技术上提升自己-CSDN.NET专访徐宜生:坚决不做代码搬运工!-CSDN.NET 上面两个文章我觉得都不错. ...

  7. Java的Integer和int有什么区别

    Java是面向对象的编程语言,一切都是对象,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cla ...

  8. MVC的路由设置【转】

    转,MVC的路由设置. 后端获取路由里面action的参数,函数需要设置相同的参数名称才行. routes.MapRoute( "Default", "{controll ...

  9. 【Linux基础】Linux基础命令行学习笔记

    绝对路径:cd /home/python相对路径:cd Downloads . 表示:当前那路径..表示:当前路径的上一层../.. 表示:当前路径的上二层 没有...或者以上的 ls: ls 查看当 ...

  10. linux下 php 安装mysql的扩展模块

    1.安装mysql-devel包 [root@DBproxy ~]# yum install mysql-devel 注:该包必须在编译php之前安装好,否则在安装php的mysql扩展模块是会碰到各 ...