# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
515794 |
2022-01-19T16:54:35 Z |
Jarif_Rahman |
Fire (JOI20_ho_t5) |
C++17 |
|
1000 ms |
30784 KB |
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
#define debug(s) for(auto x: s) cerr << x << " "; cerr << #s << "\n";
struct BIT{
int n;
vector<ll> sm;
BIT(int _n){
n = _n;
sm.resize(n);
}
void add(int in, int x){
in++;
while(in <= n) sm[in-1]+=x, in+=in&-in;
}
ll sum(int in){
in++;
ll s = 0;
while(in >= 1) s+=sm[in-1], in-=in&-in;
return s;
}
ll sum(int l, int r){
return sum(r)-(l == 0? 0:sum(l-1));
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
vector<ll> s(n);
for(ll &x: s) cin >> x;
vector<int> prev(n, -1);
stack<int> st;
for(int i = n-1; i >= 0; i--){
while(!st.empty() && s[i] > s[st.top()]) prev[st.top()] = i, st.pop();
st.push(i);
}
while(!st.empty()) st.pop();
vector<vector<int>> end(n+1);
vector<vector<pair<int, int>>> chng(n+1);
for(int i = 0; i < n; i++){
while(!st.empty() && s[st.top()] <= s[i]){
int x = st.top();
st.pop();
end[i-x-1].pb(x);
if(prev[x] == -1) continue;
if(s[prev[x]] <= s[i]) continue;
chng[i-prev[x]-1].pb({prev[x], i});
}
st.push(i);
}
while(!st.empty()) end[n-st.top()-1].pb(st.top()), st.pop();
vector<vector<array<int, 3>>> queries(n);
for(int i = 0; i < q; i++){
int t, l, r; cin >> t >> l >> r; t--, l--, r--;
queries[t].pb({l, r, i});
}
vector<ll> ans(q, 0);
BIT ss(n), bit1(n), bit2(n);
for(int i = 0; i < n; i++) ss.add(i, s[i]);
vector<ll> sth(n, 0);
for(int i = 0; i+1 < n; i++) sth[i] = max(0LL, s[i]-s[i+1]);
for(int t = 1; t <= n; t++){
for(int in: end[t-1]){
if(in != n-1) for(int i = in+1; i <= in+t-1; i++) ss.add(i, sth[in]);
sth[in] = 0;
}
for(auto [in, nxt]: chng[t-1]){
ll x = sth[in]-(s[in]-s[nxt]);
for(int i = in+1; i < nxt; i++) ss.add(i, x);
sth[in] = (s[in]-s[nxt]);
}
for(auto [l, r, in]: queries[t-1]){
ans[in]+=ss.sum(l, r);
for(int i = 0; i < n-1; i++){
for(int j = i+1; j <= i+t; j++) if(j >= l && j <= r) ans[in]+=sth[i];
}
}
}
for(ll x: ans) cout << x << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
6 ms |
288 KB |
Output is correct |
3 |
Correct |
5 ms |
352 KB |
Output is correct |
4 |
Correct |
6 ms |
308 KB |
Output is correct |
5 |
Correct |
5 ms |
288 KB |
Output is correct |
6 |
Correct |
5 ms |
308 KB |
Output is correct |
7 |
Correct |
5 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
312 KB |
Output is correct |
9 |
Correct |
5 ms |
332 KB |
Output is correct |
10 |
Correct |
6 ms |
316 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
6 ms |
440 KB |
Output is correct |
13 |
Correct |
9 ms |
356 KB |
Output is correct |
14 |
Correct |
9 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
324 KB |
Output is correct |
16 |
Correct |
10 ms |
324 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
308 KB |
Output is correct |
19 |
Correct |
8 ms |
356 KB |
Output is correct |
20 |
Correct |
8 ms |
332 KB |
Output is correct |
21 |
Correct |
9 ms |
352 KB |
Output is correct |
22 |
Correct |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
300 KB |
Output is correct |
24 |
Correct |
1 ms |
292 KB |
Output is correct |
25 |
Correct |
1 ms |
312 KB |
Output is correct |
26 |
Correct |
2 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Execution timed out |
1071 ms |
28912 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Execution timed out |
1048 ms |
30784 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1096 ms |
30028 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
6 ms |
288 KB |
Output is correct |
3 |
Correct |
5 ms |
352 KB |
Output is correct |
4 |
Correct |
6 ms |
308 KB |
Output is correct |
5 |
Correct |
5 ms |
288 KB |
Output is correct |
6 |
Correct |
5 ms |
308 KB |
Output is correct |
7 |
Correct |
5 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
312 KB |
Output is correct |
9 |
Correct |
5 ms |
332 KB |
Output is correct |
10 |
Correct |
6 ms |
316 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
6 ms |
440 KB |
Output is correct |
13 |
Correct |
9 ms |
356 KB |
Output is correct |
14 |
Correct |
9 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
324 KB |
Output is correct |
16 |
Correct |
10 ms |
324 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
308 KB |
Output is correct |
19 |
Correct |
8 ms |
356 KB |
Output is correct |
20 |
Correct |
8 ms |
332 KB |
Output is correct |
21 |
Correct |
9 ms |
352 KB |
Output is correct |
22 |
Correct |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
300 KB |
Output is correct |
24 |
Correct |
1 ms |
292 KB |
Output is correct |
25 |
Correct |
1 ms |
312 KB |
Output is correct |
26 |
Correct |
2 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Execution timed out |
1071 ms |
28912 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |