Submission #1246738

#TimeUsernameProblemLanguageResultExecution timeMemory
1246738hainam2k9Sum Zero (RMI20_sumzero)C++20
22 / 100
59 ms22596 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[MAXN][12];
int LG=0,pow3[]={1,3,9,27,81,243,729,2187,6561,19683,59049,177147};
map<ll,int> pos;
void calcLG(int n){
    while(n>=3) ++LG, n/=3;
}
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[i][0]=pos[sum]-1;
        nxt[i][0]=min(nxt[i][0],nxt[i+1][0]);
        for(int j = 1; j<=LG; ++j){
            int cur=nxt[i][j-1];
            for(int k = 1; k<=2; ++k)
                if(cur<=n) cur=nxt[cur+1][j-1];
            nxt[i][j]=cur;
        }
        pos[sum]=i;
    }
    cin >> q;
    while(q--){
        int l,r,rs=0;
        cin >> l >> r;
        for(int i = LG; i>=0; --i){
            int cur=nxt[l][i];
            if(cur>r) continue;
            if(cur<=n&&nxt[cur+1][i]<=r) rs+=2*pow3[i], l=nxt[cur+1][i]+1;
            else rs+=pow3[i], l=cur+1;
        }
        cout << rs << "\n";
    }
}

Compilation message (stderr)

sumzero.cpp: In function 'int main()':
sumzero.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:31:45: note: in expansion of macro 'fo'
   31 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
sumzero.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:31:45: note: in expansion of macro 'fo'
   31 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...