이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=400005, lg=9;
long long *pre;
ll *last, *to, **sp;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
pre=new long long[n+5];
vector <long long> num; num.pb(0);
for (ll i=1; i<=n; i++)
cin >> pre[i], pre[i]+=pre[i-1], num.pb(pre[i]);
sort(num.begin(), num.end());
num.resize(unique(num.begin(), num.end())-num.begin());
for (ll i=0; i<=n; i++)
pre[i]=lower_bound(num.begin(), num.end(), pre[i])-num.begin();
{vector <long long> ().swap(num);}
to=new ll[n+5], last=new ll[n+5];
for (ll i=0; i<=n+2; i++) to[i]=n+2, last[i]=-1;
for (ll i=0; i<=n; i++)
{
if (last[pre[i]]!=-1) to[last[pre[i]]]=i+1;
last[pre[i]]=i+1;
}
delete[] pre, delete[] last;
for (ll i=n; i>=1; i--) to[i]=min(to[i], to[i+1]);
sp=new ll*[lg];
for (int i=0; i<lg; i++) sp[i]=new ll[n+5];
for (ll i=1; i<=n+2; i++) sp[0][i]=to[i];
delete[] to;
for (ll i=1; i<lg; i++)
for (ll j=1; j<=n+2; j++)
sp[i][j]=sp[i-1][sp[i-1][j]];
ll q; cin >> q;
for (ll i=1; i<=q; i++)
{
ll l, r, ans=0; cin >> l >> r;
while (sp[lg-1][l]<=r+1)
l=sp[lg-1][l], ans+=1<<(lg-1);
for (ll i=lg-1; i>=0; i--)
if (sp[i][l]<=r+1)
l=sp[i][l], ans+=1<<i;
cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |