Submission #848416

#TimeUsernameProblemLanguageResultExecution timeMemory
848416KK_1729Addk (eJOI21_addk)C++14
56 / 100
37 ms7324 KiB
#include <bits/stdc++.h> using namespace std; // -------Macros--------- #define int long long #define double long long double #define pb push_back #define str string #define vi vector<int> #define mp make_pair #define mi map<int, int> #define umi unordered_map<int, int> #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define all(a) a.begin(), a.end() #define endl "\n" // ------------------------ // --------------------------Constants------------------------------ const int INF = 1e17; const int MOD = 1e9+7; const int MOD2 = 998244353; // const long long double PI = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; // ------------------------------------------------------------------- // ----------------------Pre Written Functions--------------------------- int sum(vector<int> a){ // Returns the sum of values in a vector<int> int total = 0; for (int x : a) total += x; return total; } template<typename T> void printVector(T a){ // cout << "[ "; for (auto x: a) cout << x << " "; cout << endl; } template<typename T> void printMap(T a){ cout << "{ "; for (auto x: a){ cout << x.first << ": " << x.second << " "; } cout << "}" << endl; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Function to return LCM of two numbers int lcm(int a, int b) { return (a / gcd(a, b)) * b; } // ------------------------------------------------------------------------ vi decToBinary(int n) { // array to store binary number vi binaryNum(100); // counter for binary array int i = 0; while (n > 0) { // storing remainder in binary array binaryNum[i] = n % 2; n = n / 2; i++; } return binaryNum; } int ceildiv(int x, int y){ return (x+y-1)/y; } int min_element(vi a){ int m = 10000000000000; for (auto x: a){ m = min(m, x); } return m; } int max_element(vi a){ int m = -1; for (auto x: a){ m = max(m, x); } return m; } int N = 0; vector<int> a; vector<int> prefix; vector<int> weight; vector<int> rweight; int sprefix(int l, int r){ int ans = prefix[r]; ans -= prefix[l-1]; return ans; } int sweight(int l, int r){ int ans = weight[r]; if (l) ans -= weight[l-1]; int diff = l; ans -= sprefix(l, r)*diff; return ans; } int srweight(int l, int r){ int ans = rweight[r]; if (l) ans -= rweight[l-1]; int length = r-l+1; int diff = (N-l)-length; ans -= sprefix(l, r)*diff; return ans; } void solve(){ int n, k; cin >> n >> k; N = n; a.resize(N); prefix.resize(N); weight.resize(N); rweight.resize(N); FOR(i,0,n) cin >> a[i]; prefix[0] = a[0]; FOR(i,1,n) prefix[i] = prefix[i-1]+a[i]; weight[0] = a[0]; FOR(i,1,n) weight[i] = weight[i-1]+a[i]*(i+1); rweight[0] = a[0]*n; FOR(i,1,n) rweight[i] = rweight[i-1]+a[i]*(n-i); int q; cin >> q; while (q--){ int t; cin >> t; if (t == 1){ int l; cin >> l; }else{ int l, r, m; cin >> l >> r >> m; l--;r--; int length = r-l+1; int o = min(m, length-m+1); int r1 = l+o-1; int l2 = r-(o-1); // cout << l << r1 << endl; int ans = sweight(l, r1); // cout << ans << endl; if (r1 +1 <= l2-1) ans += o*sprefix(r1+1, l2-1); // cout << ans << endl; if (l2 > r1) ans += srweight(l2, r); cout << ans << endl; } // cout << srweight(1, 4) << endl; } } int32_t main(){ // FastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // int t; cin >> t; int t = 1; for (int tc = 1; tc <= t; ++tc){ // cout << "Case #" << tc << ": "; solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...