Submission #1312561

#TimeUsernameProblemLanguageResultExecution timeMemory
1312561_TemirhanExam (eJOI20_exam)C++20
12 / 100
48 ms9036 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>  
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,unroll-loops,inline-functions,no-stack-protector,03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
#pragma clang loop vectorize(enable)
#pragma clang loop interleave(enable)

#define int long long
#define sz(x) (int)x.size()
#define F first
#define S second
#define pb push_back
#define nl '\n'
#define o_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;

void file( string s = "" )
{
    if( s.empty() )
        return;
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int N = 2e5 + 2;
const int N1 = 1e6 + 2;
const int inf = 1e9 + 2;
const int INF = 1e18 + 2;
const int mod = 1e9 + 7;
int T = 1, n, a[N], b[N], t[N];

void upd( int i, int x )
{
    for( ; i < N; i += i & -i )
        t[i] = max(t[i], x);
}

int get( int i )
{
    int res = 0;
    for( ; i > 0; i -= i & -i )
        res = max(res, t[i]);
    return res;
}

void solve()
{
    cin >>n;
    int cnt = 0;
    vector< int > l(n + 1, 1), r(n + 1, n);
    stack< int > st;
    set< int > stt;
    for( int i = 1; i <= n; ++i )
    {
        cin >>a[i];
        stt.insert(a[i]);
    }
    for( int i = 1; i <= n; ++i )
    {
        while( !st.empty() && a[st.top()] <= b[i] )
            st.pop();
        if( !st.empty() )
            l[i] = st.top() + 1;
        st.push(i);
    }
    while( !st.empty() )
        st.pop();
    for( int i = n; i >= 1; --i )
    {
        while( !st.empty() && a[st.top()] <= b[i] )
            st.pop();
        if( !st.empty() )
            r[i] = st.top() - 1;
        st.push(i);
    }
    for( int i = 1; i <= n; ++i )
    {
        cin >>b[i];
        if( b[i] == b[1] )
            ++cnt;
    }
    if( cnt == n )
    {
        int ans = 0;
        int ch = 0, c = 0;
        for( int i = 1; i <= n; ++i )
        {
            if( a[i] > b[1] )
            {
                ans += ch * c;
                c = ch = 0;
                continue;
            }
            ++c;
            if( a[i] == b[1] )
                ch = 1;
        }
        ans += c * ch;
        cout <<ans;
        return;
    }
    if( sz(stt) == n )
    {
        int ans = 0;
        map<int, int> pos;
        for( int i = 1; i <= n; ++i )
            pos[a[i]] = i;
        for( int j = 1; j <= n; ++j )
        {
            if( !pos.count(b[j]) )
                continue;
            int i = pos[b[j]];
            if( l[i] <= j && j <= r[i] )
            {
                int x = get(i) + 1;
                upd(i, 1);
                ans = max(ans, x);
            }
        }
        cout <<ans;
        return;
    }
}

signed main()
{
    file("");
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // cin >>T;
    while( T-- )
        solve();
}

Compilation message (stderr)

exam.cpp: In function 'void file(std::string)':
exam.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exam.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...