#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vector<int>>
#define INF INT_MAX
#define MOD 1000000009
#define MAXN 100000
using namespace std;
int p[MAXN+1][20];
int sum[MAXN+1];
vvi adj(MAXN+1);
void dfs(int v,int par,int c[]){
if (par==-1)sum[v]=0;else sum[v]=sum[par]+c[v];
p[v][0]=par;
for (int i=1;i<20;i++){
if (p[v][i-1]<1)continue;
p[v][i]=p[p[v][i-1]][i-1];
}
for (int u:adj[v])if (u!=par)dfs(u,v,c);
}
main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,q;
cin>>n>>q;
int d[n+1],c[n+1];
for (int i=1;i<=n;i++)cin>>d[i]>>c[i];
int next[n+1];
stack<pii> s;
for (int i=n;i>=1;i--){
while (!s.empty() && s.top().F<d[i])s.pop();
if (s.empty())next[i]=0; else next[i]=s.top().S;
s.push({d[i],i});
}
for (int i=1;i<=n;i++){
adj[next[i]].pb(i);
adj[i].pb(next[i]);
}
memset(p,-1,sizeof(p));
dfs(0,-1,c);
while (q--){
int v,vol;
cin>>v>>vol;
if (c[v]>=vol)cout<<v<<"\n";else{
int cur=v;
for (int i=19;i>=0;i--){
if (p[cur][i]==-1 || p[cur][i]==0)continue;
if (sum[v]-sum[p[p[cur][i]][0]]<=vol)cur=p[cur][i];
}
cout<<p[cur][0]<<"\n";
}
}
}
Compilation message
fountain.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
18260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
153 ms |
31328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
18260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |