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>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 101010; // PLZ CHK!
typedef array<int,3> arr;
struct segtr{
vector<ll> tr;
int n;
void rst(int sz) {
n=sz;
tr.resize((n+1)<<1);
}
void upd(int i, ll v) {
tr[i+n]+=v; i+=n; //AWARE OF UPD POLICY!!
for (i>>=1; i; i>>=1) tr[i] = max(tr[i<<1], tr[i<<1|1]);
}
ll qry(int l, int r) {
ll ret=0;
for (l+=n, r+=n; l<=r; l>>=1, r>>=1) {
if (l&1) ret = max(ret, tr[l++]);
if (~r&1) ret = max(ret, tr[r--]);
}
return ret;
}
};
int N,Q;
ll X[MAX],ans[MAX];
arr qry[MAX];
int main() {
fio();
cin>>N>>Q;
vector<ll> t;
for (int i=1; i<=N; i++) {
cin>>X[i];
t.pb(X[i]);
}
t.pb(0);
cmprs(t);
for (int i=1; i<=N; i++) {
X[i]=lb(all(t),X[i])-t.begin();
}
for (int i=1; i<=Q; i++) {
int l,r; cin>>l>>r;
qry[i]={l,r,i};
}
const int sq=(int)sqrt(N);
sort(qry+1,qry+Q+1,[&](auto a, auto b){
auto [al,ar,ai]=a; auto [bl,br,bi]=b;
if (al/sq==bl/sq) return ar<br;
return al/sq<bl/sq;
});
segtr seg; seg.rst(N+10);
for (int l=1,r=0,q=1; q<=Q; q++) {
auto [ql,qr,idx]=qry[q];
while (r<qr) {
r++;
int j=X[r]; ll val=t[j];
seg.upd(j,val);
}
while (ql<l) {
l--;
int j=X[l]; ll val=t[j];
seg.upd(j,val);
}
while (qr<r) {
int j=X[r]; ll val=t[j];
seg.upd(j,-val);
r--;
}
while (l<ql) {
int j=X[l]; ll val=t[j];
seg.upd(j,-val);
l++;
}
ans[idx]=seg.qry(0,N+1);
}
for (int i=1; i<=Q; i++) cout<<ans[i]<<endl;
return 0;
}
# | 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... |