博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ACM-ICPC 2018 徐州赛区网络预赛 徐州 H Ryuji doesn't want to study 线段树
阅读量:3906 次
发布时间:2019-05-23

本文共 2874 字,大约阅读时间需要 9 分钟。

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]a[i]a[i].

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

That means, if he reads books from lll to rrr, he will get a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LLL is the length of [ lll, rrr ] that equals to r−l+1r - l + 1r−l+1).

Now Ryuji has qqq questions, you should answer him:

111. If the question type is 111, you should answer how much knowledge he will get after he reads books [ lll, rrr ].

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

Input

First line contains two integers nnn and qqq (nnn, q≤100000q \le 100000q≤100000).

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

Then in next qqq line each line contains three integers aaa, bbb, ccc, if a=1a = 1a=1, it means question type is 111, and bbb, ccc represents [ lll , rrr ]. if a=2a = 2a=2 , it means question type is 222 , and bbb, ccc means Ryuji changes the bth book' knowledge to ccc

Output

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

样例输入复制

5 31 2 3 4 51 1 32 5 01 4 5

样例输出复制

108

题目来源

将a[i]*(r-i+1)化成a[i]*(r+1)-a[i] 然后建两个二叉树, 一个维护区间和, 一个维护前缀区间和。

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=100005;long long int a[maxn];long long int t1[maxn<<2];long long int t2[maxn<<2];int n,q;void pushup (int re,long long int* t){ t[re]=t[re<<1]+t[re<<1|1];}void build1 (int l,int r,int re,long long int* t){ if(l==r) { t[re]=a[l]; return; } int mid=(l+r)>>1; build1 (l,mid,re<<1,t); build1 (mid+1,r,re<<1|1,t); pushup (re,t);}void build2 (int l,int r,int re,long long int* t){ if(l==r) { t[re]=a[l]*l; return; } int mid=(l+r)>>1; build2 (l,mid,re<<1,t); build2 (mid+1,r,re<<1|1,t); pushup (re,t);}void update1 (int l,int r,int re,int loc,long long int data,long long int* t){ if(l==r) { t[re]=data; return; } int mid=(l+r)>>1; if(mid>=loc) update1 (l,mid,re<<1,loc,data,t); else update1 (mid+1,r,re<<1|1,loc,data,t); pushup(re,t);}void update2 (int l,int r,int re,int loc,long long int data,long long int* t){ if(l==r) { t[re]=data*l; return; } int mid=(l+r)>>1; if(mid>=loc) update2 (l,mid,re<<1,loc,data,t); else update2 (mid+1,r,re<<1|1,loc,data,t); pushup(re,t);}long long int query (int l,int r,int re,int left,int right,long long int* t){ if(l>=left&&r<=right) return t[re]; long long int ans=0; int mid=(l+r)>>1; if(mid>=left) ans+=query (l,mid,re<<1,left,right,t); if(mid

 

转载地址:http://ytaen.baihongyu.com/

你可能感兴趣的文章
4. 两个排序数组的中位数
查看>>
10. 正则表达式匹配
查看>>
23. 合并K个元素的有序链表
查看>>
32. 最长有效括号
查看>>
6. Z字形转换
查看>>
8. 字符串转整数(atoi)
查看>>
12. 整数转罗马数字
查看>>
15. 三数之和
查看>>
16. 最接近的三数之和
查看>>
18. 四数之和
查看>>
22. 括号生成
查看>>
24. 两两交换链表中的节点
查看>>
71. 简化路径
查看>>
77. 组合
查看>>
78. 子集
查看>>
89. 格雷编码
查看>>
刚开始学python,对脚本语言的一些理解
查看>>
matplotlib进行绘图——散点图
查看>>
matplotlib进行绘图——直方图
查看>>
需求文件requirements.txt的创建及使用
查看>>