日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Greg and Array CodeForces - 296C(差分数组+线段树)

發布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Greg and Array CodeForces - 296C(差分数组+线段树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Greg has an array a?=?a1,?a2,?…,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?…,?ri by value di.

Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1?≤?xi?≤?yi?≤?m). That means that one should apply operations with numbers xi,?xi?+?1,?…,?yi to the array.

Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.

Input
The first line contains integers n, m, k (1?≤?n,?m,?k?≤?105). The second line contains n integers: a1,?a2,?…,?an (0?≤?ai?≤?105) — the initial array.

Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1?≤?li?≤?ri?≤?n), (0?≤?di?≤?105).

Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1?≤?xi?≤?yi?≤?m).

The numbers in the lines are separated by single spaces.

Output
On a single line print n integers a1,?a2,?…,?an — the array after executing all the queries. Separate the printed numbers by spaces.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20
題意:給定一組序列。總共有m個操作,每個操作就是一次區間更新。后面有k次讀入,每次讀入l~r,代表著l ~r中的操作都執行一次,輸出最后的序列是什么。
肯定不能直接暴力,每次都更新。我們可以離線一下,把所有操作會執行幾次給計算出來(利用差分數組),然后再更新的時候就直接把最后一次的給求出來,這樣就省時間了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; struct node{int l;int r;ll sum;ll lazy; }p[maxx<<2]; struct Node{int l;int r;ll v; }t[maxx]; int a[maxx]; ll b[maxx]; int n,m,k;inline void pushdown(int cur) {if(p[cur].lazy){p[cur<<1].lazy+=p[cur].lazy;p[cur<<1|1].lazy+=p[cur].lazy;p[cur<<1].sum+=p[cur].lazy;p[cur<<1|1].sum+=p[cur].lazy;p[cur].lazy=0;} } inline void build(int l,int r,int cur) {p[cur].l=l;p[cur].r=r;p[cur].sum=0;p[cur].lazy=0;if(l==r){p[cur].sum=b[l];return ;}int mid=l+r>>1;build(l,mid,cur<<1);build(mid+1,r,cur<<1|1); } inline void update(int l,int r,int cur,ll v) {int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r){p[cur].sum+=v;p[cur].lazy+=v;return ;}pushdown(cur);int mid=L+R>>1;if(r<=mid) update(l,r,cur<<1,v);else if(l>mid) update(l,r,cur<<1|1,v);else{update(l,mid,cur<<1,v);update(mid+1,r,cur<<1|1,v);} } inline void solve(int cur) {int L=p[cur].l;int R=p[cur].r;if(L==R) {cout<<p[cur].sum<<" ";return ;}pushdown(cur);solve(cur<<1);solve(cur<<1|1); } int main() {int x,y;while(~scanf("%d%d%d",&n,&m,&k)){memset(a,0,sizeof(a));for(int i=1;i<=n;i++) cin>>b[i];build(1,n,1);for(int i=1;i<=m;i++) cin>>t[i].l>>t[i].r>>t[i].v;for(int i=1;i<=k;i++){cin>>x>>y;a[x]++;a[y+1]--;}//差分數組的常規操作for(int i=1;i<=m;i++) a[i]+=a[i-1];//還原之前的數組for(int i=1;i<=m;i++) update(t[i].l,t[i].r,1,(ll)a[i]*t[i].v);solve(1);puts("");} }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Greg and Array CodeForces - 296C(差分数组+线段树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。