Submission #978843

#TimeUsernameProblemLanguageResultExecution timeMemory
978843VMaksimoski008Addk (eJOI21_addk)C++17
92 / 100
60 ms9028 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...