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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The Trip On Abandoned Railway(线段树+树状数组)

發(fā)布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The Trip On Abandoned Railway(线段树+树状数组) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/problem/13891
來源:牛客網(wǎng)

題目描述
There are many ghosts at the abandoned station on unknown railway.

We mark the abandoned stations as 1,2…n according to the order. There are ai ghosts at the ith station.Yakumo Yukari often opens a black hole and makes a train appearing at a certain station. For example, the train appears at the x station, and k ghosts get off at the station. Then there would be k+d ghosts at the x+1 station to get off,k+2×d at x+2 station and so on…There would be k+y?d ghosts at the x+y station to get off (0≤y,x+y≤n). In others words, the numbers getting off at x,x+1,x+2…n station form a tolerance of d arithmetic progression.(you can consider ghosts getting off at the same time.)Onozuka Komachi would comes a certain station to take away the ghosts.(The number of ghosts at the station would become 0)You have the records of trains appearing and Komachi coming. You should tell Komachi how much ghosts at a certain station when she come to there.
輸入描述:
The first line contains an positive integer T(1≤T≤10), represents there are T test cases.
For each test case:
The first line contains three positive integers n,m,d(1≤n≤105,1≤m≤105,1≤d≤1000) - the number of station,the number of records,and the tolerance of the arithmetic progress.

The second line contains n integers a1,a2…an(1≤ai≤1000). Then m lines followed. Each line contains a records and there are two types. 1 x y,indicating train appearing at x station and y ghosts geting off. 2 x y,indicating Komachi coming to the x station. (1≤x≤n,0≤y≤1000)
輸出描述:
For each second records(2 x), output an integer in one line, representing the number of ghosts at the station.Since the ans may be too large, out put tme ans mod 109+7.
示例1
輸入
復(fù)制
2
6 6 1
1 2 3 3 2 1
1 1 1
2 1
2 2
2 3
2 4
2 5
5 3 2
1 2 3 4 5
1 3 0
2 4
2 4
輸出
復(fù)制
2
4
6
7
7
6
0
說明
There lists the number of ghosts changing at these station.
case1:
1 2 3 3 2 1
2 4 6 7 7 7
0 4 6 7 7 7
0 0 6 7 7 7
0 0 0 7 7 7
0 0 0 0 7 7
0 0 0 0 0 7
case2:
1 2 3 4 5
1 2 3 6 9
1 2 3 0 9
1 2 3 0 9
題意:題目大意:給你一個長度為n的數(shù)列和一個公差d,然后m個操作,操作分為兩種,第一種操作有一個x和y,代表從x開始的每個數(shù)按照等差數(shù)列開始加,x這個位置加上y,x+1這個位置加上y+d,x+2這個位置加上y+2*d,依次遞推;第二種操作有一個x,代表把這個位置的數(shù)模1e9+7后輸出,并且這個位置變成零。
思路:挺巧妙的一種思路。
對于1操作來說,x~n都會加上y,我們可以用樹狀數(shù)組或者線段樹對x ~n加上y。對于加上的d,跟位置有關(guān)系。那么我們在另一棵線段樹上對[x+1,n]都加上1,然后在求的時候,求[1,x]上1的個數(shù),就相當(dāng)于加上了多少個d。這個題目有個坑點,就是只對答案取模就可以,不用對其余的取模。
線段樹+樹狀數(shù)組

#include<bits/stdc++.h> #define ll long long #define mod 1000000007 using namespace std;const int maxx=1e5+100; struct node{int l;int r;ll sum;ll lazy1; }p[maxx<<2]; ll a[maxx]; ll c[maxx]; int n,m;ll d; /*-----------樹狀數(shù)組------------*/ inline int lowbit(int x){return x & -x;} inline void add(int cur,ll v) {while(cur<maxx){c[cur]=(c[cur]+v)%mod;cur+=lowbit(cur);} } inline ll Query(int cur) {ll ans=0;while(cur>0){ans=(ans+c[cur])%mod;cur-=lowbit(cur);}return ans; } /*------------線段樹-------------*/ inline void pushup(int cur) {p[cur].sum=(p[cur<<1].sum+p[cur<<1|1].sum); } inline void pushdown(int cur) {if(p[cur].lazy1){p[cur<<1].lazy1=(p[cur<<1].lazy1+p[cur].lazy1);p[cur<<1|1].lazy1=(p[cur<<1|1].lazy1+p[cur].lazy1);p[cur<<1].sum=(p[cur<<1].sum+(p[cur<<1].r-p[cur<<1].l+1)*1ll*p[cur].lazy1);p[cur<<1|1].sum=(p[cur<<1|1].sum+(p[cur<<1|1].r-p[cur<<1|1].l+1)*1ll*p[cur].lazy1);p[cur].lazy1=0;} } inline void build(int l,int r,int cur) {p[cur].l=l;p[cur].r=r;p[cur].lazy1=p[cur].sum=0;if(l==r) 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,ll v,int cur) {int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r){p[cur].lazy1=(p[cur].lazy1+v);p[cur].sum=(p[cur].sum+(R-L+1)*v);return ;}int mid=L+R>>1;pushdown(cur);if(r<=mid) update(l,r,v,cur<<1);else if(l>mid) update(l,r,v,cur<<1|1);else update(l,mid,v,cur<<1),update(mid+1,r,v,cur<<1|1);pushup(cur); } inline ll query(int l,int r,int cur) {int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r) return p[cur].sum;pushdown(cur);int mid=L+R>>1;ll ans;if(r<=mid) return query(l,r,cur<<1);else if(l>mid) return query(l,r,cur<<1|1);else return (query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1));return ans; } int main() {int t,op,x;ll y;scanf("%d",&t);while(t--){memset(c,0,sizeof(c));scanf("%d%d%lld",&n,&m,&d);for(int i=1;i<=n;i++) scanf("%lld",&a[i]);build(1,n,1);while(m--){scanf("%d",&op);if(op==1){scanf("%d%lld",&x,&y);add(x,y);update(x+1,n,1,1);}else{scanf("%d",&x);ll ans=(Query(x)+query(1,x,1)*d*1ll+a[x]);printf("%lld\n",ans%mod);a[x]-=ans;}}}return 0; }

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

總結(jié)

以上是生活随笔為你收集整理的The Trip On Abandoned Railway(线段树+树状数组)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。