Submission #225820

# Submission time Handle Problem Language Result Execution time Memory
225820 2020-04-21T18:42:57 Z MKopchev Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
1197 ms 52492 KB
#include<bits/stdc++.h>
using namespace std;

const int nmax=2e5+42,inf=1e9+42;

int n,q;

pair<int,int> inp[nmax];

int changes[nmax];

vector<int> tree[4*nmax];

void build(int node,int l,int r)
{
    if(l==r)
    {
        tree[node].push_back(changes[l]);
        return;
    }
    int av=(l+r)/2;
    build(node*2,l,av);
    build(node*2+1,av+1,r);

    tree[node]=tree[node*2];
    for(auto w:tree[node*2+1])tree[node].push_back(w);

    sort(tree[node].begin(),tree[node].end());
}

int last_special[nmax];

int cnt(int node,int lower)
{
    int pos=lower_bound(tree[node].begin(),tree[node].end(),lower)-tree[node].begin();
    return tree[node].size()-pos;
}
int query(int node,int l,int r,int lq,int rq,int lower,int upper)
{
    if(l==lq&&r==rq)
    {
        return cnt(node,lower)-cnt(node,upper+1);
    }
    int av=(l+r)/2,ret=0;

    if(lq<=av)ret=ret+query(node*2,l,av,lq,min(av,rq),lower,upper);
    if(av<rq)ret=ret+query(node*2+1,av+1,r,max(av+1,lq),rq,lower,upper);
    return ret;
}
int main()
{
    scanf("%i%i",&n,&q);

    for(int i=1;i<=n;i++)
        scanf("%i%i",&inp[i].first,&inp[i].second);

    for(int i=1;i<=q;i++)scanf("%i",&changes[i]);

    build(1,1,q);

    for(int i=1;i<=n;i++)
    {
        int ok=0,not_ok=q+1;

        int mini=min(inp[i].first,inp[i].second);
        int maxi=max(inp[i].first,inp[i].second);

        while(not_ok-ok>1)
        {
            int av=(ok+not_ok)/2;
            if(query(1,1,q,av,q,mini,maxi-1)>0)ok=av;
            else not_ok=av;
        }

        last_special[i]=ok;
    }

    long long output=0;
    for(int i=1;i<=n;i++)
    {
        if(last_special[i]&&inp[i].first<inp[i].second)swap(inp[i].first,inp[i].second);

        int swaps=0;

        if(last_special[i]<q)swaps=query(1,1,q,last_special[i]+1,q,max(inp[i].first,inp[i].second),inf);

        if(swaps%2)swap(inp[i].first,inp[i].second);

        output+=inp[i].first;
    }
    printf("%lld\n",output);
    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&n,&q);
     ~~~~~^~~~~~~~~~~~~~
fortune_telling2.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&inp[i].first,&inp[i].second);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:57:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=q;i++)scanf("%i",&changes[i]);
                          ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 15 ms 19200 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 17 ms 19200 KB Output is correct
7 Correct 20 ms 19328 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 15 ms 19328 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 16 ms 19200 KB Output is correct
12 Correct 16 ms 19200 KB Output is correct
13 Correct 18 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 15 ms 19200 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 17 ms 19200 KB Output is correct
7 Correct 20 ms 19328 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 15 ms 19328 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 16 ms 19200 KB Output is correct
12 Correct 16 ms 19200 KB Output is correct
13 Correct 18 ms 19200 KB Output is correct
14 Correct 47 ms 20700 KB Output is correct
15 Correct 83 ms 22264 KB Output is correct
16 Correct 126 ms 23800 KB Output is correct
17 Correct 171 ms 25464 KB Output is correct
18 Correct 168 ms 25444 KB Output is correct
19 Correct 162 ms 25720 KB Output is correct
20 Correct 177 ms 25544 KB Output is correct
21 Correct 146 ms 25464 KB Output is correct
22 Correct 113 ms 24952 KB Output is correct
23 Correct 115 ms 25080 KB Output is correct
24 Correct 119 ms 25080 KB Output is correct
25 Correct 113 ms 25080 KB Output is correct
26 Correct 169 ms 25336 KB Output is correct
27 Correct 253 ms 25568 KB Output is correct
28 Correct 166 ms 25464 KB Output is correct
29 Correct 428 ms 25664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 15 ms 19200 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 17 ms 19200 KB Output is correct
7 Correct 20 ms 19328 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 15 ms 19328 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 16 ms 19200 KB Output is correct
12 Correct 16 ms 19200 KB Output is correct
13 Correct 18 ms 19200 KB Output is correct
14 Correct 47 ms 20700 KB Output is correct
15 Correct 83 ms 22264 KB Output is correct
16 Correct 126 ms 23800 KB Output is correct
17 Correct 171 ms 25464 KB Output is correct
18 Correct 168 ms 25444 KB Output is correct
19 Correct 162 ms 25720 KB Output is correct
20 Correct 177 ms 25544 KB Output is correct
21 Correct 146 ms 25464 KB Output is correct
22 Correct 113 ms 24952 KB Output is correct
23 Correct 115 ms 25080 KB Output is correct
24 Correct 119 ms 25080 KB Output is correct
25 Correct 113 ms 25080 KB Output is correct
26 Correct 169 ms 25336 KB Output is correct
27 Correct 253 ms 25568 KB Output is correct
28 Correct 166 ms 25464 KB Output is correct
29 Correct 428 ms 25664 KB Output is correct
30 Correct 308 ms 46388 KB Output is correct
31 Correct 475 ms 47660 KB Output is correct
32 Correct 651 ms 49196 KB Output is correct
33 Correct 1027 ms 52492 KB Output is correct
34 Correct 266 ms 46260 KB Output is correct
35 Correct 1008 ms 52396 KB Output is correct
36 Correct 1004 ms 52396 KB Output is correct
37 Correct 1004 ms 52304 KB Output is correct
38 Correct 972 ms 52268 KB Output is correct
39 Correct 1054 ms 52268 KB Output is correct
40 Correct 854 ms 52140 KB Output is correct
41 Correct 1008 ms 52396 KB Output is correct
42 Correct 1035 ms 52268 KB Output is correct
43 Correct 667 ms 51628 KB Output is correct
44 Correct 647 ms 51628 KB Output is correct
45 Correct 651 ms 51500 KB Output is correct
46 Correct 654 ms 50476 KB Output is correct
47 Correct 895 ms 50220 KB Output is correct
48 Correct 1197 ms 52396 KB Output is correct
49 Correct 1111 ms 52396 KB Output is correct