제출 #1092743

#제출 시각아이디문제언어결과실행 시간메모리
1092743De3b0oSum Zero (RMI20_sumzero)C++17
100 / 100
370 ms17744 KiB
#include<bits/stdc++.h>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)

using namespace std;

const int N = 400001;

int po = 14;
const int16_t lo = 4;

int n;
map<int,int> mp;
int b[N][lo];
int pwr[lo];
int sum;

int main()
{
    d3
    cin >> n;
    for(int i = 1 ; n>=i ; i++)
        b[i][0]=n+1;
    mp[0]=0;
    for(int i = 1 ; n>=i ; i++)
    {
        int x;
        cin >> x;
        sum+=x;
        if(mp[sum]==0&&sum!=0)
        {
            mp[sum]=i;
            continue;
        }
        int e = mp[sum]+1;
        mp[sum]=i;
        b[e][0]=min(b[e][0],i);
    }
    for(int16_t j = 0 ; lo>j ; j++)
        b[n+1][j]=n+1;
    for(int i = n ; i>0 ; i--)
        b[i][0]=min(b[i][0],b[i+1][0]);
    for(int16_t j = 1 ; lo>j ; j++)
    {
        for(int i = 1 ; n>=i ; i++)
        {
            int16_t sz = po-1;
            b[i][j]=b[i][j-1];
            while(sz--)
            {
                if(b[i][j]==n+1)
                    break;
                b[i][j]=b[b[i][j]+1][j-1];
            }
        }
    }
    pwr[0]=1;
    for(int i = 1 ; lo>i ; i++)
        pwr[i]=pwr[i-1]*po;
    cin >> n;
    while(n--)
    {
        int l , r;
        cin >> l >> r;
        int ans = 0;
        for(int16_t j = lo-1 ; j>=0 ; j--)
        {
            if(l>r)
                break;
            while(true)
            {
                if(b[l][j]>r)
                    break;
                ans+=pwr[j];
                l=b[l][j]+1;
            }
        }
        cans
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...