This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <math.h>
#include <climits>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define mpr make_pair
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define vpi vector<pii>
#define pb push_back
#define pob pop_back
#define mii map<int,int>
#define vpl vector<pair<ll, ll>>
#define pll pair<ll,ll>
#define all(v) v.begin(),v.end()
#define sz(x) x.size()
#define clr(x) x.clear()
#define yes cout << "YES\n"
#define no cout << "NO\n"
//ifstream cin("bcount.in");
//ofstream cout("bcount.out");
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void setprecision(int x) {
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
}
/*void setIO(string name = "") {
if ((int)name.size() > 0) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}*/
ll gcd(ll a, ll b) {
if (a > b) {
swap(a, b);
}
if (a == 0) {
return b;
}
if (a == b) {
return a;
}
return gcd(b - a, a);
}
ll num(ll n) {
ll ans = 0;
while (n != 0) {
ans++;
n /= 10;
}
return ans;
}
ll lcm(ll a, ll b) {
return (a * b) / gcd(a, b);
}
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
ll factorial(ll n) {
ll ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
ans %= MOD2;
}
return ans;
}
ll fact2(ll n, ll k) {
ll ans = 1;
for (int i = 1; i <= n; i++) {
if (i == k) {
ans *= (k - 1);
}
else {
ans *= i;
}
ans %= MOD2;
}
return ans;
}
bool isPrime(int n)
{
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
ll bpow_mod(ll a, ll b, ll mod)
{
ll ans = 1;
while (b) {
if ((b & 1) == 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
bool sortbysec(const pair<int, int>& a,
const pair<int, int>& b)
{
return (a.second < b.second);
}
bool ask(vector<int> vertices) {
cout << "? " << vertices.size();
for(int x: vertices) {
cout << " " << x;
}
cout << endl << flush;
string answer;
cin >> answer;
return (answer == "YES");
}
void precalc(){
return;
}
const int N = 3e5 + 10;
ll a[N];
ll ans[N];
int d[N], c[N];
int up[N][20], pref[N][20];
void solve() {
int n, q; cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> d[i] >> c[i];
}
vi v;
stack<pii>st;
int cur = 0;
st.push({1e9, 0});
c[0] = d[0] = 1e9;
for(int i = n; i >= 1; --i){
while(st.top().ff <= d[i]){
st.pop();
}
up[i][0] = st.top().ss;
pref[i][0] = c[st.top().ss];
for(int j = 1; j <= 17; ++j){
up[i][j] = up[up[i][j - 1]][j - 1];
pref[i][j] += pref[i][j - 1] + pref[up[i][j - 1]][j - 1];
}
st.push({d[i], i});
}
while(q--){
int r, val; cin >> r >> val;
if(c[r] >= val){
cout << r << endl;continue;
}
val -= c[r];
for(int i = 16; i >= 0; --i){
if(pref[r][i] < val){
val -= pref[r][i];
r = up[r][i];
}
}
cout << up[r][0] << "\n";
}
}
void cases() {
int t;
cin >> t;
while (t--) {
solve();
}
}
int main() {
fastio();
precalc();
//cases();
solve();
return 0;
}
Compilation message (stderr)
fountain.cpp: In function 'void solve()':
fountain.cpp:178:9: warning: unused variable 'cur' [-Wunused-variable]
178 | int cur = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |