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
| const int maxn = 2e5 + 10, inf = 0x3f3f3f3f; int n, m; vector<int> G[maxn];
class SegTree { public: struct node { int l, r; int tag, len, f; };
int tot = 0; vector<node> t; SegTree() = default; SegTree(int _tot) : tot(_tot) { assert(_tot > 0); t.resize(tot << 2); }
void build(int p, int l, int r) { t[p].l = l, t[p].r = r; if (l >= r) { t[p].f = 0, t[p].len = inf; return; } int mid = (l+r) >> 1; build(p<<1, l, mid); build(p<<1|1, mid+1, r); }
void push(int p) { if (!t[p].tag) return; int fv = t[p].tag; t[p].tag = 0;
t[p<<1].tag = fv; t[p<<1].f = fv; t[p<<1].len = fv - t[p<<1].r + 1;
t[p<<1|1].tag = fv; t[p<<1|1].f = fv; t[p<<1|1].len = fv - t[p<<1|1].r + 1; }
void pull(int p) { t[p].len = min(t[p<<1].len, t[p<<1|1].len); t[p].f = min(t[p<<1].f, t[p<<1|1].f); }
void change(int p, const int l, const int r, int fv) { if (l <= t[p].l && t[p].r <= r) { t[p].tag = fv; t[p].f = fv; t[p].len = fv - t[p].r + 1; return; } push(p); int mid = (t[p].l + t[p].r) >> 1; if (l <= mid) change(p<<1, l, r, fv); if (r > mid) change(p<<1|1, l, r, fv); pull(p); return; }
int find(int p, const int l, const int r, int fv) { if (t[p].l == t[p].r) return t[p].l; push(p);
int mid = (t[p].l + t[p].r) >> 1; int res = -1; if (r > mid && t[p<<1|1].f < fv) res = find(p<<1|1, l, r, fv); if (res != -1) return res; if (l <= mid && t[p<<1].f < fv) res = find(p<<1, l, r, fv); return res; } } seg(maxn);
void solve() { for (int x = 1; x <= m; x++) { for (int j = 1; j < G[x].size(); j++) { int l = G[x][j-1] + 1, r = G[x][j]; // [l, pos] to be changed int pos = seg.find(1, l, r, r); if (pos == -1) continue; seg.change(1, l, pos, r); } seg.change(1, G[x].back()+1, n, inf); int res = seg.t[1].len; printf("%d ", res); } }
int main() { freopen("input.txt", "r", stdin); scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) G[i].push_back(0); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); G[x].push_back(i); }
// init seg tree seg.build(1, 1, n);
solve(); }
|