#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=(1<<18), K=11, INF=1e9+5, mod=1e9+7; using piv=pair<int, vi>; piv tab[N+N]; int lazy[N+N]; bool czy=0; piv comp(piv a, piv b){ if(a.st>b.st)swap(a, b); for(int i=0; i<a.nd.size(); i++)if(a.st+i>=b.st)a.nd[i]+=b.nd[a.st+i-b.st]; return a; } void prop(int v){ if(!lazy[v] || v>=N)return; int l=v+v, r=l+1; lazy[l]+=lazy[v]; lazy[r]+=lazy[v]; tab[l].st+=lazy[v]; tab[r].st+=lazy[v]; lazy[v]=0; } void add(int v, int L, int R, int l, int r, int c){ //assert(l<=r); if(l==L && r==R){ lazy[v]+=c; tab[v].st+=c; return; } int M=(L+R)>>1; prop(v); if(l<=M)add(v+v, L, M, l, min(r, M), c); if(r>M)add(v+v+1, M+1, R, max(l, M+1), r, c); tab[v]=comp(tab[v+v], tab[v+v+1]); return; } piv sum(int v, int L, int R, int l, int r){ if(l==L && r==R){ //deb(v,v-N, v-N/2, tab[v]); return tab[v]; } int M=(L+R)>>1; prop(v); if(l<=M && r>M){ return comp(sum(v+v, L, M, l, min(r, M)), sum(v+v+1, M+1, R, max(l, M+1), r)); } if(l<=M)return sum(v+v, L, M, l, min(r, M)); if(r>M)return sum(v+v+1, M+1, R, max(l, M+1), r); assert(false); } int tab2[2][N]; vector<pair<int, pii> > todo[N];//t, c, l, r int main(){ //BOOST; for(int i=N; i<N+N; i++)tab[i]={0, vi(K)}, tab[i].nd[0]=1; for(int i=N-1; i>0; i--){ tab[i]=comp(tab[i+i], tab[i+i+1]); } //czy=1; int n, k; cin>>n>>k; for(int i=0; i<2; i++){ for(int j=0; j<n; j++){ cin>>tab2[i][j]; } } tab2[0][n]=tab2[0][0]; tab2[1][n]=tab2[1][0]; for(int i=0; i<n; i++){ int a=tab2[0][i], aa=tab2[0][i+1], b=tab2[1][i], bb=tab2[1][i+1]; if(a>b){ swap(a, b); swap(aa,bb); } //deb(a, aa, b, bb); if(bb>b){ todo[b].eb(1, mp(b, bb-1)); todo[a].eb(-1, mp(b, bb-1)); } else{ todo[b].eb(1, mp(b, n+n)); //deb(b, bb, a); todo[max(bb, a)].eb(-1, mp(b, n+n)); } if(aa>a){ todo[a].eb(1, mp(a, min(aa, b)-1)); } else if(aa<b){ todo[a].eb(1, mp(a, b-1)); todo[aa].eb(-1, mp(a, b-1)); } if((aa<a || aa>b) && (bb<a || bb>b)){ int l=1; if(aa<a)l=max(l, aa+1); if(bb<a)l=max(l, bb+1); int r=n+n; if(aa>b)r=min(r, aa-1); if(bb>b)r=min(r, bb-1); todo[a].eb(1, mp(b, r)); todo[l-1].eb(-1, mp(b, r)); } } vl ans(K); //ans.nd[0]=1; for(int i=n+n; i>0; i--){ //deb(i); for(auto j:todo[i]){ //deb(j); add(1, 0, N-1, j.nd.st, j.nd.nd, j.st); } //deb(sum(1, 0, N-1, i, n+n)); //ans=comp(ans, sum(1, 0, N-1, i, n+n)); auto s=sum(1, 0, N-1, i, n+n); for(int j=s.st; j<K; j++){ ans[j]+=s.nd[j-s.st]; } //deb(ans); } ans[1]+=ans[0]; for(int i=1; i<=k; i++){ cout<<ans[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 | #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=(1<<18), K=11, INF=1e9+5, mod=1e9+7; using piv=pair<int, vi>; piv tab[N+N]; int lazy[N+N]; bool czy=0; piv comp(piv a, piv b){ if(a.st>b.st)swap(a, b); for(int i=0; i<a.nd.size(); i++)if(a.st+i>=b.st)a.nd[i]+=b.nd[a.st+i-b.st]; return a; } void prop(int v){ if(!lazy[v] || v>=N)return; int l=v+v, r=l+1; lazy[l]+=lazy[v]; lazy[r]+=lazy[v]; tab[l].st+=lazy[v]; tab[r].st+=lazy[v]; lazy[v]=0; } void add(int v, int L, int R, int l, int r, int c){ //assert(l<=r); if(l==L && r==R){ lazy[v]+=c; tab[v].st+=c; return; } int M=(L+R)>>1; prop(v); if(l<=M)add(v+v, L, M, l, min(r, M), c); if(r>M)add(v+v+1, M+1, R, max(l, M+1), r, c); tab[v]=comp(tab[v+v], tab[v+v+1]); return; } piv sum(int v, int L, int R, int l, int r){ if(l==L && r==R){ //deb(v,v-N, v-N/2, tab[v]); return tab[v]; } int M=(L+R)>>1; prop(v); if(l<=M && r>M){ return comp(sum(v+v, L, M, l, min(r, M)), sum(v+v+1, M+1, R, max(l, M+1), r)); } if(l<=M)return sum(v+v, L, M, l, min(r, M)); if(r>M)return sum(v+v+1, M+1, R, max(l, M+1), r); assert(false); } int tab2[2][N]; vector<pair<int, pii> > todo[N];//t, c, l, r int main(){ //BOOST; for(int i=N; i<N+N; i++)tab[i]={0, vi(K)}, tab[i].nd[0]=1; for(int i=N-1; i>0; i--){ tab[i]=comp(tab[i+i], tab[i+i+1]); } //czy=1; int n, k; cin>>n>>k; for(int i=0; i<2; i++){ for(int j=0; j<n; j++){ cin>>tab2[i][j]; } } tab2[0][n]=tab2[0][0]; tab2[1][n]=tab2[1][0]; for(int i=0; i<n; i++){ int a=tab2[0][i], aa=tab2[0][i+1], b=tab2[1][i], bb=tab2[1][i+1]; if(a>b){ swap(a, b); swap(aa,bb); } //deb(a, aa, b, bb); if(bb>b){ todo[b].eb(1, mp(b, bb-1)); todo[a].eb(-1, mp(b, bb-1)); } else{ todo[b].eb(1, mp(b, n+n)); //deb(b, bb, a); todo[max(bb, a)].eb(-1, mp(b, n+n)); } if(aa>a){ todo[a].eb(1, mp(a, min(aa, b)-1)); } else if(aa<b){ todo[a].eb(1, mp(a, b-1)); todo[aa].eb(-1, mp(a, b-1)); } if((aa<a || aa>b) && (bb<a || bb>b)){ int l=1; if(aa<a)l=max(l, aa+1); if(bb<a)l=max(l, bb+1); int r=n+n; if(aa>b)r=min(r, aa-1); if(bb>b)r=min(r, bb-1); todo[a].eb(1, mp(b, r)); todo[l-1].eb(-1, mp(b, r)); } } vl ans(K); //ans.nd[0]=1; for(int i=n+n; i>0; i--){ //deb(i); for(auto j:todo[i]){ //deb(j); add(1, 0, N-1, j.nd.st, j.nd.nd, j.st); } //deb(sum(1, 0, N-1, i, n+n)); //ans=comp(ans, sum(1, 0, N-1, i, n+n)); auto s=sum(1, 0, N-1, i, n+n); for(int j=s.st; j<K; j++){ ans[j]+=s.nd[j-s.st]; } //deb(ans); } ans[1]+=ans[0]; for(int i=1; i<=k; i++){ cout<<ans[i]<<" "; } } |