Submission #753366

#TimeUsernameProblemLanguageResultExecution timeMemory
753366keta_tsimakuridzeSum Zero (RMI20_sumzero)C++14
61 / 100
257 ms21668 KiB
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
using namespace std;
const int N = 4e5 + 2, mod = 1e9 + 7; // !
int t, R[N][2], f[N];
ll a[N];
vector<pair<ll, int>> x;
main(){
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
        x.push_back({a[i], i});
    }
    x.push_back({0, 0});
    sort(x.begin(), x.end());
    int cur = 0;
    vector<int> id(n + 1);
    for(int i = 0; i < x.size(); i++) {
        if(!i || x[i].f != x[i - 1].f) ++cur;
        id[x[i].s] = cur;
    }
    int mn = n + 1;
    for(int j = 0; j <= 1; j++) R[n + 1][j] = n + 1;
    vector<int> x(n + 5);
    for(int i = n; i >= 0; i--) {
        if(f[id[i]]) mn = min(mn, f[id[i]]);
        x[i] = mn;
        f[id[i]] = i;
    }
    id.clear(); id.shrink_to_fit();
    int q;
    cin >> q;
    vector<int> l(q + 1), r(q + 1), ans(q + 1);
    for(int i = 1; i <= q; i++) {
        cin >> l[i] >> r[i];
        --l[i];
    }

    for(int I = 9; I >= 0; I--) {
        // i * 5 ... i * 5 + 4
        for(int i = 0; i <= n; i++) R[i][0] = x[i];
        for(int j = 1; j <= I * 2 + 1; j++) {
            for(int i = 0; i <= n; i++) {
                R[i][j % 2] = R[R[i][(j % 2 + 1) % 2]][(j % 2 + 1) % 2];
            }
        }
        for(int i = 1; i <= q; i++) {
            for(int j = 1; j >= 0; j--) if(R[l[i]][j] <= r[i]) ans[i] += 1 << (I * 2 + j), l[i] = R[l[i]][j];
        }
    }
    for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
 }

Compilation message (stderr)

sumzero.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   11 | main(){
      | ^~~~
sumzero.cpp: In function 'int main()':
sumzero.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < x.size(); i++) {
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...