Submission #1010658

# Submission time Handle Problem Language Result Execution time Memory
1010658 2024-06-29T09:13:06 Z dozer Fish 3 (JOI24_fish3) C++14
100 / 100
744 ms 272812 KB
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n";
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 400005
#define LL node * 2
#define RR node * 2 + 1
#define mid (l + r) / 2
#define LOGN 20
#define int long long

const int modulo = 1e9 + 7;
const long long INF = 2e18 + 7;

	
int arr[N], d;

struct Node{
	int start, last, ans;
	vector<int> pre, gaps;
	int L, R;
	Node(){};
	Node(int l, int r){
		//cout<<l<<sp<<r<<" : ";
		L = l, R = r;
		gaps.pb(0);
		pre.pb(0);
		ans = 0;
		int curr = arr[r];
		start = arr[r];
		r--;
		while(r >= l){
			int cnt = (max((int)0, arr[r] - curr) + d - 1) / d;
			ans += cnt;
			int tmp = arr[r] - cnt * d;
			int g = (curr - tmp) / d;
			gaps.pb(gaps.back() + g);
			pre.pb(pre.back() + gaps.back());
			curr = tmp;
			r--;
		}
		last = curr;
		//cout<<start<<sp<<last<<sp<<ans<<endl;
	}

	pii merge(int curr, int res){
		res += ans;
		if (curr < 0) return {curr, -1};

		if (start <= curr) {
			if (last < 0) return {last, -1};
			return {last, res};
		}

		int diff = start - curr;
		int cnt = (diff + d - 1) / d;
		res += cnt;


		if (gaps.size() == 1) {
			curr = last - cnt * d;
			if (curr < 0) return {curr, -1};
			return {curr, res};
		}

		int pos = lower_bound(gaps.begin(), gaps.end(), cnt) - gaps.begin();

		curr = last;
		if (pos == gaps.size()){
			pos--;
			curr -= (cnt - gaps[pos]) * d;
		}

		int sz = pos;
		res += cnt * sz;
		res -= pre[pos];
		if (gaps[pos] > cnt) res += gaps[pos] - cnt;

		if (curr < 0) return {curr, -1};
		return {curr, res};
	}
};




struct SegTree{
	vector<Node> tree;
	int n;
	void build(int node, int l, int r){
		tree[node] = Node(l, r);
		if (l == r) return;
		build(LL, l, mid);
		build(RR, mid + 1, r);
	}
	SegTree(int sz){
		n = sz;
		tree.resize(4 * n + 5);
		build(1, 1, n);
	}

	pii query(int node, int l, int r, int sl, int sr, int curr, int res){
		if (l >= sl && r <= sr) return tree[node].merge(curr, res);
		if (sl > mid) return query(RR, mid + 1, r, sl, sr, curr, res);
		if (sr <= mid) return query(LL, l, mid, sl, sr, curr, res);
		pii tmp = query(RR, mid + 1, r, sl, sr, curr, res);
		return query(LL, l, mid, sl, sr, tmp.st, tmp.nd);
	}
};


int32_t main()
{
	fastio();

	int n;
	cin>>n>>d;
	for (int i = 1; i <= n; i++){
		cin>>arr[i];
	}

	SegTree tree(n);
	int q;
	cin>>q;
	while(q--){
		int l, r;
		cin>>l>>r;
		pii tmp = tree.query(1, 1, n, l, r, INF, 0); 
		cout<<tmp.nd<<endl;
	}
	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " seconds\n";
}

Compilation message

Main.cpp: In member function 'std::pair<long long int, long long int> Node::merge(long long int, long long int)':
Main.cpp:74:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   if (pos == gaps.size()){
      |       ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 2524 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 2 ms 1628 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 3 ms 2544 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 4 ms 2396 KB Output is correct
12 Correct 3 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 532 ms 265892 KB Output is correct
2 Correct 452 ms 265380 KB Output is correct
3 Correct 74 ms 5024 KB Output is correct
4 Correct 393 ms 264996 KB Output is correct
5 Correct 426 ms 265056 KB Output is correct
6 Correct 410 ms 264832 KB Output is correct
7 Correct 405 ms 264832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 8792 KB Output is correct
2 Correct 738 ms 272812 KB Output is correct
3 Correct 744 ms 272552 KB Output is correct
4 Correct 704 ms 272684 KB Output is correct
5 Correct 725 ms 272812 KB Output is correct
6 Correct 692 ms 266216 KB Output is correct
7 Correct 664 ms 266304 KB Output is correct
8 Correct 717 ms 269508 KB Output is correct
9 Correct 698 ms 269476 KB Output is correct
10 Correct 430 ms 267952 KB Output is correct
11 Correct 441 ms 268092 KB Output is correct
12 Correct 621 ms 271028 KB Output is correct
13 Correct 603 ms 271020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 448 ms 190332 KB Output is correct
2 Correct 544 ms 265924 KB Output is correct
3 Correct 269 ms 57784 KB Output is correct
4 Correct 717 ms 269220 KB Output is correct
5 Correct 594 ms 232876 KB Output is correct
6 Correct 659 ms 272572 KB Output is correct
7 Correct 544 ms 189360 KB Output is correct
8 Correct 652 ms 272392 KB Output is correct
9 Correct 431 ms 265344 KB Output is correct
10 Correct 431 ms 265128 KB Output is correct
11 Correct 517 ms 269224 KB Output is correct
12 Correct 467 ms 268448 KB Output is correct
13 Correct 666 ms 272248 KB Output is correct
14 Correct 450 ms 268200 KB Output is correct
15 Correct 647 ms 272300 KB Output is correct
16 Correct 479 ms 268200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 2524 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 2 ms 1628 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 3 ms 2544 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 4 ms 2396 KB Output is correct
12 Correct 3 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 532 ms 265892 KB Output is correct
15 Correct 452 ms 265380 KB Output is correct
16 Correct 74 ms 5024 KB Output is correct
17 Correct 393 ms 264996 KB Output is correct
18 Correct 426 ms 265056 KB Output is correct
19 Correct 410 ms 264832 KB Output is correct
20 Correct 405 ms 264832 KB Output is correct
21 Correct 121 ms 8792 KB Output is correct
22 Correct 738 ms 272812 KB Output is correct
23 Correct 744 ms 272552 KB Output is correct
24 Correct 704 ms 272684 KB Output is correct
25 Correct 725 ms 272812 KB Output is correct
26 Correct 692 ms 266216 KB Output is correct
27 Correct 664 ms 266304 KB Output is correct
28 Correct 717 ms 269508 KB Output is correct
29 Correct 698 ms 269476 KB Output is correct
30 Correct 430 ms 267952 KB Output is correct
31 Correct 441 ms 268092 KB Output is correct
32 Correct 621 ms 271028 KB Output is correct
33 Correct 603 ms 271020 KB Output is correct
34 Correct 448 ms 190332 KB Output is correct
35 Correct 544 ms 265924 KB Output is correct
36 Correct 269 ms 57784 KB Output is correct
37 Correct 717 ms 269220 KB Output is correct
38 Correct 594 ms 232876 KB Output is correct
39 Correct 659 ms 272572 KB Output is correct
40 Correct 544 ms 189360 KB Output is correct
41 Correct 652 ms 272392 KB Output is correct
42 Correct 431 ms 265344 KB Output is correct
43 Correct 431 ms 265128 KB Output is correct
44 Correct 517 ms 269224 KB Output is correct
45 Correct 467 ms 268448 KB Output is correct
46 Correct 666 ms 272248 KB Output is correct
47 Correct 450 ms 268200 KB Output is correct
48 Correct 647 ms 272300 KB Output is correct
49 Correct 479 ms 268200 KB Output is correct
50 Correct 694 ms 272756 KB Output is correct
51 Correct 684 ms 266368 KB Output is correct
52 Correct 676 ms 269508 KB Output is correct
53 Correct 422 ms 267948 KB Output is correct
54 Correct 614 ms 271020 KB Output is correct
55 Correct 717 ms 272492 KB Output is correct
56 Correct 408 ms 268200 KB Output is correct
57 Correct 399 ms 265128 KB Output is correct
58 Correct 405 ms 265148 KB Output is correct
59 Correct 640 ms 270392 KB Output is correct
60 Correct 403 ms 268204 KB Output is correct
61 Correct 435 ms 267912 KB Output is correct
62 Correct 451 ms 267936 KB Output is correct
63 Correct 595 ms 270912 KB Output is correct
64 Correct 378 ms 268204 KB Output is correct