This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const int INF = 1e6;
vector < vector <int> > pos;
inline int getmin(int x) {
    int mn = 1e9;
    for(int i = 1; i <= 3; i++) {
        mn = min(mn, pos[i][x]);
    }
    return mn;
}
inline int getmax(int x) {
    int mx = 0;
    for(int i = 1; i <= 3; i++) {
        mx = max(mx, pos[i][x]);
    }
    return mx;
}
struct SegTree {
    struct Node {
        int pref, sum;
        int pos;
    };
    vector <Node> aint;
    int n;
    inline void init(int _n) {
        n = _n;
        aint.resize(4 * n + 1);
    }
    inline void refresh(int nod) {
        aint[nod].sum = aint[2 * nod].sum + aint[2 * nod + 1].sum;
        if(aint[2 * nod].pref <= aint[2 * nod + 1].pref + aint[2 * nod].sum) {
            aint[nod].pref = aint[2 * nod].pref;
            aint[nod].pos = aint[2 * nod].pos;
        }
        else {
            aint[nod].pref = aint[2 * nod].sum + aint[2 * nod + 1].pref;
            aint[nod].pos = aint[2 * nod + 1].pos;
        }
    }
    void build(int nod, int left, int right, vector <int> &arr) {
        if(left == right) {
            aint[nod] = {arr[left], arr[left], left};
        }
        else {
            int mid = (left + right) / 2;
            build(2 * nod, left, mid, arr);
            build(2 * nod + 1, mid + 1, right, arr);
            refresh(nod);
        }
    }
    void update(int nod, int left, int right, int pos, int val) {
        if(left == right) {
            aint[nod].sum += val;
            aint[nod].pref += val;
            aint[nod].pos = left;
        }
        else {
            int mid = (left + right) / 2;
            if(pos <= mid) update(2 * nod, left, mid, pos, val);
            else update(2 * nod + 1, mid + 1, right, pos, val);
            refresh(nod);
        }
    }
};
int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, j, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> q;
    vector < vector <int> > arr(4, vector <int>(n + 1));
    pos.resize(4, vector <int>(n + 1));
    for(i = 1; i <= 3; i++) {
        for(j = 1; j <= n; j++) {
            cin >> arr[i][j];
            pos[i][arr[i][j]] = j;
        }
    }
    SegTree st; st.init(n);
    vector <int> aux(n + 1);
    for(i = 1; i <= n; i++) {
        aux[getmin(i)]++;
        aux[getmax(i)]--;
    }
    st.build(1, 1, n, aux);
    while(q > 0) {
        q--;
        int type;
        cin>> type;
        if(type == 1) {
            int x;
            cin >> x;
            cout << (st.aint[1].pos >= getmax(x) ? "DA" : "NE") << "\n";
        }
        else {
            int p, a, b;
            cin >> p >> a >> b;
            st.update(1, 1, n, getmin(a), -1);
            st.update(1, 1, n, getmax(a), 1);
            st.update(1, 1, n, getmin(b), -1);
            st.update(1, 1, n, getmax(b), 1);
            swap(arr[p][pos[p][a]], arr[p][pos[p][b]]);
            swap(pos[p][a], pos[p][b]);
            st.update(1, 1, n, getmin(a), 1);
            st.update(1, 1, n, getmax(a), -1);
            st.update(1, 1, n, getmin(b), 1);
            st.update(1, 1, n, getmax(b), -1);
        }
    }
    //cin.close();
    //cout.close();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |