Submission #413597

# Submission time Handle Problem Language Result Execution time Memory
413597 2021-05-29T03:47:19 Z cpp219 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
315 ms 15868 KB
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll Log2 = 20;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;

struct data{
    ll x,y,id,dummy;
};
data a[N];
ll n,k,st[4*N],q[N];

bool lf(data a,data b){
    return max(a.x,a.y) > max(b.x,b.y);
}

void upd(ll id,ll l,ll r,ll u,ll val){
    if (u < l||r < u) return;
    if (l == r){
        st[id] = val; return;
    }
    ll mid = (l + r)/2;
    upd(id*2,l,mid,u,val); upd(id*2 + 1,mid + 1,r,u,val);
    st[id] = max(st[id*2],st[id*2 + 1]);
}
/// last but >= val
ll WalkLast(ll id,ll l,ll r,ll val){
    if (l == r){
        if (st[id] >= val) return l;
        return 0;
    }
    ll mid = (l + r)/2;
    if (st[id*2 + 1] >= val) return WalkLast(id*2 + 1,mid + 1,r,val);
    return WalkLast(id*2,l,mid,val);
}

ll bit[N];

void updBIT(ll p,ll val){
    for (ll i = p;i <= k;i += i & -i) bit[i] += val;
}

ll Get(ll p){
    ll res = 0;
    for (ll i = p;i > 0;i -= i & -i) res += bit[i];
    return res;
}

vector<ll> pos;
bool cmp(ll x,ll y){
    return q[x] > q[y];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin>>n>>k;
    for (ll i = 1;i <= n;i++){
        cin>>a[i].x>>a[i].y,a[i].id = i;
        if (a[i].x < a[i].y) a[i].dummy = 1,swap(a[i].x,a[i].y);
    }
    for (ll i = 1;i <= k;i++) cin>>q[i],upd(1,1,k,i,q[i]),pos.push_back(i);
    sort(a + 1,a + n + 1,lf); sort(pos.begin(),pos.end(),cmp);

    ll cur = 0,kq = 0,mn = inf;
    for (ll i = 1;i <= n;i++){
        while(cur < k&&q[pos[cur]] >= a[i].x){
            mn = min(mn,pos[cur]);
            upd(1,1,k,pos[cur],-inf); updBIT(pos[cur],1);
            cur++;
        }
        ll nxt = WalkLast(1,1,k,a[i].y);
        ll ans = Get(k) - Get(nxt); ans %= 2;
        if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
        if (ans) kq += a[i].y;
        else kq += a[i].x;

    }
    cout<<kq;
    /// feel free to use idea : https://codeforces.com/blog/entry/12781?#comment-175184
}

Compilation message

fortune_telling2.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization "O2"
      | 
fortune_telling2.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:88:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   88 |         if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
      |         ^~
fortune_telling2.cpp:88:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   88 |         if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
      |                                      ^~~
fortune_telling2.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 13 ms 1472 KB Output is correct
15 Correct 25 ms 2308 KB Output is correct
16 Correct 39 ms 2764 KB Output is correct
17 Correct 52 ms 3928 KB Output is correct
18 Correct 52 ms 3908 KB Output is correct
19 Correct 49 ms 3932 KB Output is correct
20 Correct 53 ms 3916 KB Output is correct
21 Correct 42 ms 3856 KB Output is correct
22 Correct 39 ms 3908 KB Output is correct
23 Correct 41 ms 3896 KB Output is correct
24 Correct 44 ms 3940 KB Output is correct
25 Correct 38 ms 3908 KB Output is correct
26 Correct 47 ms 3732 KB Output is correct
27 Correct 50 ms 3908 KB Output is correct
28 Correct 52 ms 3908 KB Output is correct
29 Correct 53 ms 3880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 13 ms 1472 KB Output is correct
15 Correct 25 ms 2308 KB Output is correct
16 Correct 39 ms 2764 KB Output is correct
17 Correct 52 ms 3928 KB Output is correct
18 Correct 52 ms 3908 KB Output is correct
19 Correct 49 ms 3932 KB Output is correct
20 Correct 53 ms 3916 KB Output is correct
21 Correct 42 ms 3856 KB Output is correct
22 Correct 39 ms 3908 KB Output is correct
23 Correct 41 ms 3896 KB Output is correct
24 Correct 44 ms 3940 KB Output is correct
25 Correct 38 ms 3908 KB Output is correct
26 Correct 47 ms 3732 KB Output is correct
27 Correct 50 ms 3908 KB Output is correct
28 Correct 52 ms 3908 KB Output is correct
29 Correct 53 ms 3880 KB Output is correct
30 Correct 197 ms 9908 KB Output is correct
31 Correct 221 ms 11016 KB Output is correct
32 Correct 251 ms 12616 KB Output is correct
33 Correct 308 ms 15680 KB Output is correct
34 Correct 123 ms 9424 KB Output is correct
35 Correct 301 ms 15744 KB Output is correct
36 Correct 301 ms 15780 KB Output is correct
37 Correct 315 ms 15752 KB Output is correct
38 Correct 274 ms 15804 KB Output is correct
39 Correct 296 ms 15672 KB Output is correct
40 Correct 221 ms 15756 KB Output is correct
41 Correct 293 ms 15776 KB Output is correct
42 Correct 300 ms 15644 KB Output is correct
43 Correct 204 ms 14652 KB Output is correct
44 Correct 191 ms 14776 KB Output is correct
45 Correct 189 ms 15264 KB Output is correct
46 Correct 239 ms 15868 KB Output is correct
47 Correct 266 ms 15744 KB Output is correct
48 Correct 289 ms 15784 KB Output is correct
49 Correct 284 ms 15656 KB Output is correct