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>
#define INF 1e18
#define fi first
#define se second
#define FOR(i, k, n) for(ll i = k; i <= n; i++)
#define FOR1(i, k, n) for(ll i = k; i >= n; i--)
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define re return 0
#define mii map<int, int>
#define input "BESIEGE.inp"
#define output "BESIEGE.out"
#define rf freopen(input, "r", stdin); freopen(output, "w", stdout)
using namespace std;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const int base = 998244353;
void add(int &a, int b)
{
a += b;
if(a >= mod) a -= mod;
if(a < 0) a += mod;
}
ll minn[25][maxn];
ll f[maxn];
ll a[maxn];
ll get(int l, int r)
{
int k = __lg(r - l + 1);
return min(minn[k][l], minn[k][r - (1 << k) + 1]);
}
int main()
{
fastio;
ll n, D;
cin >> n >> D;
ll maxx = 0;
FOR(i, 1, n)
cin >> a[i], maxx = max(maxx, a[i]);
FOR(i, 1, n)
f[i] = f[i - 1] + a[i];
FOR(i, 1, n)
minn[0][i] = a[i];
FOR(j, 1, 20)
FOR(i, 1, n - (1 << j) + 1)
minn[j][i] = min(minn[j - 1][i], minn[j - 1][i + (1 << (j - 1))]);
int q;
cin >> q;
if(maxx <= 1)
{
while(q--)
{
int l, r;
cin >> l >> r;
int d = l, c = r, vt = -1;
while(d <= c)
{
int mid = (d + c) >> 1;
if(get(mid, r))
{
vt = mid;
c = mid - 1;
}
else
d = mid + 1;
}
if(vt == -1)
{
if(f[r] - f[l - 1] > 0)
{
if(D > 1)
cout << "-1\n";
else
cout << f[r] - f[l - 1] << "\n";
}
else
cout << "0\n";
}
else
{
int res = f[r] - f[l - 1] - (r - vt + 1);
if(res > 0)
{
if(D > 1)
cout << "-1\n";
else
cout << res << "\n";
}
else
cout << res << "\n";
}
}
}
re;
}
# | 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... |