제출 #790193

#제출 시각아이디문제언어결과실행 시간메모리
790193GrindMachineTenis (COI19_tenis)C++17
100 / 100
217 ms8224 KiB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

inspired by some submissions from:
https://github.com/mostafa-saad/MyCompetitiveProgramming/blob/master/Olympiad/COI/COI-19-tenis.txt

the winners are on some pref of court 1
why? because if i cant win and j can win, pos1(i) < pos2(j), then i can win j and hence, can also win the tournament, which is a contradiction

same observation can be extended to all courts

winners lie on some pref of all courts

find the smallest k s.t the multisets a[1..k], b[1..k], c[1..k] are all equal, check if min_pos(i) <= k

let's say our current set initially equals a[1..k]
when adding guys from b[1..k] and c[1..k], no extra element should be added to the set, otherwise our condition is violated

rephrasing the above condition:
set_union(a[1..k],b[1..k],c[1..k]).size() = k, then ans is yes

verbally,
the #of distinct elements in the array {a[1..k]+b[1..k]+c[1..k]} = k

find the smallest k that satisfies this condition

to find the #of distinct elements in the array formed for some k, just look at the min_pos of each player in all 3 courts combined

add 1 at this min_pos for each player

find the first pos where sum(1..k)-k = 0
we know that sum(1..k)-k >= 0, so just find the min val and its leftmost occurrence

we dont have to find k, just check if i is winning or not, so just check if no zero lies on [1..min_pos(i)-1] i.e min(1..min_pos(i)-1) > 0

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct lazysegtree {
    /*=======================================================*/

    struct data {
        int a;
    };

    struct lazy {
        int a;
    };

    data d_neutral = {inf1};
    lazy l_neutral = {0};

    void merge(data &curr, data &left, data &right) {
        curr.a = min(left.a,right.a);
    }

    void create(int x, int lx, int rx, T v) {

    }

    void modify(int x, int lx, int rx, T v) {
        lz[x].a = v;
    }

    void propagate(int x, int lx, int rx) {
        int v = lz[x].a;
        if(!v) return;

        tr[x].a += v;

        if(rx-lx > 1){
            lz[2*x+1].a += v;
            lz[2*x+2].a += v;
        }

        lz[x] = l_neutral;
    }

    /*=======================================================*/

    int siz = 1;
    vector<data> tr;
    vector<lazy> lz;

    lazysegtree() {

    }

    lazysegtree(int n) {
        while (siz < n) siz *= 2;
        tr.assign(2 * siz, d_neutral);
        lz.assign(2 * siz, l_neutral);
    }

    void build(vector<T> &a, int n, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                create(x, lx, rx, a[lx]);
            }

            return;
        }

        int mid = (lx + rx) / 2;

        build(a, n, 2 * x + 1, lx, mid);
        build(a, n, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void build(vector<T> &a, int n) {
        build(a, n, 0, 0, siz);
    }

    void rupd(int l, int r, T v, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return;
        if (lx >= l and rx <= r) {
            modify(x, lx, rx, v);
            propagate(x, lx, rx);
            return;
        }

        int mid = (lx + rx) / 2;

        rupd(l, r, v, 2 * x + 1, lx, mid);
        rupd(l, r, v, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void rupd(int l, int r, T v) {
        rupd(l, r + 1, v, 0, 0, siz);
    }

    data query(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return d_neutral;
        if (lx >= l and rx <= r) return tr[x];

        int mid = (lx + rx) / 2;

        data curr;
        data left = query(l, r, 2 * x + 1, lx, mid);
        data right = query(l, r, 2 * x + 2, mid, rx);

        merge(curr, left, right);
        return curr;
    }

    data query(int l, int r) {
        return query(l, r + 1, 0, 0, siz);
    }
};

int a[5][N], pos[5][N];
 
void solve(int test_case)
{
    int n,q; cin >> n >> q;
    vector<int> mn_pos(n+5,inf1);

    rep1(p,3){
        rep1(i,n) cin >> a[p][i];
        rep1(i,n){
            int x = a[p][i];
            pos[p][x] = i;
            amin(mn_pos[x],i);            
        }
    }
            
    lazysegtree<int> st(n+5);
    rep1(i,n){
        st.rupd(i,i,-inf1-i);
    }

    auto upd = [&](int x, int add){
        mn_pos[x] = inf1;
        rep1(p,3){
            amin(mn_pos[x],pos[p][x]);
        }

        st.rupd(mn_pos[x],n,add);
    };

    rep1(i,n){
        upd(i,1);
    }

    while(q--){
        int t; cin >> t;
        if(t == 1){
            int i; cin >> i;
            if(st.query(1,mn_pos[i]-1).a > 0){
                cout << "DA" << endl;
            }
            else{
                cout << "NE" << endl;
            }
        }
        else{
            int p,x,y; cin >> p >> x >> y;
            int i = pos[p][x], j = pos[p][y];
            
            upd(x,-1);
            upd(y,-1);
            
            swap(a[p][i],a[p][j]);
            swap(pos[p][x],pos[p][y]);
            
            upd(x,1);
            upd(y,1);
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
#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...