Submission #727416

# Submission time Handle Problem Language Result Execution time Memory
727416 2023-04-20T15:49:14 Z model_code Mizuyokan 2 (JOI23_mizuyokan2) C++17
100 / 100
1895 ms 76236 KB
#include <cstdio>
#include <vector>
using std::pair;
using std::vector;

using std::max;
using std::min;
#include <cmath>
using std::abs;

typedef long long int ll;
typedef pair<ll, ll> P;

ll n, m, q, k;

const ll maxl = 81;

const ll base = (1LL << 18);
ll val[base];
ll jumpfrom[base];
ll exactrtol[base];
vector<P> seg[base * 2];
P seglr[base * 2];

ll findexactrtol (ll r) {
	// rightest 0 <= l <= r-2 for minor seq (l, r)
	// l >= 0, so [0, r) doesn't count
	// if (len(minor seq) <= maxl) cannot be satisfied, -1
	// O(log A)

	ll sum = 0;
	for (ll l = r - 2; l >= 0; l--) {
		sum += val[l + 1];
		if (val[l] < sum && sum > val[r]) return l;
		if (r - l - 1 > maxl) return -1;
	}
	return -1;
}

vector<ll> splitsegs (ll l, ll r) {
	// Split [l, r) into segments

	vector<ll> vecl, vecr;
	for ((l += base), (r += base); l < r; (l >>= 1), (r >>= 1)) {
		if (l % 2) {
			vecl.push_back(l);
			l++;
		}
		if (r % 2) {
			--r;
			vecr.push_back(r);
		}
	}

	vector<ll> res = vecl;
	for (ll j = vecr.size() - 1; j >= 0; j--) res.push_back(vecr[j]);
	return res;
}

vector<P> resetseg (ll v) {
	// recalc seg[v] from seg[v * 2 + 0], seg[v * 2 + 1]

	ll sl = seglr[v].first, sr = seglr[v].second;
	ll sm = (sl + sr) / 2;
	ll vl = v * 2 + 0, vr = v * 2 + 1;

	vector<P> res;
	for (ll j = 0; j < maxl; j++) {
		ll l = sl + j;
		if (l >= sr) break;

		ll xr = l, xcnt = 0;
		if (l < sm) {
			P pl = seg[vl][j];
			ll l2 = jumpfrom[pl.first];
			if (l2 < sr) {
				P pr = seg[vr][l2 - sm];

				xr = pr.first;
				xcnt = pl.second + 1 + pr.second;
			} else {
				xr = pl.first;
				xcnt = pl.second;
			}
		} else {
			xr = seg[vr][l - sm].first;
			xcnt = seg[vr][l - sm].second;
		}

		res.emplace_back(xr, xcnt);
	}

	seg[v] = res;
	return res;
}
void init () {
	for (ll i = n; i < base; i++) {
		val[i] = 1;
	}

	// exactrtol, jumpfrom
	for (ll r = 0; r < base; r++) {
		exactrtol[r] = findexactrtol(r);
		// if (r < n) fprintf(stderr, "e %lld: %lld\n", r, exactrtol[r]);
	}
	for (ll l = base - 1; l >= 0; l--) {
		jumpfrom[l] = base;
		for (ll r = l; r < base; r++) {
			if (exactrtol[r] >= l) {
				jumpfrom[l] = r;
				break;
			}
		}
		// if (l < n) fprintf(stderr, "j %lld: %lld\n", l, jumpfrom[l]);
	}

	// seglr, seg
	for (ll i = 0; i < base; i++) {
		seglr[base + i] = (P){i, i + 1};

		vector<P> segl;
		segl.emplace_back(i, 0LL);
		seg[base + i] = segl;
	}
	for (ll i = base - 1; i >= 1; i--) {
		seglr[i] = (P){
			seglr[i * 2 + 0].first,
			seglr[i * 2 + 1].second,
		};

		seg[i] = resetseg(i);
	}
}
void update (ll x, ll y) {
	val[x] = y;

	// Update exactrtol, jumpfrom BEFORE seg!
	for (ll r = x; r < x + maxl + 5; r++) {
		if (r >= base) break;
		exactrtol[r] = findexactrtol(r);
		// if (r < n) fprintf(stderr, "e %lld: %lld\n", r, exactrtol[r]);
	}
	for (ll l = x; l > x - maxl - 5; l--) {
		if (l < 0) break;

		jumpfrom[l] = base; // if no
		for (ll r = l; r < base; r++) {
			if (exactrtol[r] >= l) {
				jumpfrom[l] = r;
				break;
			}
		}
		// if (l < n) fprintf(stderr, "j %lld: %lld\n", l, jumpfrom[l]);
	}

	// Update seg[l, l] and then the above
	vector<P> segl;
	segl.emplace_back(x, 0LL);
	ll v = x + base;
	seg[v] = segl;
	while (v /= 2) {
		resetseg(v);
	}
}

ll solve2 (ll l, ll r) {
	// [l, r], both val[l] and val[r] are in minor seq
	// counts major seq

	if (l > r) return -(n + 5);

	vector<ll> segs = splitsegs(l, r + 1);

	ll result = 0;
	ll v = l;
	for (ll segid : segs) {
		ll sl = seglr[segid].first;
		ll sr = seglr[segid].second;

		if (sr <= v) continue;

		P p = seg[segid][v - sl];
		v = p.first;
		result += p.second;

		if (jumpfrom[v] <= r) {
			v = jumpfrom[v];
			result++;
		} else {
			break;
		}
	}

	return result;
}
ll solve (ll l, ll r) {

	ll result = 1; // Eliminate "no minor seq"
	ll sum;

	// [ ], [ ), ( ], ( )
	ll lj; // |[-)
	sum = 0;
	for (lj = l + 1; lj < r; lj++) {
		sum += val[lj - 1];
		if (sum > val[lj]) break;
	}
	ll rj; // (-]|
	sum = 0;
	for (rj = r - 2; rj >= l; rj--) {
		sum += val[rj + 1];
		if (sum > val[rj]) break;
	}

	result = max(result, solve2(l, r - 1) * 2 + 1);
	result = max(result, solve2(lj, r - 1) * 2 + 2);
	result = max(result, solve2(l, rj) * 2 + 2);
	result = max(result, solve2(lj, rj) * 2 + 3);

	return result;
}

int main (void) {
	scanf("%lld", &n);
	for (ll i = 0; i < n; i++) {
		scanf("%lld", &val[i]);
	}
	scanf("%lld", &q);
	init();
	for (ll i = 0; i < q; i++) {
		ll x, y, a, b;
		scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
		--x;

		update(x, y);
		printf("%lld\n", solve(a, b));
	}
}

Compilation message

mizuyokan2.cpp: In function 'int main()':
mizuyokan2.cpp:224:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  224 |  scanf("%lld", &n);
      |  ~~~~~^~~~~~~~~~~~
mizuyokan2.cpp:226:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  226 |   scanf("%lld", &val[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~
mizuyokan2.cpp:228:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  228 |  scanf("%lld", &q);
      |  ~~~~~^~~~~~~~~~~~
mizuyokan2.cpp:232:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  232 |   scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 102 ms 72092 KB Output is correct
2 Correct 106 ms 72060 KB Output is correct
3 Correct 109 ms 72092 KB Output is correct
4 Correct 104 ms 72036 KB Output is correct
5 Correct 105 ms 71968 KB Output is correct
6 Correct 126 ms 72088 KB Output is correct
7 Correct 101 ms 72076 KB Output is correct
8 Correct 106 ms 72032 KB Output is correct
9 Correct 123 ms 71992 KB Output is correct
10 Correct 95 ms 72092 KB Output is correct
11 Correct 103 ms 72000 KB Output is correct
12 Correct 164 ms 72192 KB Output is correct
13 Correct 119 ms 72088 KB Output is correct
14 Correct 104 ms 72072 KB Output is correct
15 Correct 107 ms 72092 KB Output is correct
16 Correct 97 ms 72028 KB Output is correct
17 Correct 114 ms 72004 KB Output is correct
18 Correct 120 ms 71996 KB Output is correct
19 Correct 105 ms 72056 KB Output is correct
20 Correct 105 ms 72032 KB Output is correct
21 Correct 115 ms 72092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 72092 KB Output is correct
2 Correct 106 ms 72060 KB Output is correct
3 Correct 109 ms 72092 KB Output is correct
4 Correct 104 ms 72036 KB Output is correct
5 Correct 105 ms 71968 KB Output is correct
6 Correct 126 ms 72088 KB Output is correct
7 Correct 101 ms 72076 KB Output is correct
8 Correct 106 ms 72032 KB Output is correct
9 Correct 123 ms 71992 KB Output is correct
10 Correct 95 ms 72092 KB Output is correct
11 Correct 103 ms 72000 KB Output is correct
12 Correct 164 ms 72192 KB Output is correct
13 Correct 119 ms 72088 KB Output is correct
14 Correct 104 ms 72072 KB Output is correct
15 Correct 107 ms 72092 KB Output is correct
16 Correct 97 ms 72028 KB Output is correct
17 Correct 114 ms 72004 KB Output is correct
18 Correct 120 ms 71996 KB Output is correct
19 Correct 105 ms 72056 KB Output is correct
20 Correct 105 ms 72032 KB Output is correct
21 Correct 115 ms 72092 KB Output is correct
22 Correct 109 ms 72088 KB Output is correct
23 Correct 111 ms 72048 KB Output is correct
24 Correct 114 ms 72052 KB Output is correct
25 Correct 106 ms 72084 KB Output is correct
26 Correct 102 ms 72088 KB Output is correct
27 Correct 119 ms 72056 KB Output is correct
28 Correct 108 ms 72104 KB Output is correct
29 Correct 129 ms 71984 KB Output is correct
30 Correct 107 ms 72100 KB Output is correct
31 Correct 106 ms 72048 KB Output is correct
32 Correct 102 ms 72028 KB Output is correct
33 Correct 125 ms 72100 KB Output is correct
34 Correct 107 ms 72012 KB Output is correct
35 Correct 208 ms 72024 KB Output is correct
36 Correct 98 ms 72032 KB Output is correct
37 Correct 99 ms 72020 KB Output is correct
38 Correct 94 ms 72008 KB Output is correct
39 Correct 130 ms 72088 KB Output is correct
40 Correct 110 ms 72112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 72092 KB Output is correct
2 Correct 106 ms 72060 KB Output is correct
3 Correct 109 ms 72092 KB Output is correct
4 Correct 104 ms 72036 KB Output is correct
5 Correct 105 ms 71968 KB Output is correct
6 Correct 126 ms 72088 KB Output is correct
7 Correct 101 ms 72076 KB Output is correct
8 Correct 106 ms 72032 KB Output is correct
9 Correct 123 ms 71992 KB Output is correct
10 Correct 95 ms 72092 KB Output is correct
11 Correct 103 ms 72000 KB Output is correct
12 Correct 164 ms 72192 KB Output is correct
13 Correct 119 ms 72088 KB Output is correct
14 Correct 104 ms 72072 KB Output is correct
15 Correct 107 ms 72092 KB Output is correct
16 Correct 97 ms 72028 KB Output is correct
17 Correct 114 ms 72004 KB Output is correct
18 Correct 120 ms 71996 KB Output is correct
19 Correct 105 ms 72056 KB Output is correct
20 Correct 105 ms 72032 KB Output is correct
21 Correct 115 ms 72092 KB Output is correct
22 Correct 109 ms 72088 KB Output is correct
23 Correct 111 ms 72048 KB Output is correct
24 Correct 114 ms 72052 KB Output is correct
25 Correct 106 ms 72084 KB Output is correct
26 Correct 102 ms 72088 KB Output is correct
27 Correct 119 ms 72056 KB Output is correct
28 Correct 108 ms 72104 KB Output is correct
29 Correct 129 ms 71984 KB Output is correct
30 Correct 107 ms 72100 KB Output is correct
31 Correct 106 ms 72048 KB Output is correct
32 Correct 102 ms 72028 KB Output is correct
33 Correct 125 ms 72100 KB Output is correct
34 Correct 107 ms 72012 KB Output is correct
35 Correct 208 ms 72024 KB Output is correct
36 Correct 98 ms 72032 KB Output is correct
37 Correct 99 ms 72020 KB Output is correct
38 Correct 94 ms 72008 KB Output is correct
39 Correct 130 ms 72088 KB Output is correct
40 Correct 110 ms 72112 KB Output is correct
41 Correct 192 ms 73664 KB Output is correct
42 Correct 130 ms 74216 KB Output is correct
43 Correct 123 ms 73956 KB Output is correct
44 Correct 138 ms 73656 KB Output is correct
45 Correct 153 ms 74476 KB Output is correct
46 Correct 189 ms 73500 KB Output is correct
47 Correct 132 ms 73508 KB Output is correct
48 Correct 150 ms 73760 KB Output is correct
49 Correct 137 ms 73584 KB Output is correct
50 Correct 142 ms 73528 KB Output is correct
51 Correct 158 ms 73356 KB Output is correct
52 Correct 184 ms 73804 KB Output is correct
53 Correct 132 ms 74116 KB Output is correct
54 Correct 145 ms 73372 KB Output is correct
55 Correct 131 ms 73328 KB Output is correct
56 Correct 139 ms 73396 KB Output is correct
57 Correct 168 ms 73428 KB Output is correct
58 Correct 143 ms 73392 KB Output is correct
59 Correct 161 ms 74000 KB Output is correct
60 Correct 167 ms 74080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 72080 KB Output is correct
2 Correct 1239 ms 75228 KB Output is correct
3 Correct 1358 ms 75908 KB Output is correct
4 Correct 1527 ms 75648 KB Output is correct
5 Correct 1271 ms 75892 KB Output is correct
6 Correct 1666 ms 75156 KB Output is correct
7 Correct 1593 ms 75296 KB Output is correct
8 Correct 1580 ms 75356 KB Output is correct
9 Correct 1439 ms 75036 KB Output is correct
10 Correct 1414 ms 75444 KB Output is correct
11 Correct 1565 ms 75740 KB Output is correct
12 Correct 1385 ms 74952 KB Output is correct
13 Correct 1312 ms 74976 KB Output is correct
14 Correct 1644 ms 74984 KB Output is correct
15 Correct 1521 ms 74984 KB Output is correct
16 Correct 1516 ms 74960 KB Output is correct
17 Correct 1201 ms 76136 KB Output is correct
18 Correct 1411 ms 76236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 72012 KB Output is correct
2 Correct 112 ms 72096 KB Output is correct
3 Correct 1247 ms 74652 KB Output is correct
4 Correct 1498 ms 74688 KB Output is correct
5 Correct 1138 ms 74028 KB Output is correct
6 Correct 1822 ms 74560 KB Output is correct
7 Correct 1565 ms 74600 KB Output is correct
8 Correct 1439 ms 73820 KB Output is correct
9 Correct 1483 ms 74552 KB Output is correct
10 Correct 1496 ms 75116 KB Output is correct
11 Correct 1479 ms 74308 KB Output is correct
12 Correct 1678 ms 74368 KB Output is correct
13 Correct 1403 ms 74396 KB Output is correct
14 Correct 1730 ms 74304 KB Output is correct
15 Correct 1603 ms 74380 KB Output is correct
16 Correct 1176 ms 74776 KB Output is correct
17 Correct 1609 ms 75124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 72092 KB Output is correct
2 Correct 106 ms 72060 KB Output is correct
3 Correct 109 ms 72092 KB Output is correct
4 Correct 104 ms 72036 KB Output is correct
5 Correct 105 ms 71968 KB Output is correct
6 Correct 126 ms 72088 KB Output is correct
7 Correct 101 ms 72076 KB Output is correct
8 Correct 106 ms 72032 KB Output is correct
9 Correct 123 ms 71992 KB Output is correct
10 Correct 95 ms 72092 KB Output is correct
11 Correct 103 ms 72000 KB Output is correct
12 Correct 164 ms 72192 KB Output is correct
13 Correct 119 ms 72088 KB Output is correct
14 Correct 104 ms 72072 KB Output is correct
15 Correct 107 ms 72092 KB Output is correct
16 Correct 97 ms 72028 KB Output is correct
17 Correct 114 ms 72004 KB Output is correct
18 Correct 120 ms 71996 KB Output is correct
19 Correct 105 ms 72056 KB Output is correct
20 Correct 105 ms 72032 KB Output is correct
21 Correct 115 ms 72092 KB Output is correct
22 Correct 109 ms 72088 KB Output is correct
23 Correct 111 ms 72048 KB Output is correct
24 Correct 114 ms 72052 KB Output is correct
25 Correct 106 ms 72084 KB Output is correct
26 Correct 102 ms 72088 KB Output is correct
27 Correct 119 ms 72056 KB Output is correct
28 Correct 108 ms 72104 KB Output is correct
29 Correct 129 ms 71984 KB Output is correct
30 Correct 107 ms 72100 KB Output is correct
31 Correct 106 ms 72048 KB Output is correct
32 Correct 102 ms 72028 KB Output is correct
33 Correct 125 ms 72100 KB Output is correct
34 Correct 107 ms 72012 KB Output is correct
35 Correct 208 ms 72024 KB Output is correct
36 Correct 98 ms 72032 KB Output is correct
37 Correct 99 ms 72020 KB Output is correct
38 Correct 94 ms 72008 KB Output is correct
39 Correct 130 ms 72088 KB Output is correct
40 Correct 110 ms 72112 KB Output is correct
41 Correct 192 ms 73664 KB Output is correct
42 Correct 130 ms 74216 KB Output is correct
43 Correct 123 ms 73956 KB Output is correct
44 Correct 138 ms 73656 KB Output is correct
45 Correct 153 ms 74476 KB Output is correct
46 Correct 189 ms 73500 KB Output is correct
47 Correct 132 ms 73508 KB Output is correct
48 Correct 150 ms 73760 KB Output is correct
49 Correct 137 ms 73584 KB Output is correct
50 Correct 142 ms 73528 KB Output is correct
51 Correct 158 ms 73356 KB Output is correct
52 Correct 184 ms 73804 KB Output is correct
53 Correct 132 ms 74116 KB Output is correct
54 Correct 145 ms 73372 KB Output is correct
55 Correct 131 ms 73328 KB Output is correct
56 Correct 139 ms 73396 KB Output is correct
57 Correct 168 ms 73428 KB Output is correct
58 Correct 143 ms 73392 KB Output is correct
59 Correct 161 ms 74000 KB Output is correct
60 Correct 167 ms 74080 KB Output is correct
61 Correct 152 ms 72080 KB Output is correct
62 Correct 1239 ms 75228 KB Output is correct
63 Correct 1358 ms 75908 KB Output is correct
64 Correct 1527 ms 75648 KB Output is correct
65 Correct 1271 ms 75892 KB Output is correct
66 Correct 1666 ms 75156 KB Output is correct
67 Correct 1593 ms 75296 KB Output is correct
68 Correct 1580 ms 75356 KB Output is correct
69 Correct 1439 ms 75036 KB Output is correct
70 Correct 1414 ms 75444 KB Output is correct
71 Correct 1565 ms 75740 KB Output is correct
72 Correct 1385 ms 74952 KB Output is correct
73 Correct 1312 ms 74976 KB Output is correct
74 Correct 1644 ms 74984 KB Output is correct
75 Correct 1521 ms 74984 KB Output is correct
76 Correct 1516 ms 74960 KB Output is correct
77 Correct 1201 ms 76136 KB Output is correct
78 Correct 1411 ms 76236 KB Output is correct
79 Correct 110 ms 72012 KB Output is correct
80 Correct 112 ms 72096 KB Output is correct
81 Correct 1247 ms 74652 KB Output is correct
82 Correct 1498 ms 74688 KB Output is correct
83 Correct 1138 ms 74028 KB Output is correct
84 Correct 1822 ms 74560 KB Output is correct
85 Correct 1565 ms 74600 KB Output is correct
86 Correct 1439 ms 73820 KB Output is correct
87 Correct 1483 ms 74552 KB Output is correct
88 Correct 1496 ms 75116 KB Output is correct
89 Correct 1479 ms 74308 KB Output is correct
90 Correct 1678 ms 74368 KB Output is correct
91 Correct 1403 ms 74396 KB Output is correct
92 Correct 1730 ms 74304 KB Output is correct
93 Correct 1603 ms 74380 KB Output is correct
94 Correct 1176 ms 74776 KB Output is correct
95 Correct 1609 ms 75124 KB Output is correct
96 Correct 1025 ms 75016 KB Output is correct
97 Correct 1333 ms 74600 KB Output is correct
98 Correct 1157 ms 74504 KB Output is correct
99 Correct 1895 ms 75208 KB Output is correct
100 Correct 1738 ms 75040 KB Output is correct
101 Correct 1622 ms 75076 KB Output is correct
102 Correct 1675 ms 74120 KB Output is correct
103 Correct 1505 ms 75372 KB Output is correct
104 Correct 1507 ms 74976 KB Output is correct
105 Correct 1470 ms 73852 KB Output is correct
106 Correct 1569 ms 75000 KB Output is correct
107 Correct 1364 ms 74692 KB Output is correct
108 Correct 1557 ms 74984 KB Output is correct
109 Correct 1265 ms 75608 KB Output is correct
110 Correct 1350 ms 76112 KB Output is correct