Submission #945422

# Submission time Handle Problem Language Result Execution time Memory
945422 2024-03-13T18:56:06 Z tset Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
1422 ms 132296 KB
#include<bits/stdc++.h> 
using namespace std;

#define int long long

const int T = 1048576;
const int INF = 1e18;


vector<int > ab;
void upd(int nde)
{
    ab[nde] = max(ab[nde *2], ab[nde*2 +1]);
    if(nde > 1)
        upd(nde /2);
}

void setValue(int nde, int val)
{
    ab[nde+T] = val;
    upd((nde+T)/2);
}

int query(int nde, int RB, int RE, int GB, int GE)
{
    if(GB > RE || RB > GE)
        return -INF;
    if(RB >= GB && RE <=  GE)
        return ab[nde];
    int mid = (RB + RE)/2;
    return max(query(nde *2, RB, mid, GB, GE), query(nde*2 +1, mid+1, RE, GB, GE));
}

vector<int > ab2;

void upd2(int nde)
{
    ab2[nde] = ab2[nde *2] + ab2[nde*2 +1];
    if(nde > 1)
        upd2(nde /2);
}

void addRange(int nde, int RB, int RE, int GB, int GE, int val)
{
    //printf("%lld %lld %lld %lld %lld %lld \n", nde, RB, RE, GB, GE, val);
    if(GB > RE || RB > GE)
        return;
    if(RB >= GB && RE <=  GE)
    {
        ab2[nde] += val;
        return;
    }
    int mid = (RB + RE)/2;
    addRange(nde *2, RB, mid, GB, GE, val);
    addRange(nde*2 +1, mid+1, RE, GB, GE, val);
}

int getValue(int pos)
{
    int ans = 0;
    pos += T;
    while (pos >= 1)
    {
        ans += ab2[pos];
        pos/=2;
    }
    return ans;
}

signed main()
{
    int N, K;
    scanf("%lld%lld", &N, &K);

    vector<pair<int, int>> data;
    vector<int > queries(K);
    vector<int> original(1000000);
    map<int, int> compressed;
    set<int> values;
    for(int iN = 0; iN < N; iN ++)
    {
        int Ai, Bi;
        scanf("%lld %lld", &Ai, &Bi);
        data.push_back({Ai, Bi});
        values.insert(Ai);
        values.insert(Bi);
    }
    for(int iQ = 0; iQ< K; iQ++)
    {
        int Ti;
        scanf("%lld", &Ti);
        queries[iQ] = Ti;
        values.insert(Ti);
    }
    int idAct = 1;
    for(int val : values)
    {
        compressed[val] = idAct;
        original[idAct] = val; 
        idAct++; 
    }
    vector<int> rotaInit(N, 0);
    vector<pair<int, int>> inters;
    vector<pair<int, int> > whenStop;
    vector<pair<int, int>> cards(N);
    vector<int> ans(N, -INF);

    ab.assign(2*T, -INF); ab2.assign(2*T, 0);
    for(int iK = 0; iK<K; iK++)
    {
        queries[iK] = compressed[queries[iK]];
        setValue(queries[iK], iK);
    }
    for(int iN = 0; iN< N; iN++)
    {
        int deb = compressed[data[iN].first];
        int fin = compressed[data[iN].second];
        if(deb > fin)
        {
            swap(deb, fin);
            rotaInit[iN]++;
        }
        cards[iN] = {deb, fin};
        int lastMX = query(1, 0, T-1, deb, fin-1);
        if(lastMX!=-INF)
        {
            //printf("card %lld : last end = query nu. %lld\n", iN, lastMX);
            whenStop.push_back({lastMX, iN});
        }

    }

    sort(whenStop.begin(), whenStop.end(), greater<pair<int, int>>());
    int posiWS = 0;
    for(int iQ = K-1; iQ >= 0; iQ--)
    {
        while (posiWS< whenStop.size() && whenStop[posiWS].first == iQ)
        {
            int iN = whenStop[posiWS].second;
            ans[iN]=getValue(max(cards[iN].first, cards[iN].second))+1;//+1 bc on commence au max instant
            posiWS++;
        }
        addRange(1, 0, T-1, 0, queries[iQ], 1);
    }

    int ansTot = 0;
    for(int iN=0; iN<N; iN++)
    {
        if(ans[iN]==-INF)
        {
            ans[iN] = rotaInit[iN] + getValue(max(cards[iN].first, cards[iN].second));
        }
        //printf("%lld:%lld | %lld/%lld\n", iN, ans[iN],  original[cards[iN].first],  original[cards[iN].second]);
        if(ans[iN]%2 == 0)
        {
            ansTot += original[cards[iN].first];
        }
        else
        {
            ansTot += original[cards[iN].second];
        }
    }
    printf("%lld\n", ansTot);

    
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:137:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         while (posiWS< whenStop.size() && whenStop[posiWS].first == iQ)
      |                ~~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%lld%lld", &N, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         scanf("%lld %lld", &Ai, &Bi);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         scanf("%lld", &Ti);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 41048 KB Output is correct
2 Correct 9 ms 41308 KB Output is correct
3 Correct 9 ms 41304 KB Output is correct
4 Correct 11 ms 41560 KB Output is correct
5 Correct 12 ms 41388 KB Output is correct
6 Correct 10 ms 41560 KB Output is correct
7 Correct 9 ms 41308 KB Output is correct
8 Correct 9 ms 41308 KB Output is correct
9 Correct 9 ms 41308 KB Output is correct
10 Correct 9 ms 41264 KB Output is correct
11 Correct 9 ms 41332 KB Output is correct
12 Correct 11 ms 41308 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 41048 KB Output is correct
2 Correct 9 ms 41308 KB Output is correct
3 Correct 9 ms 41304 KB Output is correct
4 Correct 11 ms 41560 KB Output is correct
5 Correct 12 ms 41388 KB Output is correct
6 Correct 10 ms 41560 KB Output is correct
7 Correct 9 ms 41308 KB Output is correct
8 Correct 9 ms 41308 KB Output is correct
9 Correct 9 ms 41308 KB Output is correct
10 Correct 9 ms 41264 KB Output is correct
11 Correct 9 ms 41332 KB Output is correct
12 Correct 11 ms 41308 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 33 ms 45520 KB Output is correct
15 Correct 64 ms 50124 KB Output is correct
16 Correct 105 ms 54132 KB Output is correct
17 Correct 147 ms 58968 KB Output is correct
18 Correct 132 ms 59076 KB Output is correct
19 Correct 140 ms 59076 KB Output is correct
20 Correct 139 ms 59076 KB Output is correct
21 Correct 127 ms 59080 KB Output is correct
22 Correct 99 ms 54276 KB Output is correct
23 Correct 82 ms 51912 KB Output is correct
24 Correct 88 ms 50404 KB Output is correct
25 Correct 91 ms 55492 KB Output is correct
26 Correct 89 ms 53360 KB Output is correct
27 Correct 103 ms 54728 KB Output is correct
28 Correct 113 ms 54640 KB Output is correct
29 Correct 103 ms 55792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 41048 KB Output is correct
2 Correct 9 ms 41308 KB Output is correct
3 Correct 9 ms 41304 KB Output is correct
4 Correct 11 ms 41560 KB Output is correct
5 Correct 12 ms 41388 KB Output is correct
6 Correct 10 ms 41560 KB Output is correct
7 Correct 9 ms 41308 KB Output is correct
8 Correct 9 ms 41308 KB Output is correct
9 Correct 9 ms 41308 KB Output is correct
10 Correct 9 ms 41264 KB Output is correct
11 Correct 9 ms 41332 KB Output is correct
12 Correct 11 ms 41308 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 33 ms 45520 KB Output is correct
15 Correct 64 ms 50124 KB Output is correct
16 Correct 105 ms 54132 KB Output is correct
17 Correct 147 ms 58968 KB Output is correct
18 Correct 132 ms 59076 KB Output is correct
19 Correct 140 ms 59076 KB Output is correct
20 Correct 139 ms 59076 KB Output is correct
21 Correct 127 ms 59080 KB Output is correct
22 Correct 99 ms 54276 KB Output is correct
23 Correct 82 ms 51912 KB Output is correct
24 Correct 88 ms 50404 KB Output is correct
25 Correct 91 ms 55492 KB Output is correct
26 Correct 89 ms 53360 KB Output is correct
27 Correct 103 ms 54728 KB Output is correct
28 Correct 113 ms 54640 KB Output is correct
29 Correct 103 ms 55792 KB Output is correct
30 Correct 306 ms 69620 KB Output is correct
31 Correct 473 ms 82168 KB Output is correct
32 Correct 764 ms 98020 KB Output is correct
33 Correct 1353 ms 130932 KB Output is correct
34 Correct 299 ms 66280 KB Output is correct
35 Correct 1322 ms 130524 KB Output is correct
36 Correct 1406 ms 130960 KB Output is correct
37 Correct 1383 ms 130744 KB Output is correct
38 Correct 1375 ms 130440 KB Output is correct
39 Correct 1343 ms 131880 KB Output is correct
40 Correct 1102 ms 132288 KB Output is correct
41 Correct 1331 ms 131188 KB Output is correct
42 Correct 1422 ms 132296 KB Output is correct
43 Correct 562 ms 132036 KB Output is correct
44 Correct 578 ms 131900 KB Output is correct
45 Correct 598 ms 130744 KB Output is correct
46 Correct 582 ms 96984 KB Output is correct
47 Correct 399 ms 83900 KB Output is correct
48 Correct 712 ms 108640 KB Output is correct
49 Correct 732 ms 109500 KB Output is correct