답안 #433481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433481 2021-06-19T21:16:25 Z someone 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
50 / 100
1213 ms 4828 KB
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

struct Val {
    int i, val, nb, exces;

    bool operator <(const Val& other) const {
        return val < other.val;
    }
};

const int T = 1042, N = 150 * 1000 + 10, NB_BLOCS = 1000, INF = 1e9;

Val pos[N];
set<Val> iBloc;
int n, len, position[N];
vector<Val> bloc[NB_BLOCS];

void recalcul(int i) {
    int t = bloc[i].size();
    for(int j = t-1, k = t; j > -1; j--) {
        while(k > 0 && bloc[i][j].val + len < bloc[i][k-1].val)
            k--;
        if(k == t) {
            bloc[i][j].nb = 1;
            bloc[i][j].exces = bloc[i][j].val + len;
        } else {
            bloc[i][j].nb = bloc[i][k].nb + 1;
            bloc[i][j].exces = bloc[i][k].exces;
        }
    }
}

void initialise() {
    //cout << "INITIALIZE\n";
    iBloc.clear();
    sort(pos, pos + n);
    for(int i = 0; i < n; i++)
        position[pos[i].i] = i;
    for(int i = 0, j = 0; i < n; i += T, j++) {
        if(i + T >= n) {
            iBloc.insert({j, INF, 0, 0});
            //cout << j << '\n';
        } else
            iBloc.insert({j, pos[i + T].val-1, 0, 0});
        bloc[j].clear();
        for(int k = i; k < min(i + T, n); k++)
            bloc[j].push_back({k, pos[k].val, 0, 0});
        recalcul(j);
    }
}

void del(int i, int toDel) {
    int t = bloc[i].size();
    for(int j = 1; j < t; j++)
        if(bloc[i][j-1].i == toDel)
            swap(bloc[i][j-1], bloc[i][j]);
    bloc[i].pop_back();
    recalcul(i);
}

void add(int i, int toAdd) {
    bloc[i].push_back({toAdd, pos[toAdd].val, 0, 0});
    int t = bloc[i].size();
    for(int j = t-1; j > 0; j--)
        if(bloc[i][j].val < bloc[i][j-1].val)
            swap(bloc[i][j], bloc[i][j-1]);
        else
            j = 0;
    recalcul(i);
}

int dicho(int i, int deb, int fin, int obj) {
    if(deb + 1 == fin)
        return deb;
    if(deb+2 == fin) {
        if(bloc[i][deb].val > obj)
            return deb;
        return deb+1;
    }
    int med = (deb + fin) / 2;
    if(bloc[i][med-1].val <= obj)
        return dicho(i, med, fin, obj);
    return dicho(i, deb, med, obj);
}

int update(int i, int y) {
    /*for(Val v : iBloc)
        cout << v.val << ' ';
    cout << '\n';*/
    auto it = iBloc.lower_bound({0, pos[position[i]].val, 0, 0});
    //cout << (*it).i << '\n';

    del((*it).i, position[i]);
    pos[position[i]].val = y;
    it = iBloc.lower_bound({0, pos[position[i]].val, 0, 0});
    //cout << (*it).i << '\n';

    add((*it).i, position[i]);
    if(bloc[(*it).i].size() == 2*T)
        initialise();

    //cout << i << ' ' << y << '\n';
    int nb = 0, pre = -1, t = iBloc.size();
    for(int j = 0; j < t; j++) {
        int sz = bloc[j].size();
        //cout << pre << ' ' << bloc[j].back().val << ' ';
        if(bloc[j].size() != 0 && pre < bloc[j].back().val) {
            int id = dicho(j, 0, sz, pre);
            //cout << id << ' ' << bloc[j][id].val << ' ' << bloc[j][id+1].val << ' ';
            nb += bloc[j][id].nb;
            pre = bloc[j][id].exces;
        }
        //cout << nb << '\n';
    }
    //cout << '\n';
    return nb;
}

void init(int n1, int L, int X[]) {
    n = n1, len = L;
    for(int i = 0; i < n; i++)
        pos[i].val = X[i];
    for(int i = 0; i < n; i++)
        pos[i].i = position[i] = i;
    initialise();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1080 ms 1524 KB Output is correct
8 Correct 1071 ms 1712 KB Output is correct
9 Correct 732 ms 3120 KB Output is correct
10 Correct 863 ms 3160 KB Output is correct
11 Correct 857 ms 4448 KB Output is correct
12 Correct 1213 ms 4828 KB Output is correct
13 Correct 841 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1080 ms 1524 KB Output is correct
8 Correct 1071 ms 1712 KB Output is correct
9 Correct 732 ms 3120 KB Output is correct
10 Correct 863 ms 3160 KB Output is correct
11 Correct 857 ms 4448 KB Output is correct
12 Correct 1213 ms 4828 KB Output is correct
13 Correct 841 ms 4300 KB Output is correct
14 Incorrect 69 ms 3468 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1080 ms 1524 KB Output is correct
8 Correct 1071 ms 1712 KB Output is correct
9 Correct 732 ms 3120 KB Output is correct
10 Correct 863 ms 3160 KB Output is correct
11 Correct 857 ms 4448 KB Output is correct
12 Correct 1213 ms 4828 KB Output is correct
13 Correct 841 ms 4300 KB Output is correct
14 Incorrect 69 ms 3468 KB Output isn't correct
15 Halted 0 ms 0 KB -