제출 #623439

#제출 시각아이디문제언어결과실행 시간메모리
623439Ronin13자리 배치 (IOI18_seats)C++14
33 / 100
896 ms69324 KiB
#include "seats.h"
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;


std::vector<int> r;
std :: vector <int> c;
const  int NMAX = 1e6 + 5;

int lazy[4 * NMAX];
pii t[4 * NMAX];
int n, m;
void push(int v){
    if(lazy[v] == 0) return;
    t[2 * v].f += lazy[v];
    t[2 * v + 1].f += lazy[v];
    lazy[2 * v] += lazy[v];
    lazy[2 * v + 1] += lazy[v];
    lazy[v] = 0;
    return;
}

pii merg(pii a, pii b){
    if(a.f < b.f)return a;
    if(a.f > b.f)return b;
    return {a.f, b.s + a.s};
}

void update(int v, int l, int r, int st, int fin, int val){
    if(l > fin || r < st) return;
    if(l >= st && r <= fin) {
        t[v].f += val;
        lazy[v] += val;
        return;
    }
    int m = (l + r) / 2;
    push(v);
    update(2 * v, l, m, st, fin, val);
    update(2 * v +1, m + 1, r, st, fin, val);
    t[v] = merg(t[2 * v], t[2 * v + 1]);
}

void go(int v, int l, int r){
    if(l == r){
        t[v] = {0, 1};
        return;
    }
    int m = (l + r) / 2;
    go(2 * v, l, m);
    go(2 * v + 1, m + 1, r);
    t[v]= {0, t[2 * v].s + t[2 * v + 1].s};
}

int a[NMAX];

void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
    r = R;
    c = C;
    n = H, m = W;
    go(1, 1, n * m);
    for(int i = 0; i < n * m; i++){
        int x = ++c[i];
        a[x] = i;
    }
    a[0] = n * m;
    a[m + 1] = n * m;
    for(int i = 1; i <= m + 1; i++){
        int x = a[i];
        int y = a[i - 1];
        if(x < y) update(1, 1, n * m, x + 1, y, 1);
    }
}

int swap_seats(int A, int B) {
   // cout << t[1].s << "\n";
    int x = c[A], y = c[B];
    if(x > y){
        swap(x, y);
        swap(A, B);
    }
    if(a[x] < a[x - 1]) update(1, 1, n * m, a[x] + 1, a[x - 1], -1);
    if(a[x] > a[x + 1]) update(1, 1, n * m, a[x + 1] + 1, a[x], -1);
    if(a[y] < a[y - 1] && y != x + 1) update(1, 1, n * m, a[y] + 1, a[y - 1], -1);
    if(a[y] > a[y + 1]) update(1, 1, n * m, a[y + 1] + 1, a[y], -1);
    swap(a[x], a[y]);
    swap(c[A], c[B]);
    //cout << t[1].f << ' ' << t[1].s << "\n";
    if(a[x] < a[x - 1]) update(1, 1, n * m, a[x] + 1, a[x - 1], 1);
    if(a[x] > a[x + 1]) update(1, 1, n * m, a[x + 1] + 1, a[x], 1);
    if(a[y] < a[y - 1] && y != x + 1) update(1, 1, n * m, a[y] + 1, a[y - 1], 1);
    if(a[y] > a[y + 1]) update(1, 1, n * m, a[y + 1] + 1, a[y], 1);
    if(t[1].f == 1) return t[1].s;
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...