#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vii przesun(vii X){ for(auto &i:X){ i.nd++; i.st=max(i.st, i.nd); } for(int i=X.size()-1; i>0; i--){ if(X[i].st<=X[i-1].st)swap(X[i], X[i-1]); else break; } vii X2; X2.reserve(X.size()); for(auto &i:X){ if(X2.size()==0)X2.pb(i); else if(X2.back().st==i.st){ if(X2.back().nd>i.nd)X2.back().nd=i.nd; } else{ if(X2.back().nd>i.nd)X2.pb(i); } } return X2; } vii apply(vii X, vii &Y){ //X był głównym, ale możemy też przejść początkiem Y if(Y.size())X.pb({Y[0].st, 0}); return przesun(X); } int getmin(vii &V){ if(!V.size())return 1e9; else return V[0].st; } vii merge(vii &X, vii Y){ if(X.empty())return Y; if(Y.empty())return X; vii V; V.reserve(X.size()+Y.size()); int wsk1=0, wsk2=0; while(wsk1<X.size()||wsk2<Y.size()){ pii t; if(wsk1==X.size())t=Y[wsk2++]; else if(wsk2==Y.size())t=X[wsk1++]; else if(X[wsk1]<Y[wsk2])t=X[wsk1++]; else t=Y[wsk2++]; if(V.size()==0)V.pb(t); else{ if(t.st==V.back().st){ V.back().nd=min(V.back().nd, t.nd); } else if(V.back().nd>t.nd)V.pb(t); } } return V; } int main(){ //BOOST; int n, m; cin>>n>>m; vector<int> A(n), B(m); for(int &i:A)cin>>i; for(int &j:B)cin>>j; reverse(all(A)); A.pb(0); reverse(all(A)); reverse(all(B)); B.pb(0); reverse(all(B)); vi ans(n+m); vector<vector<vii > > LA(n+1, vector<vii>(m+1)), RA(n+1, vector<vii>(m+1)); vector<vector<vii > > LB(n+1, vector<vii>(m+1)), RB(n+1, vector<vii>(m+1)); /*for(int ii=0; ii<n; ii++){ for(int jj=0; jj<m; jj++){ LA[ii][jj].reserve(15); LB[ii][jj].reserve(15); RA[ii][jj].reserve(15); RB[ii][jj].reserve(15); } }*/ for(int ii=0; ii<n; ii++){ for(int jj=0; jj<m; jj++){ //deb(ii, jj); //if(ii||jj)break; //stan: prefiks A, prefiks B, skąd przyszliśmy //trzymamy wynik jako wektory optymalnych, tzn. w LA trzymamy >, w RA < //do ktorych przyszlismy z A i analogicznie //pary (wynik, aktualny ciag) LA[ii+1][jj]={{1,0}}; RA[ii+1][jj]={{1,0}}; LB[ii][jj+1]={{1,0}}; RB[ii][jj+1]={{1,0}}; for(int i=ii; i<=n; i++){ for(int j=jj; j<=m; j++){ if(i+j<=ii+jj+1)continue; //liczenie LA i RA analogiczne if(i-1>ii && A[i]<A[i-1]){ LA[i][j]=merge(LA[i][j], apply(LA[i-1][j], RA[i-1][j])); } if(i>ii && j>jj && A[i]<B[j]){ LA[i][j]=merge(LA[i][j], apply(LB[i-1][j], RB[i-1][j])); } if(i-1>ii && A[i]>A[i-1]){ RA[i][j]=merge(RA[i][j], apply(RA[i-1][j], LA[i-1][j])); } if(i>ii && j>jj && A[i]>B[j]){ RA[i][j]=merge(RA[i][j], apply(RB[i-1][j], LB[i-1][j])); } //LB i RB if(j-1>jj && B[j]<B[j-1]){ LB[i][j]=merge(LB[i][j], apply(LB[i][j-1], RB[i][j-1])); } if(i>ii && j>jj && B[j]<A[i]){ LB[i][j]=merge(LB[i][j], apply(LA[i][j-1], RA[i][j-1])); } if(j-1>jj && B[j]>B[j-1]){ RB[i][j]=merge(RB[i][j], apply(RB[i][j-1], LB[i][j-1])); } if(i>ii && j>jj && B[j]>A[i]){ RB[i][j]=merge(RB[i][j], apply(RA[i][j-1], LA[i][j-1])); } //deb(i, j); //deb(LA[i][j], RA[i][j], LB[i][j], RB[i][j]); if(i>ii){ LA[i-1][j].clear(); RA[i-1][j].clear(); LB[i-1][j].clear(); RB[i-1][j].clear(); } if(ii==i || jj==j)continue; int t=min({getmin(LA[i][j]), getmin(RA[i][j]), getmin(LB[i][j]), getmin(RB[i][j])}); ans[t]++; } if(i==n){ for(int j=jj; j<=m; j++){ LA[i][j].clear(); RA[i][j].clear(); LB[i][j].clear(); RB[i][j].clear(); } } } } } for(int i:ans){ cout<<i<<" "; } }
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 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vii przesun(vii X){ for(auto &i:X){ i.nd++; i.st=max(i.st, i.nd); } for(int i=X.size()-1; i>0; i--){ if(X[i].st<=X[i-1].st)swap(X[i], X[i-1]); else break; } vii X2; X2.reserve(X.size()); for(auto &i:X){ if(X2.size()==0)X2.pb(i); else if(X2.back().st==i.st){ if(X2.back().nd>i.nd)X2.back().nd=i.nd; } else{ if(X2.back().nd>i.nd)X2.pb(i); } } return X2; } vii apply(vii X, vii &Y){ //X był głównym, ale możemy też przejść początkiem Y if(Y.size())X.pb({Y[0].st, 0}); return przesun(X); } int getmin(vii &V){ if(!V.size())return 1e9; else return V[0].st; } vii merge(vii &X, vii Y){ if(X.empty())return Y; if(Y.empty())return X; vii V; V.reserve(X.size()+Y.size()); int wsk1=0, wsk2=0; while(wsk1<X.size()||wsk2<Y.size()){ pii t; if(wsk1==X.size())t=Y[wsk2++]; else if(wsk2==Y.size())t=X[wsk1++]; else if(X[wsk1]<Y[wsk2])t=X[wsk1++]; else t=Y[wsk2++]; if(V.size()==0)V.pb(t); else{ if(t.st==V.back().st){ V.back().nd=min(V.back().nd, t.nd); } else if(V.back().nd>t.nd)V.pb(t); } } return V; } int main(){ //BOOST; int n, m; cin>>n>>m; vector<int> A(n), B(m); for(int &i:A)cin>>i; for(int &j:B)cin>>j; reverse(all(A)); A.pb(0); reverse(all(A)); reverse(all(B)); B.pb(0); reverse(all(B)); vi ans(n+m); vector<vector<vii > > LA(n+1, vector<vii>(m+1)), RA(n+1, vector<vii>(m+1)); vector<vector<vii > > LB(n+1, vector<vii>(m+1)), RB(n+1, vector<vii>(m+1)); /*for(int ii=0; ii<n; ii++){ for(int jj=0; jj<m; jj++){ LA[ii][jj].reserve(15); LB[ii][jj].reserve(15); RA[ii][jj].reserve(15); RB[ii][jj].reserve(15); } }*/ for(int ii=0; ii<n; ii++){ for(int jj=0; jj<m; jj++){ //deb(ii, jj); //if(ii||jj)break; //stan: prefiks A, prefiks B, skąd przyszliśmy //trzymamy wynik jako wektory optymalnych, tzn. w LA trzymamy >, w RA < //do ktorych przyszlismy z A i analogicznie //pary (wynik, aktualny ciag) LA[ii+1][jj]={{1,0}}; RA[ii+1][jj]={{1,0}}; LB[ii][jj+1]={{1,0}}; RB[ii][jj+1]={{1,0}}; for(int i=ii; i<=n; i++){ for(int j=jj; j<=m; j++){ if(i+j<=ii+jj+1)continue; //liczenie LA i RA analogiczne if(i-1>ii && A[i]<A[i-1]){ LA[i][j]=merge(LA[i][j], apply(LA[i-1][j], RA[i-1][j])); } if(i>ii && j>jj && A[i]<B[j]){ LA[i][j]=merge(LA[i][j], apply(LB[i-1][j], RB[i-1][j])); } if(i-1>ii && A[i]>A[i-1]){ RA[i][j]=merge(RA[i][j], apply(RA[i-1][j], LA[i-1][j])); } if(i>ii && j>jj && A[i]>B[j]){ RA[i][j]=merge(RA[i][j], apply(RB[i-1][j], LB[i-1][j])); } //LB i RB if(j-1>jj && B[j]<B[j-1]){ LB[i][j]=merge(LB[i][j], apply(LB[i][j-1], RB[i][j-1])); } if(i>ii && j>jj && B[j]<A[i]){ LB[i][j]=merge(LB[i][j], apply(LA[i][j-1], RA[i][j-1])); } if(j-1>jj && B[j]>B[j-1]){ RB[i][j]=merge(RB[i][j], apply(RB[i][j-1], LB[i][j-1])); } if(i>ii && j>jj && B[j]>A[i]){ RB[i][j]=merge(RB[i][j], apply(RA[i][j-1], LA[i][j-1])); } //deb(i, j); //deb(LA[i][j], RA[i][j], LB[i][j], RB[i][j]); if(i>ii){ LA[i-1][j].clear(); RA[i-1][j].clear(); LB[i-1][j].clear(); RB[i-1][j].clear(); } if(ii==i || jj==j)continue; int t=min({getmin(LA[i][j]), getmin(RA[i][j]), getmin(LB[i][j]), getmin(RB[i][j])}); ans[t]++; } if(i==n){ for(int j=jj; j<=m; j++){ LA[i][j].clear(); RA[i][j].clear(); LB[i][j].clear(); RB[i][j].clear(); } } } } } for(int i:ans){ cout<<i<<" "; } } |