社区讨论
WA 0pts 求调/hack,部分点 AC
P7167[eJOI 2020] Fountain (Day1)参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @m2r5hogj
- 此快照首次捕获于
- 2024/10/27 13:29 去年
- 此快照最后确认于
- 2025/11/04 15:54 4 个月前
思路跟第一篇题解差不多。
CPP#include<bits/stdc++.h>
#define maxn 100010
#define int long long
using namespace std;
int n,q,c[maxn],d[maxn],fa[maxn][20],sc[maxn][20],lg[maxn],dep[maxn],r,v;
stack<int> s1,s2;
vector<int> g[maxn];
void build(){
for (int i=1;i<=n;i++){
while (!s1.empty()){
if (s1.top()<d[i]){
g[i].push_back(s2.top()); // add edge
s1.pop(), s2.pop();
}
else break;
}
s1.push(d[i]), s2.push(i);
}
}
void dfs(int p,int fp){
fa[p][0]=fp, dep[p]=dep[fp]+1;
sc[p][0]=c[fp];
for (int i=1;i<=lg[dep[p]];i++){
fa[p][i]=fa[fa[p][i-1]][i-1];
sc[p][i]=sc[fa[p][i-1]][i-1]+sc[p][i-1];
}
for (auto e:g[p]){
dfs(e,p);
}
}
int getans(int x,int wa){
int ans=0;
for (int i=lg[dep[x]];i>=0;i--){
if (sc[x][i]<=wa){
wa-=sc[x][i], x=fa[x][i];
}
if (wa==0) ans=x;
}
if (ans==0) ans=fa[x][0];
return ans==n?0:ans;
}
signed main(){
ios::sync_with_stdio(0);
cin>>n>>q;
for (int i=1;i<=n;i++){
cin>>d[i]>>c[i];
}
for (int i=1;i<=n+4;i++){
lg[i]=lg[i-1]+(1<<lg[i-1]==i);
}
d[++n]=2e18, c[n]=2e18;
build();
dfs(n,0);
while (q--){
cin>>r>>v;
if (c[r]>=v){
cout<<r<<"\n";
continue;
}
cout<<getans(r,v-c[r])<<"\n";
}
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...