题解
依次判断每个位置是否能有两个取值,即(a_i = a_{i - 1})或(a_i=a_{i - 1} + 1)。判断条件:没有数与它相同,或者它不落在两个相同的数之间。
代码
int main()
{
cin >> n;
Rep(i, 1, n) {
cin >> a[i];
q[a[i]] = i;
}
int ma = 0;
LL ans = 1, cnt = -1;
Rep(i, 1, n) {
ma = max(ma, q[a[i]]);
if (ma == i) {
cnt++;
if (cnt) ans = (ans * 2) % mod;
}
}
cout << ans << endl;
return 0;
}
void pushDown(int root) {
add[lson] += add[root];
add[rson] += add[root];
add[root] = 0;
}
void Update(int l, int r, int root, int L, int R, int x) {
if (l > R || r < L) return;
if (L <= l && r <= R) {
add[root] += x;
return;
}
if (add[root]) pushDown(root);
int mid = (l + r) >> 1;
Update(l, mid, lson, L, R, x);
Update(mid + 1, r, rson, L, R, x);
}
int Find(int l, int r, int root, int pos) {
if (l == r) return add[root];
if (add[root]) pushDown(root);
int mid = (l + r) >> 1;
int ans = 0;
if (pos <= mid) ans = Find(l, mid, lson, pos);
else ans = Find(mid + 1, r, rson, pos);
return ans;
}
int main()
{
cin >> n;
Rep(i, 1, n) cin >> a[i];
Rep(i, 1, n) q[a[i]] = max(q[a[i]], i);
Rep(i, 1, n) if (!use[a[i]]) {
use[a[i]] = 1;
Update(1, n, 1, i, q[a[i]], 1);
}
vis[a[1]] = 1;
LL ans = 1;
Rep(i, 2, n) if (!vis[a[i]]) {
vis[a[i]] = 1;
if (Find(1, n, 1, i) == 1) ans = (ans * 2) % mod;
}
cout << ans << endl;
return 0;
}