#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define vi vector<int>
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int MAX = 4e5 + 15;
int n, q;
int a[MAX];
int qL[MAX], qR[MAX];
ll pref[MAX];
int lst[MAX];
namespace subtask1{
bool check(void){
return max(n, q) <= 5000;
}
int dp[5005];
int calc(int l, int r){
fill(dp + 1, dp + 1 + n, 0);
for(int i = l; i <= r; i++){
dp[i] = dp[i-1];
if(lst[i] >= l) maximize(dp[i], dp[lst[i]-1] + 1);
}
return dp[r];
}
void main(){
fill(lst + 1, lst + 1 + n, -1);
for(int i = 1; i <= n; i++){
for(int j = i-1; j >= 0; j--) if(pref[i] == pref[j]){
lst[i] = j+1;
break;
}
}
for(int i = 1; i <= q; i++){
cout << calc(qL[i], qR[i]) << endl;
}
}
}
namespace subtask3{
map<ll, int> mp;
int nearest[MAX];
int spt[MAX][19];
void main(){
fill(lst + 1, lst + 1 + n, -1);
mp[0] = 1;
for(int i = 1; i <= n; i++){
if(mp.count(pref[i])) lst[i] = mp[pref[i]];
mp[pref[i]] = i+1;
}
for(int i = 1, maL = 0; i <= n; i++) if(lst[i]){
if(maL >= lst[i]) lst[i] = 0;
else maximize(maL, lst[i]);
nearest[i] = maL;
}
for(int i = 1; i <= n; i++) if(lst[i]){
spt[lst[i]][0] = nearest[lst[i]-1];
}
int lg = 31 - __builtin_clz(n);
for(int j = 1; j <= lg; j++){
for(int i = n; i - (1 << j) + 1 > 0 ; i--){
spt[i][j] = spt[spt[i][j-1]][j-1];
}
}
for(int i = 1; i <= q; i++){
int l, r; tie(l, r) = {qL[i], qR[i]};
int jump = nearest[r];
int answer = (nearest[r] >= l);
for(int j = 31 - __builtin_clz(r - l + 1); j >= 0; j--){
if(spt[jump][j] >= l){
jump = spt[jump][j];
answer += (1 << j);
}
}
cout << answer << endl;
}
}
}
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
cin >> q;
for(int i = 1; i <= q; i++){
cin >> qL[i] >> qR[i];
}
if(subtask1::check()) subtask1::main();
else subtask3::main();
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |