#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> v(n+2);
vector<vector<ll> > pref(2, vector<ll>(n+2));
vector<vector<ll> > suf(2, vector<ll>(n+2));
for(int i=1; i<=n; i++) {
cin >> v[i];
pref[0][i] = pref[0][i-1] + v[i];
pref[1][i] = pref[1][i-1] + (ll)i * v[i];
}
for(int i=n; i>=1; i--) {
suf[0][i] = suf[0][i+1] + v[i];
suf[1][i] = suf[1][i+1] + (ll)(n - i + 1) * v[i];
}
auto queryP = [&](int t, int l, int r) {
if(t == 0) return pref[t][r] - pref[t][l-1];
return (pref[1][r] - pref[1][l-1]) - (l - 1) * (pref[0][r] - pref[0][l-1]);
};
auto queryS = [&](int t, int l, int r) {
if(t == 0) return suf[t][l] - suf[t][r+1];
return (suf[1][l] - suf[1][r+1]) - (n - r) * (suf[0][l] - suf[0][r+1]);
};
//glhf with this bullshit
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 1) {
vector<int> a(k);
for(int &x : a) cin >> x;
} else {
int l, r, m;
cin >> l >> r >> m;
if(r - l + 1 == m) {
cout << queryP(0, l, r) << '\n';
continue;
}
int mp = (l + r) / 2;
int mx = min(mp, r - m + 1) - max(l, mp - m + 1) + 1;
int lp=l, rp=mp-1, L=l, R=r;
while(lp <= rp) {
int mid = (lp + rp) / 2;
int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1;
if(val < mx) L = mid, lp = mid + 1;
else rp = mid - 1;
}
lp=mp+1, rp=r;
while(lp <= rp) {
int mid = (lp + rp) / 2;
int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1;
//cout << mid << ": " << val << '\n';
if(val < mx) R = mid, rp = mid - 1;
else lp = mid + 1;
}
ll ans = queryP(1, l, L) + queryS(1, R, r);
if(R - L > 1) ans += queryP(0, L+1, R-1) * mx;
cout << ans << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
4 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2100 KB |
Output is correct |
2 |
Correct |
17 ms |
2920 KB |
Output is correct |
3 |
Correct |
22 ms |
3844 KB |
Output is correct |
4 |
Correct |
39 ms |
6380 KB |
Output is correct |
5 |
Correct |
56 ms |
9028 KB |
Output is correct |
6 |
Correct |
54 ms |
8820 KB |
Output is correct |
7 |
Correct |
60 ms |
9004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |