差分数组,前缀和
Posted klaycf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了差分数组,前缀和相关的知识,希望对你有一定的参考价值。
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: QWX 4 > Mail: 5 > Created Time: 2018/11/11 9:43:08 6 ************************************************************************/ 7 8 9 //{{{ #include 10 #include<iostream> 11 #include<cstdio> 12 #include<algorithm> 13 #include<vector> 14 #include<cmath> 15 #include<queue> 16 #include<map> 17 #include<set> 18 #include<string> 19 #include<cstring> 20 #include<complex> 21 #include<cassert> 22 //#include<bits/stdc++.h> 23 #define vi vector<int> 24 #define pii pair<int,int> 25 #define mp make_pair 26 #define pb push_back 27 #define fi first 28 #define se second 29 #define pw(x) (1ll << (x)) 30 #define sz(x) ((int)(x).size()) 31 #define all(x) (x).begin(),(x).end() 32 #define rep(i,l,r) for(int i=(l);i<(r);i++) 33 #define per(i,r,l) for(int i=(r);i>=(l);i--) 34 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 35 #define cl(a,b) memset(a,b,sizeof(a)) 36 #define fastio ios::sync_with_stdio(false);cin.tie(0); 37 #define lson l , mid , ls 38 #define rson mid + 1 , r , rs 39 #define INF 0x3f3f3f3f 40 #define LINF 0x3f3f3f3f3f3f3f3f 41 #define ll long long 42 #define ull unsigned long long 43 #define dd(x) cout << #x << " = " << (x) << "," 44 #define de(x) cout << #x << " = " << (x) << " " 45 #define endl " " 46 using namespace std; 47 //}}} 48 49 const int N=1e5+7; 50 typedef int arr[N]; 51 arr a,s,d,ans; 52 int main() 53 { 54 int n;scanf("%d",&n); 55 FOR(i,1,n)scanf("%d",&a[i]); 56 FOR(i,1,n)scanf("%d",&s[i]),s[i]+=s[i-1]; 57 FOR(i,1,n){ 58 int t=lower_bound(s+i,s+n+1,s[i-1]+a[i])-s; 59 if(t==i)ans[i]+=a[i]; 60 else if(t==n+1)d[i]++; 61 else { 62 d[i]++,d[t]--,ans[t]+=a[i]-(s[t-1]-s[i-1]); 63 } 64 } 65 FOR(i,1,n)d[i]+=d[i-1],printf("%d%c",ans[i]+d[i]*(s[i]-s[i-1]),i==n?‘ ‘:‘ ‘); 66 return 0; 67 }
以上是关于差分数组,前缀和的主要内容,如果未能解决你的问题,请参考以下文章