题解 P6619 【[省选联考 2020 A/B 卷] 冰火战士】
首先,可以才想答案温度一定是某个战士的温度,而且我们只用用到温度的相对大小关系,所以可以把把温度离散化。
用
同时定义
再发现
单峰函数怎么办?三分?
不行,因为这个函数有很多相等段,会导致三分出错。(不过据说可以给答案加一个小数点?反正我不会 /kk)
那么把两者作差,用
所以,我们只用二分出最后一个使得
关于答案温度,如果
于是可以用二分套树状数组解决,时间复杂度是
但这个复杂度显然是不行的,考虑进一步优化,直接在树状数组上二分。
现在改写上面两个二分的式子。
发现,第一次是二分出最大的满足
第二个同理,而且方法很多,可以自行尝试。
那么时间复杂度就是
#include <cstdio>
#include <algorithm>
#define rg register
#define il inline
using namespace std;
const int N = 2e6 + 5;
inline int read()
{
int val = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') { val = val * 10 + (c ^ 48); c = getchar(); }
return val;
}
int tot, type[N], k[N], x[N], x_[N], y[N], fv[N], sum_fire;
struct binaty_indexed_tree
{
int o[N];
il void Modify(int p, int v)
{
for(; p <= tot; p += p & -p)
o[p] += v;
}
il int Query(int p)
{
int res = 0;
for(; p; p -= p & -p)
res += o[p];
return res;
}
} ice_tr, fire_tr;
il int Value(int t)
{
if(t < 1 || t > tot) return -1;
return min(ice_tr.Query(t), sum_fire - fire_tr.Query(t - 1));
}
int main()
{
rg int Q = read();
for(rg int i = 1; i <= Q; i++)
{
type[i] = read();
k[i] = read();
if(type[i] == 1) { x_[++x_[0]] = x[i] = read(); y[i] = read(); }
}
sort(x_ + 1, x_ + x_[0] + 1);
tot = unique(x_ + 1, x_ + x_[0] + 1) - x_ - 1;
for(rg int i = 1; i <= Q; i++)
if(type[i] == 1) x[i] = lower_bound(x_ + 1, x_ + tot + 1, x[i]) - x_;
rg int cnt_ice = 0, cnt_fire = 0;
for(rg int i = 1; i <= Q; i++)
{
if(type[i] == 1)
{
if(k[i] == 0) { ice_tr.Modify(x[i], y[i]); cnt_ice++; }
else { fire_tr.Modify(x[i], y[i]); cnt_fire++; sum_fire += y[i]; fv[x[i]] += y[i]; }
}
else
{
if(k[k[i]] == 0) { ice_tr.Modify(x[k[i]], -y[k[i]]); cnt_ice--; }
else { fire_tr.Modify(x[k[i]], -y[k[i]]); cnt_fire--; sum_fire -= y[k[i]]; fv[x[k[i]]] -= y[k[i]]; }
}
if(cnt_ice == 0 || cnt_fire == 0) { puts("Peace"); continue; }
rg int pos = 0, sum = 0;
for(rg int s = 20; ~s; s--)
{
int nxt = pos | (1 << s);
if(nxt <= tot && sum + ice_tr.o[nxt] + fire_tr.o[nxt] - fv[nxt] <= sum_fire)
{
sum += ice_tr.o[nxt] + fire_tr.o[nxt];
pos |= 1 << s;
}
}
int ansL = Value(pos), ansR = Value(pos + 1);
if(max(ansL, ansR) <= 0) { puts("Peace"); continue; }
if(ansL > ansR)
{
printf("%d %d\n", x_[pos], ansL << 1);
continue;
}
pos = 0; sum = 0;
for(rg int s = 20; ~s; s--)
{
int nxt = pos | (1 << s);
if(nxt <= tot && sum + fire_tr.o[nxt] - fv[nxt] <= sum_fire - ansR)
{
sum += fire_tr.o[nxt];
pos |= 1 << s;
}
}
printf("%d %d\n", x_[pos], ansR << 1);
}
return 0;
}