답안 #577318

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577318 2022-06-14T13:55:05 Z Mher777 Fountain (eJOI20_fountain) C++17
30 / 100
1500 ms 3576 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 l, int r, ll sum) {
	int mid = (l + r) / 2;
	if ((pref[mid] - pref[l - 1]) == sum) {
		return mid;
	}
	else if ((pref[mid] - pref[l - 1]) < sum) {
		return calc(l, mid, sum);
	}
	else {
		return calc(mid + 1, r, 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, 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++) {
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1585 ms 3576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 Execution timed out 1585 ms 3576 KB Time limit exceeded
9 Halted 0 ms 0 KB -