Submission #974464

# Submission time Handle Problem Language Result Execution time Memory
974464 2024-05-03T11:15:29 Z yusuf12360 Sequence (APIO23_sequence) C++17
0 / 100
681 ms 82260 KB
#include "sequence.h"
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5, INF=1e9;
int n;
struct node{
    int mn, mx;
    node(int mn=0, int mx=0) : mn(mn), mx(mx) {}
};
node pull(node L, node R) {
    int MN, MX;
    MN=min(L.mn, R.mn);
    MX=max(L.mx, R.mx);
    return node(MN, MX);
}
struct ST{
    node tr[4*N]; 
    int lazy[4*N];
    
    node build(int pos=0, int l=0, int r=n) {
        lazy[pos]=0;
        if(l==r) return node(l, r);
        int mid=l+r>>1;
        return tr[pos]=pull(build(2*pos+1, l, mid), build(2*pos+2, mid+1, r));
    }
    void change(int pos, int add) {
        tr[pos].mn+=add;
        tr[pos].mx+=add;
        lazy[pos]+=add;
    }
    void push(int pos) {
        change(2*pos+1, lazy[pos]);
        change(2*pos+2, lazy[pos]);
        lazy[pos]=0;
    }
    void update(int ll, int rr, int add, int pos=0, int l=0, int r=n) {
        if(ll>rr) return;
        if(ll==l && rr==r) {change(pos, add); return;}
        push(pos); int mid=l+r>>1;
        update(ll, min(rr, mid), add, 2*pos+1, l, mid);
        update(max(mid+1, ll), rr, add, 2*pos+2, mid+1, r);
        tr[pos]=pull(tr[2*pos+1], tr[2*pos+2]);
    }
    node get(int ll, int rr, int pos=0, int l=0, int r=n) {
        if(ll>rr) return node(INF, -INF);
        if(ll==l && rr==r) return tr[pos];
        push(pos);int mid=l+r>>1; 
        return pull(get(ll, min(rr, mid), 2*pos+1, l, mid), get(max(mid+1, ll), rr, 2*pos+2, mid+1, r));
    }
};

int sequence(int N, vector<int> a) {
    n=N;
    vector<vector<int>> pos(n+1);
    ST nega, posi;
    nega.build();
    posi.build();
    for(int i=0; i<n; i++) pos[a[i]].push_back(i+1);
    int ans=1;
    for(int val=1; val<=n; val++) {
        int SZ=pos[val].size();
        for(int p : pos[val]) nega.update(p, n-1, -2);
        for(int p : pos[val-1]) posi.update(p, n-1, -2);
        
        for(int i=0; i<SZ; i++) {
            while(i+ans<SZ) {
                int L=pos[val][i], R=pos[val][i+ans];
                
                int mini=nega.get(R, n).mn-nega.get(0, L-1).mx;
                int maxi=posi.get(R, n).mx-posi.get(0, L-1).mn;
                if(mini<=0 && maxi>=0) ans++;
            }
        }
    }
    return ans;
}

Compilation message

sequence.cpp: In member function 'node ST::build(int, int, int)':
sequence.cpp:23:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |         int mid=l+r>>1;
      |                 ~^~
sequence.cpp: In member function 'void ST::update(int, int, int, int, int, int)':
sequence.cpp:39:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   39 |         push(pos); int mid=l+r>>1;
      |                            ~^~
sequence.cpp: In member function 'node ST::get(int, int, int, int, int)':
sequence.cpp:47:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |         push(pos);int mid=l+r>>1;
      |                           ~^~
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 47376 KB Output is correct
2 Correct 681 ms 67076 KB Output is correct
3 Incorrect 622 ms 66228 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 678 ms 82260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -