#include <bits/stdc++.h> #define int __int128 #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=300007; int ans[2*N]; int seg(int l,int r,int n) { r=min(r,n); if(l>r) return 0; int ans=(n+1)*(r-l+1); ans-=(r*(r+1)/2); ans+=(l*(l-1)/2); return ans; } void calc(vector<pair<int,int>>a,int n,int m) { for(int j=1; j<n+m+1;j++) { vector<pair<int,int>>na; int S1=0,S2=0,ile=0,ile0=n+1; int X=0,Y=0,Z=0; int last=0,T=0,kon=0,B=0; vector<int>pos; deque<pair<int,int>>st; for(auto [l,r]:a) { //przedzialy z dziury do dziury int rr=l-kon-1; ile+=(rr*(rr+1)/2); ile0+=(rr*(rr+1)/2); //przedialy z dziury do poprzednich while(sz(st)>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } ile+=rr*(kon-B); ile0+=rr*(kon-last); S2+=rr*T*X; S2-=rr*Y; S1+=rr*T*T*X; S1+=rr*Z; S1-=rr*2*T*Y; kon=l-1; //przedzialy z kloca poza kloca int d=r-l+1; for(int t=0;t<=m&&j*t+1<=d;t++) { //usuwamy cos ze stosu while(sz(st)+t>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } //ile poczatkow int L=min(j,d-j*t); //obliczenia ile+=L*(kon-B); //nic nie ma int z=kon-last; if(t==0) ile0+=L*z; S2+=L*t*z; S1+=L*t*t*z; //cos jest S2+=L*(T+t)*X; S2-=L*Y; S1+=L*(T+t)*(T+t)*X; S1+=L*Z; S1-=L*2*(T+t)*Y; } // cout<<S1<<" "<<S2<<" "<<ile<<" "<<ile0<<endl; //przedzialy z kloca do kloca for(int t=0;t<=m&&j*t+1<=d;t++) { int xx=seg(j*t+1,(t+1)*j,d); //cout<<t<<" "<<xx<<endl; ile+=xx; if(t==0) ile0+=xx; S1+=t*t*xx; S2+=t*xx; } //dodajemy nowe punkty int x=r-j; vector<int>pts; while(x>=l) { pts.pb(x); x-=j; } reverse(all(pts)); for(auto i:pts) { st.pb({i-last,T}); X+=st.back().st; Y+=st.back().st*st.back().nd; Z+=st.back().st*st.back().nd*st.back().nd; pos.pb(i); T++; last=i; } kon=r; if(d>j+1) na.pb({l,r}); } //dodac koncowke int rr=n-kon; ile+=(rr*(rr+1)/2); ile0+=(rr*(rr+1)/2); while(sz(st)>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } ile+=rr*(kon-B); ile0+=rr*(kon-last); S2+=rr*T*X; S2-=rr*Y; S1+=rr*T*T*X; S1+=rr*Z; S1-=rr*2*T*Y; ile+=n+1; ans[j]+=(m*m+2*m+1-m*(m+1)/2)*ile; ans[j]-=S2*m; ans[j]-=S2; ans[j]+=(S1-S2)/2; ans[j]-=ile0*(m+1); a=na; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); /* srand(time(NULL)); int n=300000,m=300000; vector<int>P(n+m); for(int i=0;i<n+m;i++) P[i]=i+1; random_shuffle(all(P)); */ ll n,m; cin>>n>>m; vector<pair<int,int>>A,B; vector<ll> aa(n),bb(m); for(int i=0; i<n; i++) cin>>aa[i]; int c=1; for(int i=2; i<n; i++) { if((aa[i]>aa[i-1])!=(aa[i-1]>aa[i-2])) { if(c>1) A.pb({i-c,i-1}); c=1; } else c++; } if(c>1) A.pb({n-c,n-1}); for(int i=0; i<m; i++) cin>>bb[i]; c=1; for(int i=2; i<m; i++) { if((bb[i]>bb[i-1])!=(bb[i-1]>bb[i-2])) { if(c>1) B.pb({i-c,i-1}); c=1; } else c++; } if(c>1) B.pb({m-c,m-1}); calc(A,n-1,m); calc(B,m-1,n); int nn=(n*(n+1)/2); int mm=(m*(m+1)/2); for(int i=1;i<n+m;i++) ans[i]-=nn*mm; for(int i=n+m-1; i>0; i--) ans[i]-=ans[i-1]; for(int i=0;i<n+m;i++) { ans[i]%=mod; ll x=ans[i]; cout<<x<<" "; } cout<<endl; 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #include <bits/stdc++.h> #define int __int128 #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=300007; int ans[2*N]; int seg(int l,int r,int n) { r=min(r,n); if(l>r) return 0; int ans=(n+1)*(r-l+1); ans-=(r*(r+1)/2); ans+=(l*(l-1)/2); return ans; } void calc(vector<pair<int,int>>a,int n,int m) { for(int j=1; j<n+m+1;j++) { vector<pair<int,int>>na; int S1=0,S2=0,ile=0,ile0=n+1; int X=0,Y=0,Z=0; int last=0,T=0,kon=0,B=0; vector<int>pos; deque<pair<int,int>>st; for(auto [l,r]:a) { //przedzialy z dziury do dziury int rr=l-kon-1; ile+=(rr*(rr+1)/2); ile0+=(rr*(rr+1)/2); //przedialy z dziury do poprzednich while(sz(st)>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } ile+=rr*(kon-B); ile0+=rr*(kon-last); S2+=rr*T*X; S2-=rr*Y; S1+=rr*T*T*X; S1+=rr*Z; S1-=rr*2*T*Y; kon=l-1; //przedzialy z kloca poza kloca int d=r-l+1; for(int t=0;t<=m&&j*t+1<=d;t++) { //usuwamy cos ze stosu while(sz(st)+t>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } //ile poczatkow int L=min(j,d-j*t); //obliczenia ile+=L*(kon-B); //nic nie ma int z=kon-last; if(t==0) ile0+=L*z; S2+=L*t*z; S1+=L*t*t*z; //cos jest S2+=L*(T+t)*X; S2-=L*Y; S1+=L*(T+t)*(T+t)*X; S1+=L*Z; S1-=L*2*(T+t)*Y; } // cout<<S1<<" "<<S2<<" "<<ile<<" "<<ile0<<endl; //przedzialy z kloca do kloca for(int t=0;t<=m&&j*t+1<=d;t++) { int xx=seg(j*t+1,(t+1)*j,d); //cout<<t<<" "<<xx<<endl; ile+=xx; if(t==0) ile0+=xx; S1+=t*t*xx; S2+=t*xx; } //dodajemy nowe punkty int x=r-j; vector<int>pts; while(x>=l) { pts.pb(x); x-=j; } reverse(all(pts)); for(auto i:pts) { st.pb({i-last,T}); X+=st.back().st; Y+=st.back().st*st.back().nd; Z+=st.back().st*st.back().nd*st.back().nd; pos.pb(i); T++; last=i; } kon=r; if(d>j+1) na.pb({l,r}); } //dodac koncowke int rr=n-kon; ile+=(rr*(rr+1)/2); ile0+=(rr*(rr+1)/2); while(sz(st)>m) { X-=st[0].st; Y-=st[0].st*st[0].nd; Z-=st[0].st*st[0].nd*st[0].nd; B=pos[st[0].nd]; st.pop_front(); } ile+=rr*(kon-B); ile0+=rr*(kon-last); S2+=rr*T*X; S2-=rr*Y; S1+=rr*T*T*X; S1+=rr*Z; S1-=rr*2*T*Y; ile+=n+1; ans[j]+=(m*m+2*m+1-m*(m+1)/2)*ile; ans[j]-=S2*m; ans[j]-=S2; ans[j]+=(S1-S2)/2; ans[j]-=ile0*(m+1); a=na; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); /* srand(time(NULL)); int n=300000,m=300000; vector<int>P(n+m); for(int i=0;i<n+m;i++) P[i]=i+1; random_shuffle(all(P)); */ ll n,m; cin>>n>>m; vector<pair<int,int>>A,B; vector<ll> aa(n),bb(m); for(int i=0; i<n; i++) cin>>aa[i]; int c=1; for(int i=2; i<n; i++) { if((aa[i]>aa[i-1])!=(aa[i-1]>aa[i-2])) { if(c>1) A.pb({i-c,i-1}); c=1; } else c++; } if(c>1) A.pb({n-c,n-1}); for(int i=0; i<m; i++) cin>>bb[i]; c=1; for(int i=2; i<m; i++) { if((bb[i]>bb[i-1])!=(bb[i-1]>bb[i-2])) { if(c>1) B.pb({i-c,i-1}); c=1; } else c++; } if(c>1) B.pb({m-c,m-1}); calc(A,n-1,m); calc(B,m-1,n); int nn=(n*(n+1)/2); int mm=(m*(m+1)/2); for(int i=1;i<n+m;i++) ans[i]-=nn*mm; for(int i=n+m-1; i>0; i--) ans[i]-=ans[i-1]; for(int i=0;i<n+m;i++) { ans[i]%=mod; ll x=ans[i]; cout<<x<<" "; } cout<<endl; return 0; } |