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>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=2e5+5;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2);
}
//=======================================================================================
vector<int> seg(4*maxn);
vector<int> vc(maxn);
void build(int l, int r, int x){
if (l==r){
seg[x]=vc[l];
return;
}
int mid=(l+r)/2;
build(l, mid, x+x);
build(mid+1, r, x+x+1);
seg[x]=max(seg[x+x], seg[x+x+1]);
}
int query(int l, int r, int nl, int nr, int x){
if (r<nl||l>nr){
return 0;
}
if (nl>=l&&nr<=r){
return seg[x];
}
int mid=(nl+nr)/2;
return max(query(l, r, nl, mid, x+x), query(l, r, mid+1, nr, x+x+1));
}
struct typ{
int l, r;
int t;
};
signed main (){
IOS();
int n, q; cin>>n>>q;
REP1(i,n) cin>>vc[i];
vector<typ> vq(q);
build(1, n, 1);
bool gg=1;
REP(i, q){
cin>>vq[i].t>>vq[i].l>>vq[i].r;
gg=(gg&(vq[i].l==vq[i].r));
}
if (n<=200&&q<=200){
REP(i,q){
int sm=0;
FOR(j, vq[i].l, vq[i].r+1){
int l=max(j-vq[i].t, 1ll);
sm+=query(l, j, 1, n, 1);
}
cout<<sm<<endl;
}
return 0;
}
if (!gg){
vector<int> pf(n+1);
REP1(i,n){
int l=max(i-vq[0].t, 1ll);
pf[i]=pf[i-1]+query(l, i, 1, n, 1);
}
REP(i, q){
cout<<pf[vq[i].r]-pf[vq[i].l-1]<<endl;
}
}
else {
REP(i, q){
int l=max(vq[i].l-vq[i].t, 1ll);
cout<<query(l, vq[i].r, 1, n, 1)<<endl;
}
}
}
# | 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... |