답안 #790193

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790193 2023-07-22T11:59:40 Z GrindMachine Tenis (COI19_tenis) C++17
100 / 100
217 ms 8224 KB
// 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 82 ms 6844 KB Output is correct
14 Correct 78 ms 6848 KB Output is correct
15 Correct 80 ms 6724 KB Output is correct
16 Correct 80 ms 6740 KB Output is correct
17 Correct 83 ms 6784 KB Output is correct
18 Correct 80 ms 6792 KB Output is correct
19 Correct 80 ms 6724 KB Output is correct
20 Correct 80 ms 6820 KB Output is correct
21 Correct 79 ms 6836 KB Output is correct
22 Correct 82 ms 6732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 7800 KB Output is correct
2 Correct 120 ms 7800 KB Output is correct
3 Correct 119 ms 7852 KB Output is correct
4 Correct 119 ms 7800 KB Output is correct
5 Correct 121 ms 7908 KB Output is correct
6 Correct 120 ms 7796 KB Output is correct
7 Correct 119 ms 7816 KB Output is correct
8 Correct 119 ms 7932 KB Output is correct
9 Correct 134 ms 7884 KB Output is correct
10 Correct 121 ms 7796 KB Output is correct
11 Correct 121 ms 7872 KB Output is correct
12 Correct 118 ms 7868 KB Output is correct
13 Correct 126 ms 7916 KB Output is correct
14 Correct 112 ms 7884 KB Output is correct
15 Correct 118 ms 7796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 82 ms 6844 KB Output is correct
14 Correct 78 ms 6848 KB Output is correct
15 Correct 80 ms 6724 KB Output is correct
16 Correct 80 ms 6740 KB Output is correct
17 Correct 83 ms 6784 KB Output is correct
18 Correct 80 ms 6792 KB Output is correct
19 Correct 80 ms 6724 KB Output is correct
20 Correct 80 ms 6820 KB Output is correct
21 Correct 79 ms 6836 KB Output is correct
22 Correct 82 ms 6732 KB Output is correct
23 Correct 122 ms 7800 KB Output is correct
24 Correct 120 ms 7800 KB Output is correct
25 Correct 119 ms 7852 KB Output is correct
26 Correct 119 ms 7800 KB Output is correct
27 Correct 121 ms 7908 KB Output is correct
28 Correct 120 ms 7796 KB Output is correct
29 Correct 119 ms 7816 KB Output is correct
30 Correct 119 ms 7932 KB Output is correct
31 Correct 134 ms 7884 KB Output is correct
32 Correct 121 ms 7796 KB Output is correct
33 Correct 121 ms 7872 KB Output is correct
34 Correct 118 ms 7868 KB Output is correct
35 Correct 126 ms 7916 KB Output is correct
36 Correct 112 ms 7884 KB Output is correct
37 Correct 118 ms 7796 KB Output is correct
38 Correct 214 ms 8088 KB Output is correct
39 Correct 156 ms 8040 KB Output is correct
40 Correct 215 ms 8140 KB Output is correct
41 Correct 165 ms 8012 KB Output is correct
42 Correct 168 ms 8132 KB Output is correct
43 Correct 217 ms 8164 KB Output is correct
44 Correct 147 ms 7932 KB Output is correct
45 Correct 172 ms 8056 KB Output is correct
46 Correct 150 ms 7928 KB Output is correct
47 Correct 160 ms 8032 KB Output is correct
48 Correct 146 ms 7936 KB Output is correct
49 Correct 172 ms 8224 KB Output is correct
50 Correct 143 ms 7932 KB Output is correct
51 Correct 164 ms 8024 KB Output is correct
52 Correct 214 ms 8208 KB Output is correct
53 Correct 139 ms 8068 KB Output is correct
54 Correct 140 ms 8056 KB Output is correct
55 Correct 171 ms 8068 KB Output is correct
56 Correct 144 ms 7980 KB Output is correct
57 Correct 143 ms 7932 KB Output is correct
58 Correct 165 ms 8068 KB Output is correct