#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; const int MOD=1000000007; using mi = mint<MOD,5>; // 5 is primitive root for both common mods namespace simp { vector<mi> fac,ifac,invn; void check(int x) { if (fac.empty()) { fac={mi(1),mi(1)}; ifac={mi(1),mi(1)}; invn={mi(0),mi(1)}; } while (SZ(fac)<=x) { int n=SZ(fac),m=SZ(fac)*2; fac.resize(m); ifac.resize(m); invn.resize(m); for (int i=n;i<m;i++) { fac[i]=fac[i-1]*mi(i); invn[i]=mi(MOD-MOD/i)*invn[MOD%i]; ifac[i]=ifac[i-1]*invn[i]; } } } mi gfac(int x) { assert(x>=0); check(x); return fac[x]; } mi ginv(int x) { assert(x>0); check(x); return invn[x]; } mi gifac(int x) { assert(x>=0); check(x); return ifac[x]; } mi binom(int n,int m) { if (m < 0 || m > n) return mi(0); return gfac(n)*gifac(m)*gifac(n - m); } } const int N=600100; int a[N],b[N]; mi ans[N],cnt[N]; int n,m,tot; mi ps[N],po[N],ps1[N],ps2[N]; void gao(VI a,int m) { int n=SZ(a); rep(i,1,m+2) ps[i]=ps[i-1]+(m+1-i),po[i]=ps[i-1]; rep(i,1,tot+1) { ps1[i]=ps1[i-1]+ps[min(i,m)]; ps2[i]=ps2[i-1]+ps[min(i,m)]*i; } VI seg; vector<array<int,3>> lseg; int pl=0,pr=0; rep(j,1,n) { int d=(a[j]>a[j-1]); if (d==pr) { pl++; } else { if (pl) seg.pb(pl); pr=d; pl=1; } } if (pl) seg.pb(pl); pl=0; for (auto x:seg) { lseg.pb({pl,pl+x-1,x}); pl+=x; } rep(x,1,tot+1) { vector<PII> tseg; vector<array<int,3>> rseg; for (auto p:lseg) if (p[2]>x) rseg.pb(p); lseg=rseg; int cl=0; for (auto [l,r,len]:lseg) { if (cl!=l) { tseg.pb(mp(0,l-cl)); } tseg.pb(mp(1,len)); cl=r+1; } if (cl!=n-1) tseg.pb(mp(0,n-1-cl)); ans[x]+=(ll)m*n; auto eval1=[&](int l,int r) { return ps1[r]-ps1[l-1]; }; auto eval2=[&](int l,int r) { return ps2[r]-ps2[l-1]; }; for (auto [ty,o]:tseg) { if (ty==0) { ans[x]+=(o+2)*eval1(2,o+1)-eval2(2,o+1); } else { for (int l=0;l<o;l++) { int r=min(l+x-1,o-1); int v=max(l/x,1); if (v>m||l>r) break; ans[x]-=(ll)(o-l+o-r)*(r-l+1)/2*ps[v-1]; ans[x]+=(o+2)*eval1(l+2,r+2)-eval2(l+2,r+2); l=r; } } } VI jump; vector<mi> sj1,sj2; jump.pb(-1); sj1.pb(-1); sj2.pb(0); int L=0; rep(p2,0,SZ(tseg)) { for (int x2l=1;x2l<=tseg[p2].se;x2l++) { int x2r=tseg[p2].fi==0?tseg[p2].se:min(tseg[p2].se,x2l+x-1); int pv=(x2l-1)/x*tseg[p2].fi; int R=min(SZ(jump),m-pv+1); if (R<=0) { x2l=x2r; continue; } auto evaj1=[&](int l,int r) { if (l>r) return mi(0); return sj1[r]-((l==0)?0:sj1[l-1]); }; auto evaj2=[&](int l,int r) { if (l>r) return mi(0); return sj2[r]-((l==0)?0:sj2[l-1]); }; mi coef1=0; coef1+=(m+2-SZ(jump)-pv)*evaj1(SZ(jump)-R,SZ(jump)-(pv==0)-1)+evaj2(SZ(jump)-R,SZ(jump)-(pv==0)-1); coef1+=po[pv]*(L-1); coef1-=po[pv+R]*jump[SZ(jump)-R]; ans[x]-=coef1*(x2r-x2l+1); int pos=SZ(jump)-R; int D1=x2r-x2l+1,D2=L-1-jump[pos],cc=x2l+2; if (D1==0||D2==0) { x2l=x2r; continue; } if (D1>D2) swap(D1,D2); --D1; --D2; ans[x]+=eval1(cc,D1+cc-1)*(-cc+1)+eval2(cc,D1+cc-1); ans[x]+=eval1(D1+cc,D2+cc-1)*(D1+1); ans[x]+=eval1(D2+cc,D1+D2+cc)*(D1+D2+cc+1)-eval2(D2+cc,D1+D2+cc); x2l=x2r; } if (tseg[p2].fi) { VI tjump; for (int o=L+tseg[p2].se-1-x;o>=L;o-=x) tjump.pb(o); reverse(all(tjump)); for (auto o:tjump) jump.pb(o); } while (SZ(sj1)<SZ(jump)) { int m=SZ(sj1)-1; sj1.pb(sj1[m]+jump[m+1]); sj2.pb(sj2[m]+(ll)jump[m+1]*(m+1)); } L+=tseg[p2].se; } } } int main() { scanf("%d%d",&n,&m); tot=n+m; rep(i,0,n) scanf("%d",&a[i]); rep(i,0,m) scanf("%d",&b[i]); gao(VI(a,a+n),m); gao(VI(b,b+m),n); per(i,1,n+m) ans[i]-=ans[i-1]; for (int l=1;l<=n&&l<=m;l++) ans[1]-=(ll)(n-l+1)*(m-l+1); rep(i,0,n+m) printf("%d%c",(int)ans[i]," \n"[i==n+m-1]); }
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 222 223 224 225 226 227 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; const int MOD=1000000007; using mi = mint<MOD,5>; // 5 is primitive root for both common mods namespace simp { vector<mi> fac,ifac,invn; void check(int x) { if (fac.empty()) { fac={mi(1),mi(1)}; ifac={mi(1),mi(1)}; invn={mi(0),mi(1)}; } while (SZ(fac)<=x) { int n=SZ(fac),m=SZ(fac)*2; fac.resize(m); ifac.resize(m); invn.resize(m); for (int i=n;i<m;i++) { fac[i]=fac[i-1]*mi(i); invn[i]=mi(MOD-MOD/i)*invn[MOD%i]; ifac[i]=ifac[i-1]*invn[i]; } } } mi gfac(int x) { assert(x>=0); check(x); return fac[x]; } mi ginv(int x) { assert(x>0); check(x); return invn[x]; } mi gifac(int x) { assert(x>=0); check(x); return ifac[x]; } mi binom(int n,int m) { if (m < 0 || m > n) return mi(0); return gfac(n)*gifac(m)*gifac(n - m); } } const int N=600100; int a[N],b[N]; mi ans[N],cnt[N]; int n,m,tot; mi ps[N],po[N],ps1[N],ps2[N]; void gao(VI a,int m) { int n=SZ(a); rep(i,1,m+2) ps[i]=ps[i-1]+(m+1-i),po[i]=ps[i-1]; rep(i,1,tot+1) { ps1[i]=ps1[i-1]+ps[min(i,m)]; ps2[i]=ps2[i-1]+ps[min(i,m)]*i; } VI seg; vector<array<int,3>> lseg; int pl=0,pr=0; rep(j,1,n) { int d=(a[j]>a[j-1]); if (d==pr) { pl++; } else { if (pl) seg.pb(pl); pr=d; pl=1; } } if (pl) seg.pb(pl); pl=0; for (auto x:seg) { lseg.pb({pl,pl+x-1,x}); pl+=x; } rep(x,1,tot+1) { vector<PII> tseg; vector<array<int,3>> rseg; for (auto p:lseg) if (p[2]>x) rseg.pb(p); lseg=rseg; int cl=0; for (auto [l,r,len]:lseg) { if (cl!=l) { tseg.pb(mp(0,l-cl)); } tseg.pb(mp(1,len)); cl=r+1; } if (cl!=n-1) tseg.pb(mp(0,n-1-cl)); ans[x]+=(ll)m*n; auto eval1=[&](int l,int r) { return ps1[r]-ps1[l-1]; }; auto eval2=[&](int l,int r) { return ps2[r]-ps2[l-1]; }; for (auto [ty,o]:tseg) { if (ty==0) { ans[x]+=(o+2)*eval1(2,o+1)-eval2(2,o+1); } else { for (int l=0;l<o;l++) { int r=min(l+x-1,o-1); int v=max(l/x,1); if (v>m||l>r) break; ans[x]-=(ll)(o-l+o-r)*(r-l+1)/2*ps[v-1]; ans[x]+=(o+2)*eval1(l+2,r+2)-eval2(l+2,r+2); l=r; } } } VI jump; vector<mi> sj1,sj2; jump.pb(-1); sj1.pb(-1); sj2.pb(0); int L=0; rep(p2,0,SZ(tseg)) { for (int x2l=1;x2l<=tseg[p2].se;x2l++) { int x2r=tseg[p2].fi==0?tseg[p2].se:min(tseg[p2].se,x2l+x-1); int pv=(x2l-1)/x*tseg[p2].fi; int R=min(SZ(jump),m-pv+1); if (R<=0) { x2l=x2r; continue; } auto evaj1=[&](int l,int r) { if (l>r) return mi(0); return sj1[r]-((l==0)?0:sj1[l-1]); }; auto evaj2=[&](int l,int r) { if (l>r) return mi(0); return sj2[r]-((l==0)?0:sj2[l-1]); }; mi coef1=0; coef1+=(m+2-SZ(jump)-pv)*evaj1(SZ(jump)-R,SZ(jump)-(pv==0)-1)+evaj2(SZ(jump)-R,SZ(jump)-(pv==0)-1); coef1+=po[pv]*(L-1); coef1-=po[pv+R]*jump[SZ(jump)-R]; ans[x]-=coef1*(x2r-x2l+1); int pos=SZ(jump)-R; int D1=x2r-x2l+1,D2=L-1-jump[pos],cc=x2l+2; if (D1==0||D2==0) { x2l=x2r; continue; } if (D1>D2) swap(D1,D2); --D1; --D2; ans[x]+=eval1(cc,D1+cc-1)*(-cc+1)+eval2(cc,D1+cc-1); ans[x]+=eval1(D1+cc,D2+cc-1)*(D1+1); ans[x]+=eval1(D2+cc,D1+D2+cc)*(D1+D2+cc+1)-eval2(D2+cc,D1+D2+cc); x2l=x2r; } if (tseg[p2].fi) { VI tjump; for (int o=L+tseg[p2].se-1-x;o>=L;o-=x) tjump.pb(o); reverse(all(tjump)); for (auto o:tjump) jump.pb(o); } while (SZ(sj1)<SZ(jump)) { int m=SZ(sj1)-1; sj1.pb(sj1[m]+jump[m+1]); sj2.pb(sj2[m]+(ll)jump[m+1]*(m+1)); } L+=tseg[p2].se; } } } int main() { scanf("%d%d",&n,&m); tot=n+m; rep(i,0,n) scanf("%d",&a[i]); rep(i,0,m) scanf("%d",&b[i]); gao(VI(a,a+n),m); gao(VI(b,b+m),n); per(i,1,n+m) ans[i]-=ans[i-1]; for (int l=1;l<=n&&l<=m;l++) ans[1]-=(ll)(n-l+1)*(m-l+1); rep(i,0,n+m) printf("%d%c",(int)ans[i]," \n"[i==n+m-1]); } |