Submission #942684

#TimeUsernameProblemLanguageResultExecution timeMemory
942684Tuanlinh123Sum Zero (RMI20_sumzero)C++17
100 / 100
736 ms18340 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...