#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define LE(vec) vec[vec.size()-1]
#define TSTS int t; cin >> t; while(t--)solve()
const int INF = 1e9;
const long long LINF = 1e18;
const long double PI = asin(1)*2;
const int MOD = 1e9+7;
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
typedef long double ld;
void setIO()
{
FAST_IO;
}
void setIO (string s)
{
setIO();
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
const int N=5e5+10, L=27;
int n, q, d[N], c[N], par[N][L];
ll sm[N][L];
void build()
{
FOR(i, 1, L)
{
FOR(j, 0, n+1)
{
par[j][i]=par[par[j][i-1]][i-1];
sm[j][i]=min(1LL*INF, sm[j][i-1]+sm[par[j][i-1]][i-1]);
}
}
}
int lift(int p, int x)
{
FOR(i, 0, L) if(x&(1<<i))
{
p=par[p][i];
}
return p;
}
ll get(int p, int x)
{
ll tot=0;
FOR(i, 0, L) if(x&(1<<i))
{
tot+=sm[p][i];
p=par[p][i];
}
return tot;
}
int query(int p, ll x)
{
int l=0, r=n;
while(l<r){
int m=(l+r)/2;
if(get(p, m+1)>=x) r=m;
else l=m+1;
}
int val=lift(p, l);
return (val==n?0:val+1);
}
int main()
{
setIO();
cin >> n >> q;
vector<pii> inf;
FOR(i, 0, n)
{
cin >> d[i] >> c[i];
sm[i][0]=c[i];
inf.PB({-d[i], i});
}
sm[n][0]=INF;
par[n][0]=n;
sort(inf.begin(), inf.end());
set<int> used;
used.insert(n);
for(auto[x, i] : inf){
auto it=used.upper_bound(i);
par[i][0]=*it;
used.insert(i);
}
build();
FOR(i, 0, q)
{
int p, x;
cin >> p >> x;
cout << query(p-1, x) << '\n';
}
}
Compilation message
fountain.cpp: In function 'int main()':
fountain.cpp:100:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | for(auto[x, i] : inf){
| ^
fountain.cpp: In function 'void setIO(std::string)':
fountain.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
838 ms |
37268 KB |
Output is correct |
2 |
Correct |
847 ms |
34424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
838 ms |
37268 KB |
Output is correct |
9 |
Correct |
847 ms |
34424 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
362 ms |
23636 KB |
Output is correct |
12 |
Correct |
1165 ms |
41700 KB |
Output is correct |
13 |
Correct |
855 ms |
43072 KB |
Output is correct |
14 |
Correct |
520 ms |
42300 KB |
Output is correct |
15 |
Correct |
399 ms |
42700 KB |
Output is correct |