[AGC012E]Camel and Oases
Description
Solution
看完题会有一个初步的想法:只能瞬移 \(\log V\) 次,从每个点出发能到达的一定是一段连续的区间,把他们看做是平面上不相交的线段,那么再考虑跳的过程,就是选一些线段覆盖所有的点,且每选一个线段,其他的线段就会发生变化。
暴力把每跳过一次的线段都处理出来,想象成 \(\log V\) 层数轴,问题就变成了在每个数轴上选一段线段覆盖所以区间,若最上层(一次都没跳)选的线段包含点x,那么x是OK的。
那么设 \(f[S], g[S]\) 为选了 \(S\) 中的线段(注意S不包含最上层),能够覆盖的最长前缀和后缀。
每次转移就找到一个不包含在S中的层的一条线段(二分查找优化)转移。
最后再考虑最上面一层,枚举线段 \([l,r]\) ,S,看选集合S中线段能覆盖的前缀是否\(\geq l - 1\),看选S的补集中线段覆盖的后缀是否\(\le r + 1\)。
Code:
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>
typedef long long LL;
typedef unsigned long long uLL;
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DE(x) cerr << x << endl;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;
#define rep(i, a, b) for (register int (i) = (a); (i) <= (b); ++(i))
using namespace std;
inline void proc_status()
{
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}
inline int read()
{
register int x = 0; register int f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
return x * f;
}
template<class T> inline void write(T x)
{
static char stk[30]; static int top = 0;
if (x < 0) { x = -x, putchar('-'); }
while (stk[++top] = x % 10 xor 48, x /= 10, x);
while (putchar(stk[top--]), top);
}
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
const int maxN = (int) 2e5, LOG = 20;
bool ans[maxN + 2];
int f[1 << LOG], g[1 << LOG], x[maxN + 2], n, V, Floor;
struct Seg
{
int l, r;
Seg() { }
Seg(int l, int r) : l(l), r(r) { }
bool operator < (Seg B) const
{ return r < B.r; }
} ;
vector<Seg> seg[LOG + 1], rev_seg[LOG + 1];
void Input()
{
n = read(), V = read();
for (int i = 1; i <= n; ++i) x[i] = read();
}
void Init()
{
for (int t = 0; (V * 2) >> t; ++t)
{
Floor = t;
int last = 1, d = V >> t;
for (int i = 1; i <= n; ++i)
{
if (x[i + 1] - x[i] > d)
{
seg[t].push_back(Seg(last, i));
rev_seg[t].push_back(Seg(n - i + 1, n - last + 1));
last = i + 1;
}
}
seg[t].push_back(Seg(last, n));
rev_seg[t].push_back(Seg(1, n - last + 1));
reverse(rev_seg[t].begin(), rev_seg[t].end());
}
}
void Solve()
{
for (int S = 0; S < 1 << Floor + 1; S += 2)
{
for (int i = 1; i <= Floor; ++i)
if (!(S >> i & 1))
{
int p = upper_bound(seg[i].begin(), seg[i].end(), Seg(0, f[S])) - seg[i].begin();
if (p != (int)seg[i].size())
chkmax(f[S | (1 << i)], seg[i][p].r);
}
}
for (int S = 0; S < 1 << Floor + 1; S += 2)
{
for (int i = 1; i <= Floor; ++i)
if (!(S >> i & 1))
{
int p = upper_bound(rev_seg[i].begin(), rev_seg[i].end(), Seg(0, g[S])) - rev_seg[i].begin();
if (p != (int)rev_seg[i].size())
chkmax(g[S | (1 << i)], rev_seg[i][p].r);
}
}
for (auto s : seg[0])
{
bool ok = 0;
for (int S = 0; S < 1 << Floor + 1; S += 2)
{
int T = ((1 << Floor + 1) - 1) ^ 1 ^ S;
if (f[S] >= s.l - 1 and g[T] >= (n - s.r + 1) - 1)
{
ok = 1;
break;
}
}
if (ok)
for (int i = s.l; i <= s.r; ++i)
ans[i] = 1;
}
for (int i = 1; i <= n; ++i)
if (ans[i])
puts("Possible");
else
puts("Impossible");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("AGC012E.in", "r", stdin);
freopen("AGC012E.out", "w", stdout);
#endif
Input();
Init();
Solve();
return 0;
}