답안 #343765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343765 2021-01-04T13:08:51 Z AmineWeslati 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
100 / 100
8371 ms 13332 KB
//Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 150000+100;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}

#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

#ifndef LOCAL
#include "elephants.h"
#endif

int N,K,C,L; 
vi X(MX),B(MX),vec[500],J(MX),T(MX);

void process(int b){
    int j=sz(vec[b]);
    ROF(i,0,sz(vec[b])){
        int lim=X[vec[b][i]]+L;
        while(j && X[vec[b][j-1]]>lim) j--;

        J[vec[b][i]]=1; 
        if(j!=sz(vec[b])) J[vec[b][i]]=J[vec[b][j]]+1;

        T[vec[b][i]]=X[vec[b][i]]+L;
        if(j!=sz(vec[b])) T[vec[b][i]]=T[vec[b][j]];
    }
}

//last bucket non full!!!!!!!!!!!!!!!!!!!!
void init(int N, int L, int P[]){
	::N=N; ::L=L; 
    C=ceil(sqrt(N)); if(C==1) C=N;
    FOR(i,0,N) X[i]=P[i];

    int cur_b=-1;
    K=0;
    FOR(i,0,N){
        if(i%C==0) cur_b++;
        vec[cur_b].pb(i);
        B[i]=cur_b;
    }
    K=cur_b+1;

    /*FOR(i,0,K){
        for(auto x: vec[i]) cout << x << ' '; 
        cout << endl;
    }*/

    FOR(i,0,K) process(i);
    //FOR(i,0,N) cout << J[i] << ' ' << T[i] << endl;
}

int solve(){
    int cur_b=0,i=0,ans=0;
    while(1){
        ans+=J[vec[cur_b][i]];
        int lim=T[vec[cur_b][i]];
        cur_b++;
        while(cur_b<=K-1 && !(lim<X[vec[cur_b].back()])){
            cur_b++;
        }
        if(cur_b==K) break;
        int nxt;
        int l=0,r=sz(vec[cur_b])-1;
        while(l<=r){
            int m=(l+r)/2;
            if(X[vec[cur_b][m]]>lim){
                nxt=m;
                r=m-1;
            }
            else l=m+1;
        }
        i=nxt;
    }
    return ans;
}

    
int cnt_upd=0;
int update(int p, int x){
    X[p]=x;
    if(N==1) return 1;

    //replacing in the new bucket
	FOR(i,0,sz(vec[B[p]])) if(vec[B[p]][i]==p){
        vec[B[p]].erase(vec[B[p]].begin()+i);
        process(B[p]);  
        break;
    }

    FOR(i,0,K) if(!vec[i].empty()){
        bool put=false;

        if(x<=X[vec[i][0]] && (!i || x>=X[vec[i-1].back()])){
            vec[i].insert(vec[i].begin(),p);
            put=true;
        }
        else if(x>=X[vec[i].back()] && (i==K-1 || x<=X[vec[i+1][0]])){
            vec[i].insert(vec[i].end(),p);
            put=true;
        }
        else if(x>=X[vec[i][0]] && x<X[vec[i].back()]){
            FOR(j,0,sz(vec[i])-1) 
                if(x>=X[vec[i][j]] && x<=X[vec[i][j+1]]){
                    vec[i].insert(vec[i].begin()+j+1,p);
                    put=true;
                    break;
                }
        }

        if(put){
            B[p]=i;
            process(i);
            break;
        }
    }

    /*FOR(i,0,K){
        for(auto x: vec[i]) cout << x << ' '; 
        cout << endl;
    }
    FOR(i,0,N) cout << J[i] << ' ' << T[i] << endl;*/


    cnt_upd++;
    //rebuilding
    if(cnt_upd%(C-1)==0){
        vi order; FOR(i,0,K) for(auto x: vec[i]) order.pb(x);
        int cur_b=-1;
        FOR(i,0,K) vec[i].clear();
        FOR(i,0,N){
            if(i%C==0) cur_b++;
            vec[cur_b].pb(order[i]);
            B[order[i]]=cur_b;
        }
        FOR(i,0,K) process(i);
    }
    return solve();

}

#ifdef LOCAL
int main() {
    boost; IO();

    int N,L; cin>>N>>L;
    int X[N]; FOR(i,0,N) cin>>X[i];
    init(N,L,X);
    int Q; cin>>Q;
    while(Q--){
        int i,x; cin>>i>>x;
        cout << update(i,x) << endl;
    }
    /*cout << update(2,16) << endl;
    //cout << endl;
    cout << update(1,25) << endl;
    //cout << endl;
    cout << update(3,35) << endl;
    //cout << endl;
    cout << update(0,38) << endl;
    //cout << endl;
    cout << update(2,0) << endl;
    //cout << endl;*/
    

    return 0;
}
#endif


/* Careful!!!
    .Array bounds
    .Infinite loops
    .Uninitialized variables / empty containers
    .Multisets are shit

   Some insights:
    .Binary search
    .Graph representation
    .Write brute force code
    .Change your approach
*/

Compilation message

elephants.cpp: In function 'int solve()':
elephants.cpp:113:28: warning: 'nxt' may be used uninitialized in this function [-Wmaybe-uninitialized]
  113 |         ans+=J[vec[cur_b][i]];
      |                            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 546 ms 4460 KB Output is correct
8 Correct 681 ms 4920 KB Output is correct
9 Correct 950 ms 5904 KB Output is correct
10 Correct 833 ms 5700 KB Output is correct
11 Correct 729 ms 5824 KB Output is correct
12 Correct 1296 ms 6008 KB Output is correct
13 Correct 779 ms 5540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 546 ms 4460 KB Output is correct
8 Correct 681 ms 4920 KB Output is correct
9 Correct 950 ms 5904 KB Output is correct
10 Correct 833 ms 5700 KB Output is correct
11 Correct 729 ms 5824 KB Output is correct
12 Correct 1296 ms 6008 KB Output is correct
13 Correct 779 ms 5540 KB Output is correct
14 Correct 684 ms 5864 KB Output is correct
15 Correct 1136 ms 6020 KB Output is correct
16 Correct 2004 ms 6432 KB Output is correct
17 Correct 2374 ms 7444 KB Output is correct
18 Correct 2461 ms 7364 KB Output is correct
19 Correct 1710 ms 7772 KB Output is correct
20 Correct 2382 ms 7480 KB Output is correct
21 Correct 2305 ms 7388 KB Output is correct
22 Correct 1405 ms 6916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 546 ms 4460 KB Output is correct
8 Correct 681 ms 4920 KB Output is correct
9 Correct 950 ms 5904 KB Output is correct
10 Correct 833 ms 5700 KB Output is correct
11 Correct 729 ms 5824 KB Output is correct
12 Correct 1296 ms 6008 KB Output is correct
13 Correct 779 ms 5540 KB Output is correct
14 Correct 684 ms 5864 KB Output is correct
15 Correct 1136 ms 6020 KB Output is correct
16 Correct 2004 ms 6432 KB Output is correct
17 Correct 2374 ms 7444 KB Output is correct
18 Correct 2461 ms 7364 KB Output is correct
19 Correct 1710 ms 7772 KB Output is correct
20 Correct 2382 ms 7480 KB Output is correct
21 Correct 2305 ms 7388 KB Output is correct
22 Correct 1405 ms 6916 KB Output is correct
23 Correct 6562 ms 12984 KB Output is correct
24 Correct 6841 ms 12940 KB Output is correct
25 Correct 5843 ms 12912 KB Output is correct
26 Correct 6299 ms 12844 KB Output is correct
27 Correct 6567 ms 13004 KB Output is correct
28 Correct 985 ms 7660 KB Output is correct
29 Correct 913 ms 7660 KB Output is correct
30 Correct 1000 ms 7660 KB Output is correct
31 Correct 909 ms 7660 KB Output is correct
32 Correct 4720 ms 12212 KB Output is correct
33 Correct 4234 ms 11592 KB Output is correct
34 Correct 5402 ms 12200 KB Output is correct
35 Correct 4297 ms 13332 KB Output is correct
36 Correct 3557 ms 12036 KB Output is correct
37 Correct 7502 ms 13300 KB Output is correct
38 Correct 5439 ms 11376 KB Output is correct
39 Correct 5727 ms 12464 KB Output is correct
40 Correct 5184 ms 11640 KB Output is correct
41 Correct 8325 ms 12276 KB Output is correct
42 Correct 8371 ms 12620 KB Output is correct