Submission #343775

# Submission time Handle Problem Language Result Execution time Memory
343775 2021-01-04T13:19:29 Z AmineWeslati Dancing Elephants (IOI11_elephants) C++14
100 / 100
8380 ms 8424 KB
//Never stop trying
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#endif
#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]];
    }
}

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) process(i);
}

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;
        }
    }

    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;
    }
    
    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:4: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    4 | #pragma GCC optimization ("O3")
      | 
elephants.cpp:5: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("unroll-loops")
      | 
elephants.cpp: In function 'int solve()':
elephants.cpp:107:28: warning: 'nxt' may be used uninitialized in this function [-Wmaybe-uninitialized]
  107 |         ans+=J[vec[cur_b][i]];
      |                            ^
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory 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 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 544 ms 3692 KB Output is correct
8 Correct 661 ms 3896 KB Output is correct
9 Correct 979 ms 4340 KB Output is correct
10 Correct 828 ms 4320 KB Output is correct
11 Correct 756 ms 4236 KB Output is correct
12 Correct 1276 ms 4248 KB Output is correct
13 Correct 805 ms 4236 KB Output is correct
# Verdict Execution time Memory 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 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 544 ms 3692 KB Output is correct
8 Correct 661 ms 3896 KB Output is correct
9 Correct 979 ms 4340 KB Output is correct
10 Correct 828 ms 4320 KB Output is correct
11 Correct 756 ms 4236 KB Output is correct
12 Correct 1276 ms 4248 KB Output is correct
13 Correct 805 ms 4236 KB Output is correct
14 Correct 676 ms 4192 KB Output is correct
15 Correct 1118 ms 4080 KB Output is correct
16 Correct 1993 ms 4672 KB Output is correct
17 Correct 2420 ms 5208 KB Output is correct
18 Correct 2555 ms 5360 KB Output is correct
19 Correct 1697 ms 5384 KB Output is correct
20 Correct 2362 ms 5360 KB Output is correct
21 Correct 2312 ms 5372 KB Output is correct
22 Correct 1427 ms 5364 KB Output is correct
# Verdict Execution time Memory 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 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 544 ms 3692 KB Output is correct
8 Correct 661 ms 3896 KB Output is correct
9 Correct 979 ms 4340 KB Output is correct
10 Correct 828 ms 4320 KB Output is correct
11 Correct 756 ms 4236 KB Output is correct
12 Correct 1276 ms 4248 KB Output is correct
13 Correct 805 ms 4236 KB Output is correct
14 Correct 676 ms 4192 KB Output is correct
15 Correct 1118 ms 4080 KB Output is correct
16 Correct 1993 ms 4672 KB Output is correct
17 Correct 2420 ms 5208 KB Output is correct
18 Correct 2555 ms 5360 KB Output is correct
19 Correct 1697 ms 5384 KB Output is correct
20 Correct 2362 ms 5360 KB Output is correct
21 Correct 2312 ms 5372 KB Output is correct
22 Correct 1427 ms 5364 KB Output is correct
23 Correct 6608 ms 7860 KB Output is correct
24 Correct 6898 ms 7836 KB Output is correct
25 Correct 5923 ms 7952 KB Output is correct
26 Correct 6380 ms 7704 KB Output is correct
27 Correct 6642 ms 8072 KB Output is correct
28 Correct 995 ms 4716 KB Output is correct
29 Correct 924 ms 4648 KB Output is correct
30 Correct 982 ms 4716 KB Output is correct
31 Correct 909 ms 4716 KB Output is correct
32 Correct 4872 ms 7824 KB Output is correct
33 Correct 4235 ms 7736 KB Output is correct
34 Correct 5586 ms 8008 KB Output is correct
35 Correct 4284 ms 8372 KB Output is correct
36 Correct 3574 ms 7732 KB Output is correct
37 Correct 7672 ms 8424 KB Output is correct
38 Correct 5638 ms 7816 KB Output is correct
39 Correct 5861 ms 7952 KB Output is correct
40 Correct 5677 ms 7916 KB Output is correct
41 Correct 8286 ms 7888 KB Output is correct
42 Correct 8380 ms 7876 KB Output is correct