제출 #1269006

#제출 시각아이디문제언어결과실행 시간메모리
1269006YassirSalamaBubble Sort 2 (JOI18_bubblesort2)C++20
100 / 100
1035 ms157764 KiB
#include "bubblesort2.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(),v.end()
#define pb push_back
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif


const int maxn = 2e6+100;
vector<int> a,x,y;
set<int> s[maxn];
int tree[4*maxn];
int t2[4*maxn];

void update(int node,int l,int r,int x,int id,int cnt){
    if(l==r){
        t2[node] = cnt;
        tree[node] = id-cnt+1;
        return;
    }
    int mid = (l+r)/2;
    if(x<=mid){
        update(node<<1,l,mid,x,id,cnt);
    }else{
        update(node<<1|1,mid+1,r,x,id,cnt);
    }
    t2[node] = t2[node<<1]+t2[node<<1|1];
    tree[node] = tree[node<<1];
    tree[node] = max(tree[node],tree[node<<1|1]-t2[node<<1]);
}

int m;
void add(int x,int i){
    s[x].insert(i);
    dbg(x,i)
    if(!s[x].empty()){
        update(1,0,m-1,x,*s[x].rbegin(),s[x].size());
    }
}
void erase(int x,int i){
    s[x].erase(i);
    if(!s[x].empty()){
        update(1,0,m-1,x,*s[x].rbegin(),s[x].size());        
    }else{
        update(1,0,m-1,x,-1e9,0);
    }
}


vector<int> countScans(vector<int> A, vector<int> X,vector<int> V) {
    int n = A.size();
    vector<int> c;
    a  = A;
    x=X;
    y = V;
    for(int i = 0;i<n;i++){
        c.pb(a[i]);
    }
    int q = x.size();
    for(int j = 0;j<q;j++){
        c.pb(y[j]);
    }
    sort(all(c));
    c.erase(unique(all(c)),c.end());
    for(int i = 0;i<n;i++){
        a[i] = lower_bound(all(c),a[i])-c.begin();
    }
    for(int i = 0;i<q;i++){
        y[i] = lower_bound(all(c),y[i])-c.begin();
    }
    //i - number of elements smaller than a[i]
    m = c.size();
    for(int i = 0;i<n;i++){
        add(a[i],i);
    }
    vector<int> ans(q);
    for(int i = 0;i<q;i++){
        int j = x[i];
        int nv = y[i];
        int o = a[x[i]];
        erase(o,j);
        add(nv,j);
        a[j] = nv;
        ans[i] = tree[1];
    }
    return ans;

    
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...