Submission #588297

# Submission time Handle Problem Language Result Execution time Memory
588297 2022-07-03T03:07:48 Z perchuts Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3687 ms 129576 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;
 
const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 1e6+100;
 
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
 
int val[maxn], seg[4*maxn], lazy[4*maxn], n, q, freq[4*maxn];
 
map<ii,int>v2;
 
//comprimir coordenadas de todos os valores (iniciais e alteracoes nas queries)
//necessario pra ordenar: max(0,posicao atual - posicao ordenada)
//pra achar posicao ordenada: seg de frequencia com compressao
//update: vou trocar x por y
//se x < y, todo mundo entre compressao(x) e compressao(y) diminui em 1.
//se x > y, todo mundo entre compressao(x) e compressao(y) aumenta em 1.
//se x=y, fodase
 
void updatef(int i,int l,int r,int x,int d){
    if(l>x||r<x)return;
    if(l==r){
        freq[i] = d;
        return;
    }
    int md = (l+r)/2;
    updatef(2*i,l,md,x,d), updatef(2*i+1,md+1,r,x,d);
    freq[i] = freq[2*i] + freq[2*i+1];
}
 
int query(int i,int l,int r,int x){
    if(!x)return 0;
    if(l==r)return freq[i];
    int md = (l+r)/2;
    if(md<x)return freq[2*i] + query(2*i+1,md+1,r,x);
    return query(2*i,l,md,x);
}
 
void push(int i,int l,int r){
    seg[i] += lazy[i];
    if(l!=r)lazy[2*i] += lazy[i], lazy[2*i+1] += lazy[i];
    lazy[i] = 0;
}
 
void adiciona(int i,int l,int r,int x,int d){
    push(i,l,r);
    if(l>x||r<x)return;
    if(l==r){
        seg[i] = d;
        return;
    } 
    int md = (l+r)/2;
    adiciona(2*i,l,md,x,d), adiciona(2*i+1,md+1,r,x,d);
    seg[i] = max(seg[2*i],seg[2*i+1]);
}
 
void update(int i,int l,int r,int x,int y,int d){
    push(i,l,r);
    if(l>y||r<x)return;
    if(x<=l&&r<=y){
        lazy[i] += d;
        push(i,l,r);
        return;
    }
    int md = (l+r)/2;
    update(2*i,l,md,x,y,d), update(2*i+1,md+1,r,x,y,d);
    seg[i] = max(seg[2*i],seg[2*i+1]);
}
 
 
vector<int> countScans(vector<int> a,vector<int> x,vector<int> v){
	q=sz(x), n=sz(a);
 
    vector<int>answers;
 
    for(int i=1;i<=4*(n+q);++i)seg[i] = -6e5, freq[i] = 0;
 
    for(int i=0;i<n;++i){
        v2[{a[i],i}] = 0;
    }
 
    for(int i=0;i<q;++i){
        v2[{v[i],x[i]}] = 0;
    }

    int qnt = 1;

    for(auto& [k,l]:v2)l = qnt++;
 
    for(int i=0;i<n;++i){
        a[i] = v2[{a[i],i}];
    }

    for(int i=0;i<q;++i){
        v[i] = v2[{v[i],x[i]}];
    }

    for(int i=0;i<n;++i){
        updatef(1,1,qnt,a[i],1);
    }

    for(int i=0;i<n;++i){
        adiciona(1,1,qnt,a[i],max(0,i - query(1,1,qnt,a[i]-1)));
    }
 
    for(int i=0;i<q;++i){
        int atual = a[x[i]], novo = v[i];
 
        updatef(1,1,qnt,atual,0), adiciona(1,1,qnt,atual,-6e5), updatef(1,1,qnt,novo,1);
 
        update(1,1,qnt,atual+1,qnt,1);
        update(1,1,qnt,novo+1,qnt,-1);

        adiciona(1,1,qnt,novo,max(0,x[i] - query(1,1,qnt,v[i]-1)));
        push(1,1,qnt);
        answers.pb(max(0,seg[1]));
        a[x[i]] = novo;
    }
    return answers;
}
 
//5 4 3 2 5
//4 3 2 5 5
//3 2 4 5 5
 
 
// int main(){
//     int n,q;cin>>n>>q;
//     vector<int>a;
//     for(int i=1;i<=n;++i){
//         int x;cin>>x;
//         a.pb(x);
//     }
//     vector<int>x,v;
//     for(int i=0;i<q;++i){
//         int d,f;cin>>d>>f;
//         x.pb(d), v.pb(f);
//     }
//     vector<int>resp = countScans(a,x,v);
//     for(auto x:resp)cout<<x<<" ";
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 5 ms 724 KB Output is correct
6 Correct 5 ms 724 KB Output is correct
7 Correct 5 ms 724 KB Output is correct
8 Correct 5 ms 776 KB Output is correct
9 Correct 9 ms 724 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 7 ms 724 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 5 ms 724 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 5 ms 724 KB Output is correct
6 Correct 5 ms 724 KB Output is correct
7 Correct 5 ms 724 KB Output is correct
8 Correct 5 ms 776 KB Output is correct
9 Correct 9 ms 724 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 7 ms 724 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 5 ms 724 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 724 KB Output is correct
19 Correct 19 ms 1940 KB Output is correct
20 Correct 23 ms 2132 KB Output is correct
21 Correct 21 ms 2188 KB Output is correct
22 Correct 23 ms 2156 KB Output is correct
23 Correct 29 ms 2004 KB Output is correct
24 Correct 23 ms 2068 KB Output is correct
25 Correct 23 ms 2004 KB Output is correct
26 Correct 28 ms 1976 KB Output is correct
27 Correct 20 ms 1928 KB Output is correct
28 Correct 20 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 3592 KB Output is correct
2 Correct 99 ms 8044 KB Output is correct
3 Correct 217 ms 12828 KB Output is correct
4 Correct 176 ms 12792 KB Output is correct
5 Correct 169 ms 12772 KB Output is correct
6 Correct 212 ms 12700 KB Output is correct
7 Correct 166 ms 12612 KB Output is correct
8 Correct 171 ms 12712 KB Output is correct
9 Correct 219 ms 12892 KB Output is correct
10 Correct 143 ms 9548 KB Output is correct
11 Correct 136 ms 9504 KB Output is correct
12 Correct 143 ms 9456 KB Output is correct
13 Correct 142 ms 9568 KB Output is correct
14 Correct 129 ms 9548 KB Output is correct
15 Correct 132 ms 9516 KB Output is correct
16 Correct 126 ms 9532 KB Output is correct
17 Correct 132 ms 9476 KB Output is correct
18 Correct 130 ms 9572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 5 ms 724 KB Output is correct
6 Correct 5 ms 724 KB Output is correct
7 Correct 5 ms 724 KB Output is correct
8 Correct 5 ms 776 KB Output is correct
9 Correct 9 ms 724 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 7 ms 724 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 5 ms 724 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 724 KB Output is correct
19 Correct 19 ms 1940 KB Output is correct
20 Correct 23 ms 2132 KB Output is correct
21 Correct 21 ms 2188 KB Output is correct
22 Correct 23 ms 2156 KB Output is correct
23 Correct 29 ms 2004 KB Output is correct
24 Correct 23 ms 2068 KB Output is correct
25 Correct 23 ms 2004 KB Output is correct
26 Correct 28 ms 1976 KB Output is correct
27 Correct 20 ms 1928 KB Output is correct
28 Correct 20 ms 1880 KB Output is correct
29 Correct 33 ms 3592 KB Output is correct
30 Correct 99 ms 8044 KB Output is correct
31 Correct 217 ms 12828 KB Output is correct
32 Correct 176 ms 12792 KB Output is correct
33 Correct 169 ms 12772 KB Output is correct
34 Correct 212 ms 12700 KB Output is correct
35 Correct 166 ms 12612 KB Output is correct
36 Correct 171 ms 12712 KB Output is correct
37 Correct 219 ms 12892 KB Output is correct
38 Correct 143 ms 9548 KB Output is correct
39 Correct 136 ms 9504 KB Output is correct
40 Correct 143 ms 9456 KB Output is correct
41 Correct 142 ms 9568 KB Output is correct
42 Correct 129 ms 9548 KB Output is correct
43 Correct 132 ms 9516 KB Output is correct
44 Correct 126 ms 9532 KB Output is correct
45 Correct 132 ms 9476 KB Output is correct
46 Correct 130 ms 9572 KB Output is correct
47 Correct 793 ms 40620 KB Output is correct
48 Correct 3360 ms 119168 KB Output is correct
49 Correct 3561 ms 129256 KB Output is correct
50 Correct 3589 ms 129252 KB Output is correct
51 Correct 3575 ms 129384 KB Output is correct
52 Correct 3687 ms 129376 KB Output is correct
53 Correct 3669 ms 129292 KB Output is correct
54 Correct 3162 ms 129400 KB Output is correct
55 Correct 3538 ms 129436 KB Output is correct
56 Correct 3155 ms 129576 KB Output is correct
57 Correct 3369 ms 129516 KB Output is correct
58 Correct 3302 ms 129500 KB Output is correct
59 Correct 3108 ms 120464 KB Output is correct
60 Correct 3134 ms 120244 KB Output is correct
61 Correct 3082 ms 120240 KB Output is correct
62 Correct 3012 ms 116180 KB Output is correct
63 Correct 2923 ms 116224 KB Output is correct
64 Correct 3008 ms 116276 KB Output is correct
65 Correct 2952 ms 112180 KB Output is correct
66 Correct 2906 ms 112144 KB Output is correct
67 Correct 2962 ms 112148 KB Output is correct