제출 #1036302

#제출 시각아이디문제언어결과실행 시간메모리
1036302parsadox2Fish 3 (JOI24_fish3)C++17
0 / 100
181 ms49260 KiB
#include <bits/stdc++.h> #define int long long #define F first #define S second using namespace std; const int N = 3e5 + 10; int n , a[N] , D , ans[N] , q , s[N]; vector <pair<int , int>> qu[N]; struct SEG{ int a[N << 2] , b[N << 2]; void Add(int l , int r , int A , int B , int node = 1 , int nl = 1 , int nr = n + 1) { if(r <= nl || nr <= l) return; if(l <= nl && nr <= r) { a[node] += A; b[node] += B; return; } int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1; Add(l , r , A , B , lc , nl , mid); Add(l , r , A , B , rc , mid , nr); } int Get(int id , int node = 1 , int nl = 1 , int nr = n + 1) { int res = a[node] * id + b[node]; if(nr - nl == 1) return res; int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1; if(id < mid) res += Get(id , lc , nl , mid); else res += Get(id , rc , mid , nr); return res; } } seg; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> D; for(int i = 1 ; i <= n ; i++) cin >> a[i]; cin >> q; for(int i = 0 ; i < q ; i++) { int l , r; cin >> l >> r; qu[r].push_back(make_pair(l , i)); } vector <int> st; for(int i = 1 ; i <= n ; i++) { if(a[i] >= a[i - 1]) { s[i] = (a[i] - a[i - 1]) / D; st.push_back(i); } else { int k = (a[i - 1] - a[i] + D - 1) / D; seg.Add(1 , i , -k , k * i); while(!st.empty() && k > 0) { int tmp = min(k , s[st.back()]); seg.Add(1 , st.back() , tmp , -tmp * i); s[st.back()] -= tmp; k -= tmp; if(s[st.back()] == 0) st.pop_back(); } } for(auto u : qu[i]) ans[u.S] = seg.Get(u.F); } for(int i = 0 ; i < q ; i++) cout << ans[i] << '\n'; return 0; }
#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...