题解:CF1810D Climbing the Tree
lailai0916 · · 题解
解题思路
设当前可能的树高范围为
初始时,树高可能为任意正整数,所以令
事件 1
一只属性为
a,b 的蜗牛声称花了n 天爬树。
设符合条件的树高范围为
前
爬了
计算
事件 2
一只属性为
a,b 的蜗牛询问爬树所需天数。
当树高为
最后
显然,当
细节
- 在事件
1 中,当n=1 时,最小树高H_{min}=1 米。 - 在事件
2 中,所需天数至少为1 天。 - 使用
double类型,可能会导致精度丢失。建议根据公式手写取上整函数:
参考代码
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int N=200005;
ll up(ll a,ll b){return (a+b-1)/b;}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--)
{
ll L=1,R=inf;
int q;
cin>>q;
while(q--)
{
int op;
ll a,b,n;
cin>>op;
if(op==1)
{
cin>>a>>b>>n;
ll mn=(n==1?1:(a-b)*(n-2)+a+1);
ll mx=(a-b)*(n-1)+a;
if(mx<L||mn>R)
{
cout<<0<<' ';
}
else
{
cout<<1<<' ';
L=max(L,mn);
R=min(R,mx);
}
}
else
{
cin>>a>>b;
ll mn=max(up(L-a,a-b)+1,1ll);
ll mx=max(up(R-a,a-b)+1,1ll);
cout<<(mn==mx?mn:-1)<<' ';
}
}
cout<<'\n';
}
return 0;
}