Submission #1090076

# Submission time Handle Problem Language Result Execution time Memory
1090076 2024-09-17T17:09:19 Z onlk97 Comparing Plants (IOI20_plants) C++14
44 / 100
361 ms 33600 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
 
int n;
pair <int,int> seg[800040];
int laz[800040],arr[200010];
void build(int id,int tl,int tr){
    if (tl==tr){
        seg[id]={arr[tl],tl};
        return;
    }
    int tm=(tl+tr)/2;
    build(2*id,tl,tm);
    build(2*id+1,tm+1,tr);
    seg[id]=min(seg[2*id],seg[2*id+1]);
}
void pushdown(int id,int tl,int tr){
    seg[2*id].first+=laz[id]; laz[2*id]+=laz[id];
    seg[2*id+1].first+=laz[id]; laz[2*id+1]+=laz[id];
    laz[id]=0;
}
void update(int id,int tl,int tr,int l,int r,int val){
    if (l>r) return;
    if (l<=tl&&tr<=r){
        seg[id].first+=val;
        laz[id]+=val;
        return;
    }
    pushdown(id,tl,tr);
    int tm=(tl+tr)/2;
    update(2*id,tl,tm,l,min(r,tm),val);
    update(2*id+1,tm+1,tr,max(l,tm+1),r,val);
    seg[id]=min(seg[2*id],seg[2*id+1]);
}
pair <int,int> query(int id,int tl,int tr,int l,int r){
    if (l>r) return {1e9,1e9};
    if (l<=tl&&tr<=r) return seg[id];
    pushdown(id,tl,tr);
    int tm=(tl+tr)/2;
    pair <int,int> lx=query(2*id,tl,tm,l,min(r,tm));
    pair <int,int> rx=query(2*id+1,tm+1,tr,max(l,tm+1),r);
    return min(lx,rx);
}
vector <int> p;
set <int> s;
set <pair <int,int>,greater <pair <int,int> > > diff;
void add_s(int u){
    if (s.empty()){
        s.insert(u);
        return;
    }
    if (s.size()==1){
        int prv=*s.begin();
        s.insert(u);
        if (prv>u) swap(prv,u);
        diff.insert({u-prv,u});
        diff.insert({prv-u+n,prv});
        return;
    }
    auto it=s.lower_bound(u);
    if (it==s.end()) it=s.begin();
    int nxt=(*it);
    if (it==s.begin()) it=s.end();
    it--;
    int prv=(*it);
    diff.erase({(nxt>prv?nxt-prv:nxt-prv+n),nxt});
    diff.insert({(u>prv?u-prv:u-prv+n),u});
    diff.insert({(nxt>u?nxt-u:nxt-u+n),nxt});
    s.insert(u);
}
void del_s(int u){
    if (s.size()<=2){
        s.erase(u);
        diff.clear();
        return;
    }
    auto it=s.lower_bound(u);
    auto it2=it;
    it2++;
    if (it2==s.end()) it2=s.begin();
    int nxt=(*it2);
    it2=it;
    if (it2==s.begin()) it2=(--s.end());
    else it2--;
    int prv=(*it2);
    diff.erase({(u>prv?u-prv:u-prv+n),u});
    diff.erase({(nxt>u?nxt-u:nxt-u+n),nxt});
    diff.insert({(nxt>prv?nxt-prv:nxt-prv+n),nxt});
    s.erase(u);
}
int get_s(){
    if (s.size()==1) return *s.begin();
    pair <int,int> tp=*diff.begin();
    return tp.second;
}
void init(int k,vector <int> r){
    n=r.size();
    r.insert(r.begin(),0);
    p.resize(n+1);
    for (int i=1; i<=n; i++) arr[i]=r[i];
    build(1,1,n);
    for (int i=n; i; i--){
        while (1){
            pair <int,int> tp=query(1,1,n,1,n);
            if (tp.first) break;
            add_s(tp.second);
            update(1,1,n,tp.second,tp.second,1e9);
        }
        int idx=get_s();
        p[idx]=i;
        if (idx>=k) update(1,1,n,idx-k+1,idx,-1);
        else {
            update(1,1,n,1,idx,-1);
            update(1,1,n,n-(k-idx)+1,n,-1);
        }
        del_s(idx);
    }
}
 
int compare_plants(int x,int y){
    x++; y++;
    if (p[x]>p[y]) return 1;
    return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2484 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 548 KB Output is correct
7 Correct 36 ms 5332 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 38 ms 7292 KB Output is correct
11 Correct 37 ms 5456 KB Output is correct
12 Correct 34 ms 5460 KB Output is correct
13 Correct 35 ms 5712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 548 KB Output is correct
7 Correct 36 ms 5332 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 38 ms 7292 KB Output is correct
11 Correct 37 ms 5456 KB Output is correct
12 Correct 34 ms 5460 KB Output is correct
13 Correct 35 ms 5712 KB Output is correct
14 Correct 54 ms 6404 KB Output is correct
15 Correct 260 ms 15564 KB Output is correct
16 Correct 52 ms 7988 KB Output is correct
17 Correct 250 ms 15940 KB Output is correct
18 Correct 341 ms 24552 KB Output is correct
19 Correct 149 ms 15636 KB Output is correct
20 Correct 228 ms 15728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 35 ms 7108 KB Output is correct
4 Correct 238 ms 21056 KB Output is correct
5 Correct 283 ms 16384 KB Output is correct
6 Correct 266 ms 15460 KB Output is correct
7 Correct 272 ms 15428 KB Output is correct
8 Correct 245 ms 15424 KB Output is correct
9 Correct 254 ms 19524 KB Output is correct
10 Correct 239 ms 16560 KB Output is correct
11 Correct 317 ms 33600 KB Output is correct
12 Correct 125 ms 14944 KB Output is correct
13 Correct 361 ms 28292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2484 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -