This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 1e6 + 100;
const int M = 31;
const int mod = 998244353;
const int inf = 2e9 + 100;
int a[N];
struct DO
{
vector < vector < int > > t;
vector < pair < int, int > > li;
vector < int > ans;
int pr = 2;
void build(int n)
{
while (pr < n) pr *= 2;
t.resize(2 * pr);
ans.resize(2 * pr, 0);
for(int i = pr; i < pr + n; i++)
t[i].pb(a[i - pr]);
for(int v = pr - 1; v > 0; v--)
{
ans[v] = max(ans[v * 2], ans[v * 2 + 1]);
for(auto to: t[v * 2 + 1])
{
if (t[v * 2].back() <= to) break;
ans[v] = max(ans[v], t[v * 2].back() + to);
}
int i = 0, j = 0;
while (i < sz(t[v * 2]) || j < sz(t[v * 2 + 1]))
{
if (j == sz(t[v * 2 + 1]) || (i < sz(t[v * 2]) && t[v * 2][i] <= t[v * 2 + 1][j])) t[v].pb(t[v * 2][i++]);
else t[v].pb(t[v * 2 + 1][j++]);
}
}
}
void get_ans(int l, int r, int v, int cl, int cr)
{
if (cr < l || r < cl) return;
if (l <= cl && cr <= r)
{
li.pb({v, ans[v]});
return;
}
int mid = (cl + cr) / 2;
get_ans(l, r, v * 2, cl, mid);
get_ans(l, r, v * 2 + 1, mid + 1, cr);
}
int get(int l, int r, int k)
{
li.clear();
get_ans(l, r, 1, 0, pr - 1);
int ans = 0;
for(auto to: li)
ans = max(ans, to.S);
if (ans > k) return 0;
int mx = 0;
for(int i = 0; i < sz(li) - 1; i++)
{
int v = li[i].F, u = li[i + 1].F;
mx = max(mx, t[v].back());
if (t[u].back() + mx <= k) continue;
int l = 0, r = sz(t[u]) - 1;
while (l <= r)
{
int mid = (l + r) / 2;
if (t[u][mid] >= mx) r = mid - 1;
else
{
ans = max(ans, mx + t[u][mid]);
l = mid + 1;
}
}
if (ans > k) return 0;
}
return 1;
}
};
DO t;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int n, q;
cin >> n >> q;
for(int i = 0; i < n; i++)
cin >> a[i];
t.build(n);
while (q--)
{
int l, r, k;
cin >> l >> r >> k;
l--; r--;
cout << t.get(l, r, k) << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |