# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202854 | Mercenary | Fire (JOI20_ho_t5) | C++14 | 390 ms | 59636 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 2e5 + 6;
typedef pair<int,int> ii;
typedef long long ll;
ll bit[maxn];
void U(int x , int val){
assert(x > 0);
for( ; x < maxn ; x += x & -x)bit[x] += val;
}
ll Q(int x){
if(x <= 0)return 0;
ll res = 0;
for( ; x > 0 ; x &= x - 1)res += bit[x];
return res;
}
int n , q;
int s[maxn] , pre[maxn] , suf[maxn];
vector<pair<ii,int>> query[maxn];
ll ans[maxn];
vector<ii> f[maxn] , g[maxn];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP","r",stdin);
freopen(taskname".OUT","w",stdout);
}
cin >> n >> q;
for(int i = 1 ; i <= n ; ++i){
cin >> s[i];
}
for(int i = 1 ; i <= q ; ++i){
int t , l , r;cin >> t >> l >> r;
query[t].pb(mp(mp(l,r),i));
}
vector<int> val;
for(int i = 1 ; i <= n ; ++i){
while(val.size() && s[val.back()] <= s[i])val.pop_back();
if(val.size())pre[i] = val.back();
else pre[i] = 0;
val.pb(i);
}
val.clear();
for(int i = n ; i >= 1 ; --i){
while(val.size() && s[val.back()] < s[i])val.pop_back();
if(val.size())suf[i] = val.back();
else suf[i] = n + 1;
val.pb(i);
}
for(int i = 1 ; i <= n ; ++i){
if(pre[i] == 0 || i - pre[i] > suf[i] - i){
for(int j = i ; j < suf[i] ; ++j){
f[j - i].pb(mp(j,s[i]));
if(pre[i])f[j - pre[i]].pb(mp(j,-s[i]));
}
}else{
for(int j = pre[i] + 1 ; j <= i ; ++j){
g[i - j].pb(mp(j,s[i]));
g[suf[i] - j].pb(mp(j,-s[i]));
}
}
}
for(int i = 0 ; i <= n ; ++i){
for(auto & c : f[i]){
U(c.first,c.second);
}
for(auto & c : query[i]){
ans[c.second] += Q(c.first.second) - Q(c.first.first-1);
}
}
fill_n(bit,maxn,0);
for(int i = 0 ; i <= n ; ++i){
for(auto & c : g[i]){
U(c.first,c.second);
}
for(auto & c : query[i]){
ans[c.second] += Q(c.first.second - i) - Q(c.first.first-i-1);
}
}
for(int i = 1 ; i <= q ; ++i)cout << ans[i] << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |