#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define per(i,a,b) for(int i=a;i>b;i--) #define repLL(i,a,b) for(LL i=(LL)a;i<(LL)b;i++) #define perLL(i,a,b) for(LL i=(LL)a;i>(LL)b;i--) #define debug5(a,b,c,d,e) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<" "<<#e<<": "<<e<<endl; #define debug4(a,b,c,d) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<endl; #define debug3(a,b,c) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<endl; #define debug2(a,b) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<endl; #define debug(a) cerr<<#a<<": "<<a<<endl; #define pb push_back #define mp make_pair #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; const int maxn=(1e6)+7; const int inf=(1e9)+7; const LL LLinf=(1e18)+7; const LD eps=1e-9; const LL mod=1e9+7; // ***************************** CODE ***************************** // PLL zap[maxn]; LL lewo[maxn]; LL ile[maxn]; int F[maxn]; LL odp[maxn]; LL res; LL zmienna; int n,m; priority_queue<pair<LL,PII> > q; int fin(int x) { if(F[x]!=x) F[x]=fin(F[x]); return F[x]; } LL npo2(LL N) { return N*(N-1LL)/2LL; } inline void policz(LL liczba,LL lew,LL wsp) { zmienna+=wsp*npo2(liczba); res+=wsp*liczba*lew; } void uni(int x,int y) { // cout<<x<<" "<<y<<endl; x=fin(x); y=fin(y); if(x>y) swap(x,y); policz(ile[y],lewo[y],-1LL); policz(ile[x],lewo[x],-1LL); ile[x]+=ile[y]; policz(ile[x],lewo[x],1LL); F[y]=x; } inline void spr(int a,int b) { if(a<0 || b<0) return ; if(a>n || b>n) return ; a=fin(a); b=fin(b); if(a==b) return ; if(a>b) swap(a,b); // cout<<a<<" "<<b<<" "<<((lewo[b]-lewo[a]+ile[a])/(ile[a]+1))<<endl; q.push({-((lewo[b]-lewo[a]+ile[a])/(ile[a])),{a,b}}); } int main() { _upgrade cin>>n>>m; ile[0]=1; rep(i,1,n+1) { cin>>lewo[i]; F[i]=i; ile[i]=1; spr(i-1,i); } rep(i,0,m) { cin>>zap[i].fi; // if(i==11635 || i==11634) // cout<<zap[i].fi<<endl; zap[i].se=i; } sort(zap,zap+m); rep(i,0,m) { while(SZ(q) && -q.top().fi<=zap[i].fi) { PII a=q.top().se; q.pop(); a.fi=fin(a.fi); a.se=fin(a.se); if(a.fi==a.se) continue; uni(a.fi,a.se); spr(a.fi,fin(a.fi-1)); spr(a.fi,a.fi+ile[a.fi]); } // cout<<zap[i].fi<<" "<<zmienna<<endl; odp[zap[i].se]=res+(LL)zap[i].fi*zmienna; } // cout<<res<<" "<<zmienna; rep(i,0,m) cout<<odp[i]<<"\n"; return 0; }
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define per(i,a,b) for(int i=a;i>b;i--) #define repLL(i,a,b) for(LL i=(LL)a;i<(LL)b;i++) #define perLL(i,a,b) for(LL i=(LL)a;i>(LL)b;i--) #define debug5(a,b,c,d,e) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<" "<<#e<<": "<<e<<endl; #define debug4(a,b,c,d) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<endl; #define debug3(a,b,c) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<endl; #define debug2(a,b) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<endl; #define debug(a) cerr<<#a<<": "<<a<<endl; #define pb push_back #define mp make_pair #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; const int maxn=(1e6)+7; const int inf=(1e9)+7; const LL LLinf=(1e18)+7; const LD eps=1e-9; const LL mod=1e9+7; // ***************************** CODE ***************************** // PLL zap[maxn]; LL lewo[maxn]; LL ile[maxn]; int F[maxn]; LL odp[maxn]; LL res; LL zmienna; int n,m; priority_queue<pair<LL,PII> > q; int fin(int x) { if(F[x]!=x) F[x]=fin(F[x]); return F[x]; } LL npo2(LL N) { return N*(N-1LL)/2LL; } inline void policz(LL liczba,LL lew,LL wsp) { zmienna+=wsp*npo2(liczba); res+=wsp*liczba*lew; } void uni(int x,int y) { // cout<<x<<" "<<y<<endl; x=fin(x); y=fin(y); if(x>y) swap(x,y); policz(ile[y],lewo[y],-1LL); policz(ile[x],lewo[x],-1LL); ile[x]+=ile[y]; policz(ile[x],lewo[x],1LL); F[y]=x; } inline void spr(int a,int b) { if(a<0 || b<0) return ; if(a>n || b>n) return ; a=fin(a); b=fin(b); if(a==b) return ; if(a>b) swap(a,b); // cout<<a<<" "<<b<<" "<<((lewo[b]-lewo[a]+ile[a])/(ile[a]+1))<<endl; q.push({-((lewo[b]-lewo[a]+ile[a])/(ile[a])),{a,b}}); } int main() { _upgrade cin>>n>>m; ile[0]=1; rep(i,1,n+1) { cin>>lewo[i]; F[i]=i; ile[i]=1; spr(i-1,i); } rep(i,0,m) { cin>>zap[i].fi; // if(i==11635 || i==11634) // cout<<zap[i].fi<<endl; zap[i].se=i; } sort(zap,zap+m); rep(i,0,m) { while(SZ(q) && -q.top().fi<=zap[i].fi) { PII a=q.top().se; q.pop(); a.fi=fin(a.fi); a.se=fin(a.se); if(a.fi==a.se) continue; uni(a.fi,a.se); spr(a.fi,fin(a.fi-1)); spr(a.fi,a.fi+ile[a.fi]); } // cout<<zap[i].fi<<" "<<zmienna<<endl; odp[zap[i].se]=res+(LL)zap[i].fi*zmienna; } // cout<<res<<" "<<zmienna; rep(i,0,m) cout<<odp[i]<<"\n"; return 0; } |