Submission #1010715

#TimeUsernameProblemLanguageResultExecution timeMemory
1010715mychecksedadFish 3 (JOI24_fish3)C++17
100 / 100
493 ms142536 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' const int N = 3e5+100, M = 1e5+10, K = 20, MX = 30; int n, q, to[N][K]; ll D, C[N], B[N], suf[N], arr[N], rmq[N][K], pref[N], S[N], dp[N][K]; ll get(int l, int r){ int k= log2(r-l+1); return min(rmq[l][k], rmq[r-(1<<k)+1][k]); } void solve(){ cin >> n >> D; for(int i = 1; i <= n; ++i) cin >> C[i]; B[1] = C[1] % D; for(int i = 2; i <= n; ++i){ if(C[i] % D >= C[i-1] % D){ B[i] = B[i-1] + (C[i]%D) - (C[i-1]%D); }else{ B[i] = B[i-1] + D + (C[i]%D) - (C[i-1]%D); } } for(int i = 1; i <= n; ++i) arr[i] = rmq[i][0] = C[i] - B[i]; for(int j = 1; j < K ;++j){ for(int i = 1; i + (1<<j) <= n+1; ++i){ rmq[i][j] = min(rmq[i][j - 1], rmq[i+(1<<(j-1))][j-1]); } } pref[0] = 0; S[0] = 0; C[0] = arr[0] = 0; for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + B[i]; for(int i = 1; i <= n; ++i) S[i] = S[i - 1] + C[i]; vector<int> Q; to[1][0] = 0; dp[1][0] = arr[1] * (1 - to[1][0]); Q.pb(1); for(int i = 2; i <= n; ++i){ while(!Q.empty() && arr[Q.back()] > arr[i]) Q.pop_back(); if(Q.empty()) to[i][0] = 0; else to[i][0] = Q.back(); dp[i][0] = arr[i] * (i - to[i][0]); Q.pb(i); } for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i){ dp[i][j] = dp[i][j - 1] + dp[to[i][j - 1]][j - 1]; to[i][j] = to[to[i][j - 1]][j - 1]; } cin >> q; for(int i = 1; i <= q; ++i){ int l, r; cin >> l >> r; ll ans = 0; ll mn = get(l, r); ll R = B[l] - (C[l]%D); if(mn + R < 0){ cout << -1 << '\n'; continue; } ans = (S[r] - S[l-1]) - (pref[r] - pref[l-1]); int p = r; for(int j = K-1; j >= 0; --j){ if(to[p][j] >= l){ ans -= dp[p][j]; p = to[p][j]; } } ans -= (p-l+1)*arr[p]; cout << ans / D << '\n'; } } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:88:15: warning: unused variable 'aa' [-Wunused-variable]
   88 |   int tt = 1, aa;
      |               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...