Submission #531474

# Submission time Handle Problem Language Result Execution time Memory
531474 2022-02-28T19:10:29 Z cadmiumsky Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
4541 ms 114096 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;

const int nmax = 1e6 + 3; // e memorie putina, nu merge +5

int len;

namespace AINT {
  int lazy[4 * nmax];
  int maxx[4 * nmax];
  namespace AIB {
    #define lsb(x) (x & (-x))
    int active[nmax];
    int sum[nmax];
    void upd(int x, int val) {
      while(x <= len)
        sum[x] += val, x += lsb(x);
    }
    int query(int x) {
      int s = 0;
      while(x > 0)
        s += sum[x], x -= lsb(x); 
      return s;
    }
    void toggle(int poz) {
      upd(poz, (active[poz] ^ 1) - active[poz]);
      active[poz] ^= 1;
    }
  }
  void apply(int node, int cl, int cr) {
    maxx[node] += lazy[node];
    int mid = cl + cr >> 1;
    if(cl != cr)
      lazy[2 * node] += lazy[node], lazy [2 * node + 1] += lazy[node];
    lazy[node] = 0;
  }
  void prop(int node, int cl, int cr) {
    apply(node, cl, cr);
    int mid = cl + cr >> 1;
    if(cl != cr) {
      apply(2 * node, cl, mid);
      apply(2 * node + 1, mid + 1, cr);
    }
    return;
  }
  void push(int poz, int val, int node = 1, int cl = 1, int cr = len) {
    //cerr << poz << ' ' << cl << ' ' << cr << '\n';
    prop(node, cl, cr);
    if(cl == cr) {
      maxx[node] = val;
      return;
    }
    int mid = cl + cr >> 1;
    if(poz <= mid)
      push(poz, val, 2 * node, cl, mid);
    else
      push(poz, val, 2 * node + 1, mid + 1, cr);
    maxx[node] = max(maxx[2 * node], maxx[2 * node + 1]);
    return;
  }
  void add(int l, int r, int val = 1, int node = 1, int cl = 1, int cr = len) {
    prop(node, cl, cr);
    #warning incearca cu apply simplu
    if(r < cl || cr < l)
      return;
    if(l <= cl && cr <= r) {
      lazy[node] += val;
      prop(node, cl, cr);
      return;
    }
    int mid = cl + cr >> 1;
    add(l, r, val, 2 * node, cl, mid);
    add(l, r, val, 2 * node + 1, mid + 1, cr);
    maxx[node] = max(maxx[2 * node], maxx[2 * node + 1]);
  }
  void erase(int poz) {
    AIB::toggle(poz);
    push(poz, - 2e6 - 5);
    add(poz + 1, len);
  }
  void insert(int poz, int val) {
    push(poz, val - AIB::query(poz));
    add(poz + 1, len, -1);
    AIB::toggle(poz);
  }
  void construct(int node = 1, int cl = 1, int cr = len) {
    maxx[node] = -2e6 - 5;
    if(cl == cr)
      return;
    int mid = cl + cr >> 1;
    construct(2 * node, cl, mid);
    construct(2 * node + 1, mid + 1, cr);
    return;
  }
}
namespace NORM {
  map<pair<int,int>, int> norm;
  void push(int v, int poz) {
    norm[{v, poz}];
  }
  void init() {
    int cnt = 1;
    for(auto &x : norm) 
      x.second = cnt++; 
    len = norm.size() + 1;
  }
  int query(int v, int poz) {
    return norm[{v, poz}];
  }
};

vector<int> countScans(vector<int> a, vector<int> x, vector<int> y){
  int n = a.size(), q = x.size();
  vector<int> ans(q);
  for(int i = 0; i < n; i++)
    NORM::push(a[i], i);
  for(int i = 0; i < q; i++)
    NORM::push(y[i], x[i]);
  NORM::init();
  AINT::construct();
  for(int i = 0; i < n; i++)
    AINT::insert(NORM::query(a[i], i), i);
  for(int i = 0; i < q; i++) {
    AINT::erase(NORM::query(a[x[i]], x[i]));
    AINT::insert(NORM::query(y[i], x[i]), x[i]);
    a[x[i]] = y[i];
    ans[i] = AINT::maxx[1];
  }
	return ans;
}

Compilation message

bubblesort2.cpp:64:6: warning: #warning incearca cu apply simplu [-Wcpp]
   64 |     #warning incearca cu apply simplu
      |      ^~~~~~~
bubblesort2.cpp: In function 'void AINT::apply(int, int, int)':
bubblesort2.cpp:33:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
bubblesort2.cpp:33:9: warning: unused variable 'mid' [-Wunused-variable]
   33 |     int mid = cl + cr >> 1;
      |         ^~~
bubblesort2.cpp: In function 'void AINT::prop(int, int, int)':
bubblesort2.cpp:40:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
bubblesort2.cpp: In function 'void AINT::push(int, int, int, int, int)':
bubblesort2.cpp:54:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
bubblesort2.cpp: In function 'void AINT::add(int, int, int, int, int, int)':
bubblesort2.cpp:72:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
bubblesort2.cpp: In function 'void AINT::construct(int, int, int)':
bubblesort2.cpp:91:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   91 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 436 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
3 Correct 6 ms 688 KB Output is correct
4 Correct 7 ms 724 KB Output is correct
5 Correct 6 ms 692 KB Output is correct
6 Correct 6 ms 688 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 8 ms 716 KB Output is correct
9 Correct 8 ms 736 KB Output is correct
10 Correct 7 ms 716 KB Output is correct
11 Correct 7 ms 696 KB Output is correct
12 Correct 6 ms 728 KB Output is correct
13 Correct 8 ms 740 KB Output is correct
14 Correct 8 ms 716 KB Output is correct
15 Correct 8 ms 696 KB Output is correct
16 Correct 6 ms 588 KB Output is correct
17 Correct 5 ms 588 KB Output is correct
18 Correct 5 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 436 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
3 Correct 6 ms 688 KB Output is correct
4 Correct 7 ms 724 KB Output is correct
5 Correct 6 ms 692 KB Output is correct
6 Correct 6 ms 688 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 8 ms 716 KB Output is correct
9 Correct 8 ms 736 KB Output is correct
10 Correct 7 ms 716 KB Output is correct
11 Correct 7 ms 696 KB Output is correct
12 Correct 6 ms 728 KB Output is correct
13 Correct 8 ms 740 KB Output is correct
14 Correct 8 ms 716 KB Output is correct
15 Correct 8 ms 696 KB Output is correct
16 Correct 6 ms 588 KB Output is correct
17 Correct 5 ms 588 KB Output is correct
18 Correct 5 ms 588 KB Output is correct
19 Correct 28 ms 1912 KB Output is correct
20 Correct 27 ms 2140 KB Output is correct
21 Correct 32 ms 2096 KB Output is correct
22 Correct 32 ms 2100 KB Output is correct
23 Correct 28 ms 1952 KB Output is correct
24 Correct 26 ms 1940 KB Output is correct
25 Correct 26 ms 1860 KB Output is correct
26 Correct 24 ms 1864 KB Output is correct
27 Correct 23 ms 1792 KB Output is correct
28 Correct 26 ms 1720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3248 KB Output is correct
2 Correct 140 ms 7144 KB Output is correct
3 Correct 244 ms 11304 KB Output is correct
4 Correct 253 ms 11332 KB Output is correct
5 Correct 274 ms 11168 KB Output is correct
6 Correct 257 ms 11236 KB Output is correct
7 Correct 293 ms 11240 KB Output is correct
8 Correct 227 ms 11140 KB Output is correct
9 Correct 273 ms 11112 KB Output is correct
10 Correct 186 ms 7084 KB Output is correct
11 Correct 180 ms 7184 KB Output is correct
12 Correct 242 ms 7188 KB Output is correct
13 Correct 158 ms 7212 KB Output is correct
14 Correct 180 ms 7116 KB Output is correct
15 Correct 163 ms 7204 KB Output is correct
16 Correct 152 ms 7140 KB Output is correct
17 Correct 191 ms 7144 KB Output is correct
18 Correct 162 ms 7208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 436 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
3 Correct 6 ms 688 KB Output is correct
4 Correct 7 ms 724 KB Output is correct
5 Correct 6 ms 692 KB Output is correct
6 Correct 6 ms 688 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 8 ms 716 KB Output is correct
9 Correct 8 ms 736 KB Output is correct
10 Correct 7 ms 716 KB Output is correct
11 Correct 7 ms 696 KB Output is correct
12 Correct 6 ms 728 KB Output is correct
13 Correct 8 ms 740 KB Output is correct
14 Correct 8 ms 716 KB Output is correct
15 Correct 8 ms 696 KB Output is correct
16 Correct 6 ms 588 KB Output is correct
17 Correct 5 ms 588 KB Output is correct
18 Correct 5 ms 588 KB Output is correct
19 Correct 28 ms 1912 KB Output is correct
20 Correct 27 ms 2140 KB Output is correct
21 Correct 32 ms 2096 KB Output is correct
22 Correct 32 ms 2100 KB Output is correct
23 Correct 28 ms 1952 KB Output is correct
24 Correct 26 ms 1940 KB Output is correct
25 Correct 26 ms 1860 KB Output is correct
26 Correct 24 ms 1864 KB Output is correct
27 Correct 23 ms 1792 KB Output is correct
28 Correct 26 ms 1720 KB Output is correct
29 Correct 43 ms 3248 KB Output is correct
30 Correct 140 ms 7144 KB Output is correct
31 Correct 244 ms 11304 KB Output is correct
32 Correct 253 ms 11332 KB Output is correct
33 Correct 274 ms 11168 KB Output is correct
34 Correct 257 ms 11236 KB Output is correct
35 Correct 293 ms 11240 KB Output is correct
36 Correct 227 ms 11140 KB Output is correct
37 Correct 273 ms 11112 KB Output is correct
38 Correct 186 ms 7084 KB Output is correct
39 Correct 180 ms 7184 KB Output is correct
40 Correct 242 ms 7188 KB Output is correct
41 Correct 158 ms 7212 KB Output is correct
42 Correct 180 ms 7116 KB Output is correct
43 Correct 163 ms 7204 KB Output is correct
44 Correct 152 ms 7140 KB Output is correct
45 Correct 191 ms 7144 KB Output is correct
46 Correct 162 ms 7208 KB Output is correct
47 Correct 1173 ms 37196 KB Output is correct
48 Correct 4334 ms 105756 KB Output is correct
49 Correct 4534 ms 113776 KB Output is correct
50 Correct 4494 ms 113772 KB Output is correct
51 Correct 4473 ms 113768 KB Output is correct
52 Correct 4541 ms 113772 KB Output is correct
53 Correct 4431 ms 113768 KB Output is correct
54 Correct 4159 ms 114096 KB Output is correct
55 Correct 4413 ms 114080 KB Output is correct
56 Correct 4153 ms 114036 KB Output is correct
57 Correct 4334 ms 113964 KB Output is correct
58 Correct 4097 ms 113944 KB Output is correct
59 Correct 3615 ms 103808 KB Output is correct
60 Correct 3730 ms 103820 KB Output is correct
61 Correct 3764 ms 103940 KB Output is correct
62 Correct 3477 ms 99136 KB Output is correct
63 Correct 3517 ms 99292 KB Output is correct
64 Correct 3594 ms 99256 KB Output is correct
65 Correct 3406 ms 94784 KB Output is correct
66 Correct 3406 ms 94708 KB Output is correct
67 Correct 3480 ms 94716 KB Output is correct