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].
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]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LL is the length of [ ll, rr ] that equals to r - l + 1r−l+1).
Now Ryuji has qq questions, you should answer him:
11. If the question type is 11, you should answer how much knowledge he will get after he reads books [ ll, rr ].
22. If the question type is 22, Ryuji will change the ith book's knowledge to a new value.
Input
First line contains two integers nn and qq (nn, q \le 100000q≤100000).
The next line contains n integers represent a[i]( a[i] \le 1e9)a[i](a[i]≤1e9) .
Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ ll , rr ]. if a = 2a=2 , it means question type is 22 , and bb, cc means Ryuji changes the bth book' knowledge to cc
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
题意:问区间[l,r]中的每一个值乘以对应的权值求和;
思路:板子,维护两个区间和,照着板子都打错。。。
下面附上我的代码:
#include<bits/stdc++.h>
#define lson o<<1
#define rson o<<1|1
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
LL n, q, b, c;
struct Tree{
LL l, r;
LL sum;
};
Tree tree1[maxn << 2];
Tree tree2[maxn << 2];
LL a[maxn];
void Pushup(int o)
{
tree1[o].sum = tree1[lson].sum + tree1[rson].sum;
tree2[o].sum = tree2[lson].sum + tree2[rson].sum;
}
void build(LL o, LL l, LL r)
{
tree1[o].l = l, tree1[o].r = r;
tree2[o].l = l; tree2[o].r = r;
if(l == r)
{
LL a;
scanf("%lld", &a);
tree1[o].sum = a * (n - l + 1);
tree2[o].sum = a;
return ;
}
LL mid = (tree1[o].l + tree1[o].r) >> 1;
build(o<<1, l, mid);
build(o<<1|1, mid + 1, r);
Pushup(o);
}
void update(LL o, LL pos, LL val)
{
if(tree1[o].l == tree1[o].r)
{
tree1[o].sum = val * (n - tree1[o].l + 1);
tree2[o].sum = val;
return ;
}
LL mid = (tree1[o].l + tree1[o].r) >> 1;
if(mid >= pos)
update(lson, pos, val);
else
update(rson, pos, val);
Pushup(o);
}
LL Query(LL o, LL L, LL R)
{
if(L <= tree1[o].l && R >= tree1[o].r)
return tree1[o].sum;
LL mid = (tree1[o].l + tree1[o].r) >> 1;
if(R <= mid)
return Query(lson, L, R);
else if(L > mid)
return Query(rson, L, R);
else{
return Query(lson, L, mid) + Query(rson, mid + 1, R);
}
}
LL Query1(LL o, LL L, LL R)
{
if(L <= tree2[o].l && R >= tree2[o].r)
return tree2[o].sum;
LL mid = (tree2[o].l + tree2[o].r) >> 1;
if(R <= mid)
return Query1(lson, L, R);
else if(L > mid)
return Query1(rson, L, R);
else{
return Query1(lson, L, mid) + Query1(rson, mid + 1, R);
}
}
int main()
{
int e;
cin >> n >> q;
build(1, 1, n);
while(q--)
{
scanf("%d %lld %lld", &e, &b, &c);
if(e == 2)
update(1, b, c);
else
{
printf("%lld\n", Query(1, b, c) - Query1(1, b, c) * (n - c));
}
}
return 0;
}