#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = (int)4e5 + 6969; int n, k; int a[3][nax]; const int OFFSET = nax / 2; tuple<int, int, int, int> daj(vector<int> exist, vector<int> nonexist){ int from = exist[0]; int to = exist[0]; for(int cur : exist){ from = min(from, cur); to = max(to, cur); } vector<int> banned; banned.pb(0); banned.pb(n * 2 + 1); for(int cur : nonexist) banned.pb(cur); sort(banned.begin(), banned.end()); for(int i=1;i<(int)banned.size();i++){ int skad = banned[i - 1] + 1; int dokad = banned[i] - 1; if(skad > dokad) continue; if(from >= skad && to <= dokad){ return mt(skad, from, to, dokad); } } return mt(-1, -1, -1, -1); } vector<tuple<int, int, int, int> > rects; void app(tuple<int, int, int, int> cur){ auto [l1, l2, r1, r2] = cur; if(l1 != -1){ rects.pb(cur); } } void go2(int c){ int p = c - 1; if(p == 0) p = n; auto s1 = daj({a[1][c], a[2][c]}, {a[1][p], a[2][p]}); auto s2 = daj({a[1][c]}, {a[2][c], a[1][p]}); auto s3 = daj({a[2][c]}, {a[1][c], a[2][p]}); app(s1); app(s2); app(s3); } vector<tuple<int, int, int> > events[nax]; // [from, to, znak] const int BLOCK = 750; int cnt[OFFSET / BLOCK][nax]; int added[nax / BLOCK]; ll ans[nax]; int getblock(int &x){ return ((x - 1) / BLOCK); } int value[nax]; void update_pos(int &pos, int &znak){ int bl = getblock(pos); int akt = value[pos]; cnt[bl][OFFSET + akt]--; value[pos] += znak; akt = value[pos]; cnt[bl][OFFSET + akt]++; } void perform(tuple<int, int, int> upd){ auto [from, to, znak] = upd; int dokad = (getblock(from) + 1) * BLOCK; for(int i=from;i<=min(to, dokad);i++){ update_pos(i, znak); } from = min(to + 1, dokad + 1); int skad = (getblock(to)) * BLOCK + 1; for(int i=to;i>=max(skad, from);i--){ update_pos(i, znak); } to = max(skad, from) - 1; while(from <= to){ int bl = getblock(from); added[bl] += znak; from += BLOCK; } } void update(int cord){ // >= cord int bl = getblock(cord); int wsk = cord; while(wsk <= n * 2){ int cur = getblock(wsk); if(cur > bl) break; int val = value[wsk] + added[cur]; assert(val >= 0); if(val <= k) ans[val]++; wsk++; } // od wsk sie zaczyna blok while(wsk <= n * 2){ bl = getblock(wsk); for(int i=0;i<=k;i++){ ans[i] += cnt[bl][OFFSET + i - added[bl]]; } wsk += BLOCK; } } void solve(){ cin >> n >> k; for(int i=1;i<=2;i++){ for(int j=1;j<=n;j++){ cin >> a[i][j]; } } for(int i=1;i<=n;i++) go2(i); for(auto [l1, l2, r1, r2] : rects){ events[l1].pb(mt(r1, r2, 1)); events[l2 + 1].pb(mt(r1, r2, -1)); } for(int i=1;i<=n*2;i++){ cnt[getblock(i)][OFFSET] += 1; } for(int i=1;i<=n*2;i++){ for(auto ev : events[i]) perform(ev); update(i); } ans[1] += ans[0]; for(int i=1;i<=k;i++) cout << ans[i] << " "; cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = (int)4e5 + 6969; int n, k; int a[3][nax]; const int OFFSET = nax / 2; tuple<int, int, int, int> daj(vector<int> exist, vector<int> nonexist){ int from = exist[0]; int to = exist[0]; for(int cur : exist){ from = min(from, cur); to = max(to, cur); } vector<int> banned; banned.pb(0); banned.pb(n * 2 + 1); for(int cur : nonexist) banned.pb(cur); sort(banned.begin(), banned.end()); for(int i=1;i<(int)banned.size();i++){ int skad = banned[i - 1] + 1; int dokad = banned[i] - 1; if(skad > dokad) continue; if(from >= skad && to <= dokad){ return mt(skad, from, to, dokad); } } return mt(-1, -1, -1, -1); } vector<tuple<int, int, int, int> > rects; void app(tuple<int, int, int, int> cur){ auto [l1, l2, r1, r2] = cur; if(l1 != -1){ rects.pb(cur); } } void go2(int c){ int p = c - 1; if(p == 0) p = n; auto s1 = daj({a[1][c], a[2][c]}, {a[1][p], a[2][p]}); auto s2 = daj({a[1][c]}, {a[2][c], a[1][p]}); auto s3 = daj({a[2][c]}, {a[1][c], a[2][p]}); app(s1); app(s2); app(s3); } vector<tuple<int, int, int> > events[nax]; // [from, to, znak] const int BLOCK = 750; int cnt[OFFSET / BLOCK][nax]; int added[nax / BLOCK]; ll ans[nax]; int getblock(int &x){ return ((x - 1) / BLOCK); } int value[nax]; void update_pos(int &pos, int &znak){ int bl = getblock(pos); int akt = value[pos]; cnt[bl][OFFSET + akt]--; value[pos] += znak; akt = value[pos]; cnt[bl][OFFSET + akt]++; } void perform(tuple<int, int, int> upd){ auto [from, to, znak] = upd; int dokad = (getblock(from) + 1) * BLOCK; for(int i=from;i<=min(to, dokad);i++){ update_pos(i, znak); } from = min(to + 1, dokad + 1); int skad = (getblock(to)) * BLOCK + 1; for(int i=to;i>=max(skad, from);i--){ update_pos(i, znak); } to = max(skad, from) - 1; while(from <= to){ int bl = getblock(from); added[bl] += znak; from += BLOCK; } } void update(int cord){ // >= cord int bl = getblock(cord); int wsk = cord; while(wsk <= n * 2){ int cur = getblock(wsk); if(cur > bl) break; int val = value[wsk] + added[cur]; assert(val >= 0); if(val <= k) ans[val]++; wsk++; } // od wsk sie zaczyna blok while(wsk <= n * 2){ bl = getblock(wsk); for(int i=0;i<=k;i++){ ans[i] += cnt[bl][OFFSET + i - added[bl]]; } wsk += BLOCK; } } void solve(){ cin >> n >> k; for(int i=1;i<=2;i++){ for(int j=1;j<=n;j++){ cin >> a[i][j]; } } for(int i=1;i<=n;i++) go2(i); for(auto [l1, l2, r1, r2] : rects){ events[l1].pb(mt(r1, r2, 1)); events[l2 + 1].pb(mt(r1, r2, -1)); } for(int i=1;i<=n*2;i++){ cnt[getblock(i)][OFFSET] += 1; } for(int i=1;i<=n*2;i++){ for(auto ev : events[i]) perform(ev); update(i); } ans[1] += ans[0]; for(int i=1;i<=k;i++) cout << ans[i] << " "; cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } |