Submission #703665

# Submission time Handle Problem Language Result Execution time Memory
703665 2023-02-28T04:58:03 Z Chal1shkan Fountain (eJOI20_fountain) C++14
100 / 100
327 ms 46892 KB
# include <bits/stdc++.h>
 
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 
# define pll pair <ll, ll>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
const ll maxn = 1e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
 
using namespace std;

ll n, q, up[maxn][22];
pll p[maxn];
ll sum[maxn][22];

void ma1n (/* SABR */)
{
	cin >> n >> q;
	for (ll i = 1; i <= n; ++i)
	{
		cin >> p[i].ff >> p[i].ss;
		sum[i][0] = p[i].ss;
	}
	p[n + 1] = {inf, inf};
	set <pll> st;
	for (ll i = 1; i <= n; ++i)
	{
		while (!st.empty() && (st.begin() -> ff) < p[i].ff)
		{
			up[(st.begin() -> ss)][0] = i;
			st.erase(st.begin());
		}
		st.insert({p[i].ff, i});
	}
	for (ll i = 1; i <= n; ++i)
	{
		if (up[i][0] == 0) up[i][0] = n + 1;
	}
	for (ll j = 0; j <= 20; ++j)
	{
		sum[n + 1][j] = inf;
	}
	for (ll j = 1; j <= 20; ++j)
	{
		for (ll i = 1; i <= n; ++i)
		{
			up[i][j] = up[up[i][j - 1]][j - 1];
			sum[i][j] = sum[i][j - 1] + sum[up[i][j - 1]][j - 1];
		}
	}
	while (q--)
	{
		ll id, v;
		cin >> id >> v;
		for (ll j = 20; j >= 0; --j)
		{
			if (sum[id][j] < v)
			{
				v -= sum[id][j];
				if (v)
					id = up[id][j];
			}
		}
		if (id == n + 1) id = 0; 
		cout << id << nl;
	}
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("file.in", "r", stdin);
//  freopen("file.out", "w", stdout);
    int ttt = 1;	
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 35288 KB Output is correct
2 Correct 232 ms 35640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 212 ms 35288 KB Output is correct
9 Correct 232 ms 35640 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 93 ms 22780 KB Output is correct
12 Correct 327 ms 41540 KB Output is correct
13 Correct 233 ms 41552 KB Output is correct
14 Correct 177 ms 40516 KB Output is correct
15 Correct 156 ms 46892 KB Output is correct