题解:P14231 复读机 / repeat
双 log 卡一下常是能过的(至少目前能过)。
思路:
遇到最小化最大值(或最大化最小值),就考虑二分答案。
设二分的答案为
我们尽量选更多的数。只要能选多于
把数分成两组,把
于是就已经有
此时我们再推一些性质:
我们定义选进来的数产生了
结论:如果一个数在
::::info[证明]
考虑
于是求出每个
找出
现在我们需要把一个询问区间套上去。找出询问区间的左边第一个 A 组数,和右边第一个 A 组数(我用的 st 表+二分)。在这两个数之间,我们使用可持久化线段树快速求出这个区间里有多少
现在就有
还没完,我们直接交上去只能得到
最神奇的卡常来了。因为
代码:
#include <bits/stdc++.h>
#define ls(u) tree[u].lson
#define rs(u) tree[u].rson
using namespace std;
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
#endif
char gc() {
#if DEBUG // 调试,可显示字符
return getchar();
#endif
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
return p1 == p2 ? ' ' : *p1++;
}
bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
template <class T>
void read(T &x) {
double tmp = 1;
bool sign = false;
x = 0;
char ch = gc();
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
if (ch == '.')
for (ch = gc(); isdigit(ch); ch = gc())
tmp /= 10.0, x += tmp * (ch - '0');
if (sign) x = -x;
}
void read(char *s) {
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
}
void read(char &c) { for (c = gc(); blank(c); c = gc()); }
void push(const char &c) {
#if DEBUG // 调试,可显示字符
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <class T>
void write(T x) {
if (x < 0) x = -x, push('-');
static T sta[35];
T top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) push(sta[--top] + '0');
}
template <class T>
void write(T x, char lastChar) {
write(x), push(lastChar);
}
} io;
const int N = 3e5 + 5;
const int M = 22;
const int INF = 1e9 + 7;
struct Node{
int lson,rson;
int cnt;
}tree[(2 * N) * M + (N << 2)];
// Mind 2 * N;
int tot;
int addnode(void)
{
++tot;
ls(tot) = rs(tot) = 0;
tree[tot].cnt = 0;
return tot;
}
int root[N];
void push_up(int id)
{
tree[id].cnt = tree[ls(id)].cnt + tree[rs(id)].cnt;
return;
}
void modify(int& u,int v,int l,int r,int k)
{
u = addnode();
if(l == r)
{
tree[u].cnt = tree[v].cnt + 1;
return;
}
int mid = l + r >> 1;
if(k <= mid)
{
modify(ls(u),ls(v),l,mid,k);
rs(u) = rs(v);
}
else
{
modify(rs(u),rs(v),mid + 1,r,k);
ls(u) = ls(v);
}
push_up(u);
return;
}
int getcnt(int u,int v,int l,int r,int k)
{
if(u >= v)
return 0;
if(r <= k)
return tree[v].cnt - tree[u].cnt;
int mid = l + r >> 1;
if(k <= mid)
return getcnt(ls(u),ls(v),l,mid,k);
else
return tree[ls(v)].cnt - tree[ls(u)].cnt + getcnt(rs(u),rs(v),mid + 1,r,k);
}
int n,q;
int arr[N];
int contri[N];
int stk[N],top;
int cl[N],cr[N];
int LOG[N];
int st[M][N];
int getmin(int l,int r)
{
if(l > r)
return INF;
int j = LOG[r - l + 1];
return min(st[j][l],st[j][r - (1 << j) + 1]);
}
int main(void)
{
// freopen("repeat.in", "r", stdin);
// freopen("repeat.out", "w", stdout);
LOG[0] = -1;
for(int i = 1;i <= N - 2;i++)
LOG[i] = LOG[i >> 1] + 1;
io.read(n),io.read(q);
for(int i = 1;i <= n;i++)
io.read(arr[i]);
for(int i = 1;i <= n;i++)
{
while(top && arr[stk[top]] >= arr[i])
top--;
cl[i] = stk[top];
stk[++top] = i;
}
top = 0;
for(int i = n;i >= 1;i--)
{
while(top && arr[stk[top]] > arr[i])
top--;
cr[i] = stk[top];
stk[++top] = i;
}
for(int i = 1;i <= n;i++)
contri[i] = max(arr[cl[i]],arr[cr[i]]) + arr[i];
for(int i = 1;i <= n;i++)
st[0][i] = arr[i];
for(int j = 1;j <= LOG[n];j++)
{
int i = 1;
for(;i + (1 << j - 1) <= n;i++)
st[j][i] = min(st[j - 1][i],st[j - 1][i + (1 << j - 1)]);
for(;i <= n;i++)
st[j][i] = st[j - 1][i];
}
root[0] = addnode();
for(int i = 1;i <= n;i++)
modify(root[i],root[i - 1],1,n << 1,contri[i]);
while(q--)
{
int l,r,k;
io.read(l),io.read(r),io.read(k);
int L = 1,R = n << 1;
while(L < R)
{
int M = L + R >> 1;
int res = 0;
int _L = l,_R = r + 1;
while(_L < _R)
{
int _M = _L + _R >> 1;
if(getmin(l,_M) * 2 <= M)
_R = _M;
else
_L = _M + 1;
}
int fm = _L;
_L = l - 1,_R = r;
while(_L < _R)
{
int _M = _L + _R + 1 >> 1;
if(getmin(_M,r) * 2 <= M)
_L = _M;
else
_R = _M - 1;
}
int gm = _L;
if(fm ^ (r + 1))
res = getcnt(root[fm - 1],root[gm],1,n << 1,M) + (arr[fm] + getmin(l,fm - 1) <= M) + (arr[gm] + getmin(gm + 1,r) <= M);
if(res >= k)
R = M;
else
L = M + 1;
}
io.write(L,'\n');
}
return 0;
}