Submission #1113516

# Submission time Handle Problem Language Result Execution time Memory
1113516 2024-11-16T17:21:12 Z lucascgar Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
252 ms 23760 KB
#include <bits/stdc++.h>

using namespace std;

/*
foco no cara da esquerda ativado ou nn

query so na esquerda: desligo
na direita: mudo

query so na esquerda:
    desliga esquerda
    nn mexe em direita

depois de query na esquerda ele sempre vai ficar com direita ativa
olhar ultima query na esquerda
depois só importam queries na direita
*/

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;

const int MAXN=2e5+10, MAXTT = 2*MAXN, BIG = 1e9+10;

pii c[MAXN];
bool ac[MAXN];

int seg[4*MAXN], lg[MAXN];

void build(int id, int il, int ir, vector<pii> &op){
    if (il==ir){
        seg[id]=op[il-1].second;
        return;
    }
    int m = (il+ir)/2;
    build(2*id, il, m, op);
    build(2*id+1, m+1, ir, op);
    seg[id] = max(seg[2*id], seg[2*id+1]);

}

int qry(int id, int il, int ir, int l, int r){
    if (il>r || ir<l) return -BIG;
    if (il>=l && ir<=r) return seg[id];

    int m = (il+ir)/2;
    return max(qry(2*id, il, m, l, r), qry(2*id+1, m+1, ir, l, r));
}

pii lst[MAXN];


int bit[MAXTT];

int bqry(int p){
    int ss = 0;
    while (p>0){
        ss += bit[p];
        p -= p&-p;
    }
    return ss;
}

void bup(int p, int v, int sz){
    while (p<=sz){
        bit[p]+=v;
        p += p&-p;
    }
}

int p[MAXN];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    int a, b;
    vector<pair<int, pii>> comp;
    for (int i=0;i<n;i++){
        cin >> a >> b;
        if (a<b) ac[i]=1;
        else if(a>b) swap(a,b);
        c[i] = {a,b}, lg[i]=b;
        comp.push_back({b,{i, 0}});
    }

    vector<pii> op(k);
    for (int i=0;i<k;i++){
        cin >> p[i+1];
        op[i] = {p[i+1], i+1};
        comp.push_back({p[i+1], {i+1, 1}});
    }

    sort(op.begin(), op.end());

    build(1, 1, k, op);

    for (int i=0;i<n;i++){
        int l, r;
        pii srch = {c[i].second, -1};
        r = lower_bound(op.begin(), op.end(), srch) - op.begin(), srch = {c[i].first, -1}, l = lower_bound(op.begin(), op.end(), srch) - op.begin() + 1;

        if (l<=r) lst[i].first = qry(1, 1, k, l, r);
        lst[i].second = i;
    }

    sort(lst,lst+n);  // ordena por ultimo que importa

    // comprime qries com caras da direita
    sort(comp.begin(), comp.end());
    int cr = 1;
    for (int i=0;i<comp.size();i++){
        if (i>0 && comp[i].first != comp[i-1].first) cr++;
    
        if (comp[i].second.second) p[comp[i].second.first] = cr;
        else lg[comp[i].second.first] = cr;
    }

    int j = n-1;  // primeiro q nn pega

    for (int i=k;i>0&&j>=0;i--){
        // atualiza
        bup(cr-p[i]+1, 1, cr+1);

        while (j>=0 && lst[j].first == i){
            int am = bqry(cr-lg[lst[j].second]+1);
            // cerr << lst[j].second << ": " << am << " so " << ac[lst[j].second] << '\n';
            ac[lst[j].second] = (am%2 == 1);
            j--;
        }
    }
    while (j>=0){  // quem só pega no direito
        int am = bqry(cr-lg[lst[j].second]+1);
        ac[lst[j].second] ^= (am%2 == 1);
        // cerr << lst[j].second << ": " << am << " so " << ac[lst[j].second] << '\n';
        j--;
    }
    long long ss = 0;
    for (int i=0;i<n;i++){
        if (ac[i]) ss += c[i].first;
        else ss += c[i].second;
    }

    cout << ss << '\n';

    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:115:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for (int i=0;i<comp.size();i++){
      |                  ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8696 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8756 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 3 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 3 ms 8732 KB Output is correct
11 Correct 3 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8696 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8756 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 3 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 3 ms 8732 KB Output is correct
11 Correct 3 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 10 ms 9292 KB Output is correct
15 Correct 21 ms 9892 KB Output is correct
16 Correct 31 ms 10856 KB Output is correct
17 Correct 42 ms 10872 KB Output is correct
18 Correct 41 ms 10592 KB Output is correct
19 Correct 39 ms 11196 KB Output is correct
20 Correct 40 ms 9920 KB Output is correct
21 Correct 39 ms 10684 KB Output is correct
22 Correct 29 ms 10436 KB Output is correct
23 Correct 29 ms 10692 KB Output is correct
24 Correct 31 ms 10684 KB Output is correct
25 Correct 28 ms 10692 KB Output is correct
26 Correct 32 ms 10684 KB Output is correct
27 Correct 36 ms 11260 KB Output is correct
28 Correct 34 ms 11196 KB Output is correct
29 Correct 32 ms 10684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8696 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8756 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 3 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 3 ms 8732 KB Output is correct
11 Correct 3 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 10 ms 9292 KB Output is correct
15 Correct 21 ms 9892 KB Output is correct
16 Correct 31 ms 10856 KB Output is correct
17 Correct 42 ms 10872 KB Output is correct
18 Correct 41 ms 10592 KB Output is correct
19 Correct 39 ms 11196 KB Output is correct
20 Correct 40 ms 9920 KB Output is correct
21 Correct 39 ms 10684 KB Output is correct
22 Correct 29 ms 10436 KB Output is correct
23 Correct 29 ms 10692 KB Output is correct
24 Correct 31 ms 10684 KB Output is correct
25 Correct 28 ms 10692 KB Output is correct
26 Correct 32 ms 10684 KB Output is correct
27 Correct 36 ms 11260 KB Output is correct
28 Correct 34 ms 11196 KB Output is correct
29 Correct 32 ms 10684 KB Output is correct
30 Correct 58 ms 15044 KB Output is correct
31 Correct 98 ms 16336 KB Output is correct
32 Correct 147 ms 21848 KB Output is correct
33 Correct 233 ms 23656 KB Output is correct
34 Correct 52 ms 14776 KB Output is correct
35 Correct 252 ms 23528 KB Output is correct
36 Correct 249 ms 23716 KB Output is correct
37 Correct 247 ms 23652 KB Output is correct
38 Correct 241 ms 23480 KB Output is correct
39 Correct 248 ms 23480 KB Output is correct
40 Correct 208 ms 23476 KB Output is correct
41 Correct 241 ms 23480 KB Output is correct
42 Correct 244 ms 23632 KB Output is correct
43 Correct 136 ms 22864 KB Output is correct
44 Correct 139 ms 22964 KB Output is correct
45 Correct 143 ms 22852 KB Output is correct
46 Correct 159 ms 21952 KB Output is correct
47 Correct 145 ms 21844 KB Output is correct
48 Correct 205 ms 23580 KB Output is correct
49 Correct 193 ms 23760 KB Output is correct