QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#449863 | #8597. Запити красот пiдмасивiв | green_gold_dog# | 0 | 7ms | 3956kb | C++20 | 4.7kb | 2024-06-21 18:28:30 | 2024-06-21 22:27:47 |
Judging History
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);
for (ll i = n - 1; i >= 0; i--) {
stmi.add(i, n, arr[i]);
stma.add(i, n, arr[i]);
}
for (ll i = 0; i < q; i++) {
ll t, l, r;
cin >> t >> l >> r;
if (t == 1) {
r = n - r;
l = n - l;
l++;
swap(l, r);
ll ans = ps[r] - ps[l];
ll nx = INF64;
ll ni = l;
bool b = false;
while (ni < r) {
if (b) {
auto[mi, ind] = stmi.get(ni, r);
mi -= ps[ni];
assign_min(nx, abs(mi));
ni = ind + 1;
} else {
auto[mi, ind] = stma.get(ni, r);
mi -= ps[ni];
assign_min(nx, abs(mi));
ni = ind + 1;
}
assign_max(ans, min(nx, ps[r] - ps[ni]));
}
b = true;
nx = INF64;
ni = l;
while (ni < r) {
if (b) {
auto[mi, ind] = stmi.get(ni, r);
mi -= ps[ni];
assign_min(nx, abs(mi));
ni = ind + 1;
} else {
auto[mi, ind] = stma.get(ni, r);
mi -= ps[ni];
assign_min(nx, abs(mi));
ni = ind + 1;
}
assign_max(ans, min(nx, ps[r] - ps[ni]));
}
cout << ans << '\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:
result:
Subtask #2:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 7ms
memory: 3956kb
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:
5680838627 7971661681 7251185652 5902581650 9224227779 6576626399 7445393358 5650446665 2859080331 9951777555 8590138425 9126178404 8438322412 8776166208 2974203012 12336530829 5615794410 3485660512 4635011636 7961676420 5091313656 2023977413 8619457836 8862797014 12336530829 2035043282 9621449135 6...
result:
wrong answer 1st numbers differ - expected: '9772834244', found: '5680838627'
Subtask #3:
score: 0
Time Limit Exceeded
Test #14:
score: 0
Time Limit Exceeded
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:
69046709744 4991294820 49608837794 167224730990 95444577384 96201437344 14004737622 116583841927 132736782972 91173562774 104875680798 161962533451 114535165601 4544782563 57332852335 9401557889 124721196267 113664481114 126457277533 62277254144 74051643120 4021418416 107993042372 54265868433 479722...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
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 1049446138 567034117 626176356 1347343662 724482259 890232007 906557623 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 1347343662 134734366...
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #28:
score: 0
Time Limit Exceeded
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:
result:
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%