# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1246743 | hainam2k9 | Sum Zero (RMI20_sumzero) | C++20 | 420 ms | 25064 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 4e5+5;
const string NAME = "";
int n,q,a[MAXN],nxt[7][MAXN];
int LG=0;
const int pow7[]={1,7,49,343,2401,16807,117649};
map<ll,int> pos;
void calcLG(int n){
while(n>=7) ++LG, n/=7;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> a[i];
calcLG(n);
memset(nxt,0x3f,sizeof(nxt));
pos[0]=n+1;
ll sum=0;
for(int i = n; i>0; --i){
sum+=a[i];
if(pos.count(sum)) nxt[0][i]=pos[sum]-1;
nxt[0][i]=min(nxt[0][i],nxt[0][i+1]);
pos[sum]=i;
}
map<ll,int>().swap(pos);
for(int j = 1; j<=LG; ++j){
for(int i = 1; i<=n; ++i){
int cur=nxt[j-1][i];
for(int k = 1; k<=6; ++k)
if(cur<=n) cur=nxt[j-1][cur+1];
nxt[j][i]=cur;
}
}
cin >> q;
while(q--){
int l,r,rs=0;
cin >> l >> r;
for(int i = LG; i>=0; --i){
int cur=nxt[i][l],cnt=1;
if(cur>r) continue;
for(int j = 1; j<=5; ++j)
if(cur<=n&&nxt[i][cur+1]<=r) ++cnt, cur=nxt[i][cur+1];
rs+=cnt*pow7[i], l=cur+1;
}
cout << rs << "\n";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |