QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449837#8597. Запити красот пiдмасивiвgreen_gold_dog#0 823ms349804kbC++206.8kb2024-06-21 18:07:072024-06-21 18:07:08

Judging History

你现在查看的是最新测评结果

  • [2024-06-21 18:07:08]
  • 评测
  • 测评结果:0
  • 用时:823ms
  • 内存:349804kb
  • [2024-06-21 18:07:07]
  • 提交

answer

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007, LOG = 21;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
	if (b > a) {
		a = b;
		return true;
	}
	return false;
}

template<typename T>
bool assign_min(T& a, T b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}

template<typename T>
T square(T a) {
	return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
	ll operator() (pair<ll, ll> p) const {
		return ((__int128)p.first * MOD + p.second) % INF64;
	}
};

struct segment_tree_min {
	vector<pair<ll, ll>> tree;
	vector<ll> m;
	ll size;
	segment_tree_min(ll n) {
		size = 1;
		while (size < n) {
			size *= 2;
		}
		tree.resize(size * 2, make_pair(0, 0));
		m.resize(size * 2, 0);
		for (ll i = 0; i < size; i++) {
			tree[i + size].second = i;
		}
	}
	pair<ll, ll> add(ll v, ll x) {
		tree[v].first += x;
		m[v] += x;
		return tree[v];
	}
	void push(ll v) {
		add(v * 2, m[v]);
		add(v * 2 + 1, m[v]);
		m[v] = 0;
	}
	void add(ll l, ll r, ll x) {
		add(1, 0, size, l, r, x);
	}
	pair<ll, ll> add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
		if (ql <= l && r <= qr) {
			return add(v, x);
		}
		if (qr <= l || r <= ql) {
			return tree[v];
		}
		push(v);
		ll mid = (l + r) / 2;
		return tree[v] = min(add(v * 2, l, mid, ql, qr, x), add(v * 2 + 1, mid, r, ql, qr, x));
	}
	pair<ll, ll> get(ll l, ll r) {
		return get(1, 0, size, l, r);
	}
	pair<ll, ll> get(ll v, ll l, ll r, ll ql, ll qr) {
		if (ql <= l && r <= qr) {
			return tree[v];
		}
		if (qr <= l || r <= ql) {
			return make_pair(INF64, 0);
		}
		push(v);
		ll mid = (l + r) / 2;
		return min(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
	}
};

struct segment_tree_max {
	vector<pair<ll, ll>> tree;
	vector<ll> m;
	ll size;
	segment_tree_max(ll n) {
		size = 1;
		while (size < n) {
			size *= 2;
		}
		tree.resize(size * 2, make_pair(0, 0));
		m.resize(size * 2, 0);
		for (ll i = 0; i < size; i++) {
			tree[i + size].second = i;
		}
	}
	pair<ll, ll> add(ll v, ll x) {
		tree[v].first += x;
		m[v] += x;
		return tree[v];
	}
	void push(ll v) {
		add(v * 2, m[v]);
		add(v * 2 + 1, m[v]);
		m[v] = 0;
	}
	void add(ll l, ll r, ll x) {
		add(1, 0, size, l, r, x);
	}
	pair<ll, ll> add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
		if (ql <= l && r <= qr) {
			return add(v, x);
		}
		if (qr <= l || r <= ql) {
			return tree[v];
		}
		push(v);
		ll mid = (l + r) / 2;
		return tree[v] = max(add(v * 2, l, mid, ql, qr, x), add(v * 2 + 1, mid, r, ql, qr, x));
	}
	pair<ll, ll> get(ll l, ll r) {
		return get(1, 0, size, l, r);
	}
	pair<ll, ll> get(ll v, ll l, ll r, ll ql, ll qr) {
		if (ql <= l && r <= qr) {
			return tree[v];
		}
		if (qr <= l || r <= ql) {
			return make_pair(-INF64, 0);
		}
		push(v);
		ll mid = (l + r) / 2;
		return max(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
	}
};

void solve() {
	ll n, q;
	cin >> n >> q;
	vector<ll> arr(n);
	vector<ll> ps(1, 0);
	for (ll i = 0; i < n; i++) {
		cin >> arr[i];
	}
	reverse(arr.begin(), arr.end());
	for (ll i = 0; i < n; i++) {
		ps.push_back(ps.back() + arr[i]);
	}
	segment_tree_min stmi(n);
	segment_tree_max stma(n);
	vector<pair<ll, ll>> jmi(n), jma(n);
	vector<vector<ll>> pbinup(n + 1, vector<ll>(LOG, n));
	vector<vector<ll>> pbinupc(n + 1, vector<ll>(LOG, INF64));
	vector<vector<ll>> pbinupminp(n + 1, vector<ll>(LOG, INF64));
	vector<vector<ll>> pbinupmaxp(n + 1, vector<ll>(LOG, -INF64));
	vector<vector<ll>> mbinup(n + 1, vector<ll>(LOG, n));
	vector<vector<ll>> mbinupc(n + 1, vector<ll>(LOG, INF64));
	vector<vector<ll>> mbinupminp(n + 1, vector<ll>(LOG, INF64));
	vector<vector<ll>> mbinupmaxp(n + 1, vector<ll>(LOG, -INF64));
	for (ll i = n - 1; i >= 0; i--) {
		stmi.add(i, n, arr[i]);
		stma.add(i, n, arr[i]);
		jmi[i] = stmi.get(i, n);
		jma[i] = stma.get(i, n);
		jmi[i].second++;
		jma[i].second++;
		pbinup[i][0] = jma[i].second;
		pbinupc[i][0] = jma[i].first;
		pbinupminp[i][0] = ps[i];
		pbinupmaxp[i][0] = ps[pbinup[i][0]];
		mbinup[i][0] = jmi[i].second;
		mbinupc[i][0] = -jmi[i].first;
		mbinupmaxp[i][0] = ps[i];
		mbinupminp[i][0] = ps[mbinup[i][0]];
		{
			ll j = 1;
			pbinup[i][j] = mbinup[pbinup[i][j - 1]][j - 1];
			pbinupc[i][j] = min(mbinupc[pbinup[i][j - 1]][j - 1], pbinupc[i][j - 1]);
			pbinupminp[i][j] = min(mbinupminp[pbinup[i][j - 1]][j - 1], pbinupminp[i][j - 1]);
			pbinupmaxp[i][j] = max(mbinupmaxp[pbinup[i][j - 1]][j - 1], pbinupmaxp[i][j - 1]);
			mbinup[i][j] = pbinup[mbinup[i][j - 1]][j - 1];
			mbinupc[i][j] = min(pbinupc[mbinup[i][j - 1]][j - 1], mbinupc[i][j - 1]);
			mbinupminp[i][j] = min(pbinupminp[mbinup[i][j - 1]][j - 1], mbinupminp[i][j - 1]);
			mbinupmaxp[i][j] = max(pbinupmaxp[mbinup[i][j - 1]][j - 1], mbinupmaxp[i][j - 1]);
		}
		for (ll j = 2; j < LOG; j++) {
			pbinup[i][j] = pbinup[pbinup[i][j - 1]][j - 1];
			pbinupc[i][j] = min(pbinupc[pbinup[i][j - 1]][j - 1], pbinupc[i][j - 1]);
			pbinupminp[i][j] = min(pbinupminp[pbinup[i][j - 1]][j - 1], pbinupminp[i][j - 1]);
			pbinupmaxp[i][j] = max(pbinupmaxp[pbinup[i][j - 1]][j - 1], pbinupmaxp[i][j - 1]);
			mbinup[i][j] = mbinup[mbinup[i][j - 1]][j - 1];
			mbinupc[i][j] = min(mbinupc[mbinup[i][j - 1]][j - 1], mbinupc[i][j - 1]);
			mbinupminp[i][j] = min(mbinupminp[mbinup[i][j - 1]][j - 1], mbinupminp[i][j - 1]);
			mbinupmaxp[i][j] = max(mbinupmaxp[mbinup[i][j - 1]][j - 1], mbinupmaxp[i][j - 1]);
		}
	}
	for (ll i = 0; i < q; i++) {
		ll t, l, r;
		cin >> t >> l >> r;
		if (t == 1) {
			r = n - r;
			l = n - l;
			swap(l, r);
			l++;
			ll nl = 0, nr = INF64;
			while (nr - nl > 1) {
				ll mid = (nl + nr) / 2;
				ll vm = l, vp = l;
				ll mminp = INF64, mmaxp = -INF64;
				ll pminp = INF64, pmaxp = -INF64;
				for (ll j = LOG - 1; j >= 0; j--) {
					if (mbinup[vm][j] < r && mbinupc[vm][j] >= mid) {
						assign_min(mminp, mbinupminp[vm][j]);
						assign_max(mmaxp, mbinupmaxp[vm][j]);
						vm = mbinup[vm][j];
					}
					if (pbinup[vp][j] < r && pbinupc[vp][j] >= mid) {
						assign_min(pminp, pbinupminp[vp][j]);
						assign_max(pmaxp, pbinupmaxp[vp][j]);
						vp = pbinup[vp][j];
					}
				}
				if (max(max(ps[r] - mminp, mmaxp - ps[r]), max(ps[r] - pminp, pmaxp - ps[r])) >= mid) {
					nl = mid;
				} else {
					nr = mid;
				}
			}
			cout << max(nl, abs(ps[r] - ps[l])) << '\n';
		} else {
			exit(1);
		}
	}
}

int main() {
	if (IS_FILE) {
		freopen("", "r", stdin);
		freopen("", "w", stdout);
	}
    	ios_base::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
	ll t = 1;
	if (IS_TEST_CASES) {
		cin >> t;
	}
	for (ll i = 0; i < t; i++) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

1000000 1000000
548734664 631285694 511074814 185066089 177199147 524740185 143108778 954318193 103939390 194933972 126964310 977448144 188825490 775722231 460775045 254691982 436971964 566341931 148211711 74910105 923734998 440021758 474275150 717904448 680353276 612974499 712145218 300908726 34722...

output:

251800114859667
230984293950750
233363608156425
165508299454194
383253400404044
448483247843193
365778691177101
259921187069097
396031503037144
463174686819395
396489580550331
379292529971191
380903405151144
248639556319516
372751013616185
250611157186031
382670381823577
249746476399358
377677788555...

result:


Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 4ms
memory: 5188kb

input:

1000 1000
-873807720 -737050877 797489760 406646247 -750849890 -581119106 43724194 -931326234 -9389547 -684003608 -926307185 -502569356 -461635706 -238087464 -83909772 -884633970 46721429 -576741985 -323890970 -855342249 -736506813 336516318 -4075924 -782227362 56799828 290931118 -471600723 81594380...

output:

9836004253
1249286079
6588997960
2856977832
10848150640
8594107969
12014204459
8981062265
13129660816
9442750739
8744138172
8862797014
8550743768
11187583881
10663839157
12279731001
11910246851
13279881616
11211564057
8957029838
8361224658
11892906158
8007439318
8067916626
12279731001
7354614934
990...

result:

wrong answer 1st numbers differ - expected: '9772834244', found: '9836004253'

Subtask #3:

score: 0
Wrong Answer

Test #14:

score: 0
Wrong Answer
time: 823ms
memory: 349628kb

input:

200000 200000
580139218 -783262026 565291185 -701435432 -591602198 -342573855 -900284959 -10919966 -682899137 -282058183 963121871 491072571 691886927 761414760 -828585515 888361166 -790814084 145385324 214735368 388759807 -80339362 -975535748 522135695 301673442 36714481 785639770 319723485 1098009...

output:

351017689799
210891732896
313303435206
204085410033
287376776054
246940174354
215049878677
160172676856
167616359479
213491805711
197554349142
256300938819
319618527935
292608506598
314889776714
323371794818
262732658648
187519769001
220996596860
220924627393
294058869383
310587865656
188865182286
1...

result:

wrong answer 1st numbers differ - expected: '351460487491', found: '351017689799'

Subtask #4:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 730ms
memory: 349804kb

input:

200000 200000
128744308 920701830 -482412021 59142239 721167306 -622861403 165749748 -449445968 -760679479 -207234657 171482190 -239316743 75518499 -717652242 502074875 -731242646 -183939818 -625333917 -53052313 185734819 -780000096 -563390882 -690210905 596581513 764466021 776717157 -38544163 -7898...

output:

128744308
0
920701830
482412021
497432048
1218599354
622861403
761487699
906557623
1218599354
1218599354
1218599354
1218599354
1218599354
1365840701
1218599354
1595008472
1778948290
2404282207
2457334520
2271599701
3051599797
3614990679
4305201584
3708620071
2944154050
2167436893
2205981056
29957891...

result:

wrong answer 2nd numbers differ - expected: '1049446138', found: '0'

Subtask #5:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 790ms
memory: 349700kb

input:

200000 200000
3 -5 -3 3 3 -5 7 -2 2 -4 -4 9 0 -4 -2 2 -5 7 -2 3 -8 1 6 -7 9 -3 -2 4 -2 -2 3 -2 1 0 -3 6 0 -6 -2 6 -5 6 -4 3 2 -4 5 -2 -8 1 1 2 1 -3 3 5 -9 1 3 -2 0 1 -2 1 2 5 -1 -4 -1 -3 7 -7 7 1 -6 7 -1 -5 1 4 -7 6 -3 -4 -1 4 1 5 -7 -3 9 1 -6 2 -3 1 6 -1 -1 1 -1 -5 -3 9 -5 4 -1 3 -2 -4 -4 8 1 0 -9 ...

output:

5
9
9
5
7
8
2
3
7
8
4
3
3
6
3
5
4
2
6
5
2
4
6
3
1
6
7
4
7
2
2
3
7
6
6
7
8
8
4
10
5
7
6
4
5
7
2
4
5
5
2
6
9
2
5
9
4
5
2
6
7
2
1
2
5
8
6
3
1
5
4
7
8
6
2
3
2
5
8
6
7
6
6
5
7
2
1
6
4
8
3
5
4
3
5
8
8
2
7
4
9
5
3
3
8
2
4
2
5
5
8
4
7
6
6
8
8
3
3
5
7
7
7
6
2
7
8
4
6
3
3
3
2
4
9
7
3
5
4
6
6
9
9
7
5
7
6
10
4
...

result:

wrong answer 1st numbers differ - expected: '9', found: '5'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%

Subtask #7:

score: 0
Skipped

Dependency #6:

0%

Subtask #8:

score: 0
Skipped

Dependency #6:

0%

Subtask #9:

score: 0
Skipped

Dependency #1:

0%