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

H.Ryuji doesn't want to study

  •  27.34%
  •  1000ms
  •  262144K
 

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]×(L1)++a[r1]×2+a[r] (LL is the length of [ ll, rr ] that equals to r - l + 1rl+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 100000q100000).

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, ccrepresents [ 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

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

 

 

 

题意很好理解,我有两种方法来写这道题目。

第一种就是树状数组,倒的梯形面积。

因为下标是从1开始的,所以r+1。横坐标上面的是a数组,下面的是b数组。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<bitset>
 6 #include<cassert>
 7 #include<cctype>
 8 #include<cmath>
 9 #include<cstdlib>
10 #include<ctime>
11 #include<deque>
12 #include<iomanip>
13 #include<list>
14 #include<map>
15 #include<queue>
16 #include<set>
17 #include<stack>
18 #include<vector>
19 using namespace std;
20 typedef long long ll;
21 
22 const double PI=acos(-1.0);
23 const double eps=1e-6;
24 const ll mod=1e9+7;
25 const int inf=0x3f3f3f3f;
26 const int maxn=1e5+10;
27 const int maxm=1e3+10;
28 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
29 
30 ll a[maxn],b[maxn],n,q;
31 
32 int lowbit(int x)
33 {
34     return x&(-x);
35 }
36 
37 void add(ll a[],int x,ll val)
38 {
39     for(int i=x;i<=n;i+=lowbit(i))
40         a[i]+=val;
41 }
42 
43 ll query(ll a[],int x)
44 {
45     ll ans=0;
46     for(int i=x;i>0;i-=lowbit(i))
47         ans+=a[i];
48     return ans;
49 }
50 
51 int main()
52 {
53     cin>>n>>q;
54     for(int i=1;i<=n;i++){
55         ll val;
56         cin>>val;
57         add(a,i,val);//单纯的保存,类似前缀和
58         add(b,i,i*val);//这样保存,减去的时候正好满足条件,*L,*(L-1)。。。
59     }
60     while(q--){
61         ll op,l,r;
62         cin>>op>>l>>r;
63         if(op==2){
64             ll cnt=query(a,l)-query(a,l-1);//单点更新
65             add(a,l,r-cnt);
66             ll ret=query(b,l)-query(b,l-1);//同上
67             add(b,l,l*r-ret);
68         }
69         else{
70             ll cnt=(r+1)*(query(a,r)-query(a,l-1));//先算出横坐标的和,然后*(r+1)就是一个大矩形的面积
71             ll ret=query(b,r)-query(b,l-1);//倒着的梯形面积,(因为从1开始的,所以是梯形不是三角形)
72             cout<<cnt-ret<<endl;
73         }
74     }
75 }

还有一种线段树的方法,线段树的就是左儿子+右儿子+两者包围的矩形的面积。

为了好理解,画成三角形。

代码睡醒上完课再贴,想睡觉了,头发要紧,哈哈哈哈哈。

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/9631937.html