제출 #1279876

#제출 시각아이디문제언어결과실행 시간메모리
1279876mactoddloverSum Zero (RMI20_sumzero)C++17
61 / 100
1096 ms24224 KiB
#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];
ll pref[MAX];
int lst[MAX];
int log8[8];

namespace subtask3{
    int mp[MAX];
    int nearest[MAX];
    int spt[MAX][7];
    vector<ll> compress;

    void main(){
        for(int i = 0; i <= n; i++) compress.push_back(pref[i]);
        sort(all(compress)); compact(compress);

        for(int i = 0; i <= n; i++){
            int x = lower_bound(all(compress), pref[i]) - compress.begin();
            if(i && mp[x]) lst[i] = mp[x];
            mp[x] = 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];
        }

        log8[0] = 1;
        for(int i = 1; i < 8; i++) log8[i] = log8[i-1]*8;

        for(int j = 1; j < 7; j++){
            for(int i = 1; i <= n; i++){
                int jump = spt[i][j-1];
                for(int k = 0; k < 7; k++) jump = spt[jump][j-1];
                spt[i][j] = jump;
            }
        }

        cin >> q;

        auto kth = [&](int i, int j) -> int{
            if(!j) return spt[i][j];
            for(int k = 0; k < 8; k++){
                i = spt[i][j-1];
                if(!i) return i;
            }
            return i;
        };

        while(q--){
            int l, r; cin >> l >> r;
            int jump = nearest[r];
            int answer = (nearest[r] >= l);

            while(jump >= l){
                int pjump = jump;
                for(int j = 6; j >= 0; j--){
                    int nxt = kth(jump, j);
                    if(nxt >= l){
                        answer += log8[j];
                        jump = nxt;
                    }
                }
                if(jump == pjump) break;
            }

            cout << answer << endl;
        }
    }
}

void solve(){
    cin >> n;

    for(int i = 1; i <= n; i++){
        cin >> a[i];
        pref[i] = pref[i-1] + a[i];
    }

    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();
}

컴파일 시 표준 에러 (stderr) 메시지

sumzero.cpp: In function 'int main()':
sumzero.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...