Submission #577321

# Submission time Handle Problem Language Result Execution time Memory
577321 2022-06-14T14:08:11 Z Mher777 Fountain (eJOI20_fountain) C++17
60 / 100
338 ms 6748 KB
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
using namespace std;

typedef int itn;
typedef long long ll;

#define MAXL 1e18 + 9
#define MAX 1e9 + 9
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front

// 30 points

const int N = 1e5 + 10;
int go[N], pref[N];
vector<pair<int, pll>> v;

int calc(int tl, int l, int r, ll sum) {
	int mid = (l + r) / 2;
	if ((pref[mid] - pref[tl - 1]) >= sum && (pref[mid - 1] - pref[tl - 1]) < sum) {
		return mid;
	}
	else if ((pref[mid] - pref[tl - 1]) < sum) {
		return calc(tl, mid + 1, r, sum);
	}
	else {
		return calc(tl, l, mid, sum);
	}
}

void solve() {
	int n, q;
	ll n1, n2;
	cin >> n >> q;
	for (int i = 0; i < n; i++) {
		cin >> n1 >> n2;
		v.pub({ i + 1, { n1, n2 } });
	}
	v.pub({ 0, { MAXL, MAXL } });
	ll sum;
	if (n <= 1000 && q <= 2000) {
		for (int i = 0; i < v.size(); i++) {
			for (int j = i + 1; j < v.size(); j++) {
				if (v[j].ss.ff > v[i].ss.ff) {
					go[v[i].ff] = v[j].ff;
					break;
				}
			}
		}
		while (q--) {
			cin >> n1 >> n2;
			sum = 0;
			while (sum < n2) {
				if (sum + v[n1 - 1].ss.ss >= n2) {
					cout << n1 << '\n';
					break;
				}
				else {
					sum += v[n1 - 1].ss.ss;
					n1 = go[n1];
					if (n1 == 0) {
						cout << n1 << '\n';
						break;
					}
				}
			}
		}
	}
	else {
		for (int i = 0; i < n; i++) {
			pref[i + 1] = pref[i] + v[i].ss.ss;
		}
		while (q--) {
			cin >> n1 >> n2;
			if (n2 > pref[n] - pref[n1 - 1]) {
				cout << 0 << '\n';
			}
			else {
				cout << calc(n1, n1, n, n2) << '\n';
			}
		}
	}
}

int main() {
	solve();
	return 0;
}

Compilation message

fountain.cpp: In function 'void solve()':
fountain.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0; i < v.size(); i++) {
      |                   ~~^~~~~~~~~~
fountain.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |    for (int j = i + 1; j < v.size(); j++) {
      |                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 5 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 4904 KB Output is correct
2 Correct 338 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 5 ms 340 KB Output is correct
8 Correct 287 ms 4904 KB Output is correct
9 Correct 338 ms 6748 KB Output is correct
10 Incorrect 5 ms 340 KB Output isn't correct
11 Halted 0 ms 0 KB -