1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<cstdio> #include<vector> using namespace std; using ll=long long; const int N=1000010,FSIZE=1<<24; int n,m,l[N],r[N]; ll ans; vector<int> h[N]; vector<pair<int,int>> s[N]; struct FT{ ll a[N]; void modify(int x,int y,ll c){ a[x]+=c; a[++y]-=c; for(;x!=y;) if(x<y) a[x+=x&-x]+=c; else a[y+=y&-y]-=c; } ll query(int x){ ll re=0; for(;x;x-=x&-x) re+=a[x]; return(re); } }t; char BuF[FSIZE],*InF=BuF; template<typename T>void read(T &x){ for(;48>*InF||*InF>57;++InF); for(x=0;47<*InF&&*InF<58;x=x*10+(*InF++^48)); } int get(int *s,int x){return(s[x]?s[x]=get(s,s[x]):x);} int main(){ fread(BuF,1,FSIZE,stdin); read(n); for(int i=1,x;i<=n;++i){ read(x); h[x].push_back(i); } read(m); for(int i=1,x,y,c;i<=m;++i){ read(x);read(y);read(c); s[y].emplace_back(x,c); } for(int i=1;i<=n;++i){ for(auto j:s[i]){ int p=j.first; ll x=t.query(p); if(x<j.second){ ans+=x; t.modify(get(l,p),get(r,p),j.second-x); }else ans+=j.second; } for(auto j:h[i]) l[j+1]=r[j-1]=j; } printf("%lld\n",ans); return(0); }
|