답안 #372910

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372910 2021-03-02T09:00:19 Z BartolM 운세 보기 2 (JOI14_fortune_telling2) C++17
35 / 100
287 ms 50888 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=4e5+5;
const int OFF=(1<<20);

int n, q;
vector <pii> v, vec[N];
vector <int> que, saz;
int desaz[N], sol[N], Tmax[2*OFF], T[2*OFF], br[N];
#define DEBUG 0

int sazmi(int x) {
    int ret=lower_bound(saz.begin(), saz.end(), x)-saz.begin();
    desaz[ret]=x;
    return ret;
}

int query_max(int a, int b, int pos=1, int lo=0, int hi=OFF) {
    if (lo>=a && hi<=b) return Tmax[pos];
    if (lo>=b || hi<=a) return -1;
    int mid=(lo+hi)/2;
    return max(query_max(a, b, pos*2, lo, mid), query_max(a, b, pos*2+1, mid, hi));
}

int query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
    if (lo>=a && hi<=b) return T[pos];
    if (lo>=b || hi<=a) return 0;
    int mid=(lo+hi)/2;
    return query(a, b, pos*2, lo, mid)+query(a, b, pos*2+1, mid, hi);
}

void update(int pos, int x) {
    pos+=OFF; T[pos]+=x;
    pos/=2;
    while (pos) {
        T[pos]=T[pos*2]+T[pos*2+1];
        pos/=2;
    }
}

void build() {
    for (int i=OFF-1; i>0; --i) Tmax[i]=max(Tmax[i*2], Tmax[i*2+1]);
}

void solve() {
    sort(saz.begin(), saz.end());
    saz.erase(unique(saz.begin(), saz.end()), saz.end());
    memset(Tmax, -1, sizeof Tmax);

    for (int i=0; i<q; ++i) {
        que[i]=sazmi(que[i]);
        Tmax[OFF+que[i]]=i;
    }
    build();

    for (int i=0; i<n; ++i) {
        v[i].X=sazmi(v[i].X); v[i].Y=sazmi(v[i].Y);
        int mn=min(v[i].X, v[i].Y), mx=max(v[i].X, v[i].Y);
        int pos=query_max(mn, mx);
        br[i]=pos;
        vec[pos+1].pb(mp(mx, i));
    }
    for (int i=q-1; i>=0; --i) {
        update(que[i], 1);
        for (pii pp:vec[i]) sol[pp.Y]=query(pp.X, OFF);
    }
    #if DEBUG
        for (int i=0; i<n; ++i) {
            printf("%d %d\n", v[i].X, v[i].Y);
        }
        for (int x:que) printf("%d\n", x);
    #endif // DEBUG

    ll res=0;
    for (int i=0; i<n; ++i) {
        int curr;
        if (br[i]==-1) curr=sol[i]%2 ? v[i].Y : v[i].X;
        else {
            if (v[i].X>v[i].Y) swap(v[i].X, v[i].Y);
            curr=sol[i]%2 ? v[i].X : v[i].Y;
        }
        res+=desaz[curr];
        #if DEBUG
            printf("i: %d, pos: %d, sol: %d, curr: %d, koji: %d\n", i, br[i], sol[i], curr, desaz[curr]);
        #endif
    }
    printf("%lld\n", res);
}

void load() {
    scanf("%d %d", &n, &q);
    for (int i=0; i<n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        saz.pb(a); saz.pb(b);
        v.pb(mp(a, b));
    }
    for (int i=0; i<q; ++i) {
        int x;
        scanf("%d", &x);
        que.pb(x); saz.pb(x);
    }
}

int main() {
    load();
    solve();
    return 0;
}

/*
1 4
7 8
8
6
5
3

*/

Compilation message

fortune_telling2.cpp: In function 'void load()':
fortune_telling2.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  113 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 18028 KB Output is correct
2 Correct 13 ms 18028 KB Output is correct
3 Correct 16 ms 18028 KB Output is correct
4 Correct 14 ms 18028 KB Output is correct
5 Correct 13 ms 18028 KB Output is correct
6 Correct 13 ms 18028 KB Output is correct
7 Correct 13 ms 18028 KB Output is correct
8 Correct 13 ms 18028 KB Output is correct
9 Correct 13 ms 18028 KB Output is correct
10 Correct 13 ms 18028 KB Output is correct
11 Correct 14 ms 18028 KB Output is correct
12 Correct 13 ms 18028 KB Output is correct
13 Correct 13 ms 18028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 18028 KB Output is correct
2 Correct 13 ms 18028 KB Output is correct
3 Correct 16 ms 18028 KB Output is correct
4 Correct 14 ms 18028 KB Output is correct
5 Correct 13 ms 18028 KB Output is correct
6 Correct 13 ms 18028 KB Output is correct
7 Correct 13 ms 18028 KB Output is correct
8 Correct 13 ms 18028 KB Output is correct
9 Correct 13 ms 18028 KB Output is correct
10 Correct 13 ms 18028 KB Output is correct
11 Correct 14 ms 18028 KB Output is correct
12 Correct 13 ms 18028 KB Output is correct
13 Correct 13 ms 18028 KB Output is correct
14 Correct 34 ms 18796 KB Output is correct
15 Correct 47 ms 19560 KB Output is correct
16 Correct 68 ms 20324 KB Output is correct
17 Correct 89 ms 21088 KB Output is correct
18 Correct 87 ms 21216 KB Output is correct
19 Correct 86 ms 21216 KB Output is correct
20 Correct 91 ms 21216 KB Output is correct
21 Correct 85 ms 21472 KB Output is correct
22 Correct 71 ms 20576 KB Output is correct
23 Correct 66 ms 20448 KB Output is correct
24 Correct 69 ms 20192 KB Output is correct
25 Correct 66 ms 21088 KB Output is correct
26 Correct 70 ms 20320 KB Output is correct
27 Correct 82 ms 20704 KB Output is correct
28 Correct 74 ms 20704 KB Output is correct
29 Correct 85 ms 21100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 18028 KB Output is correct
2 Correct 13 ms 18028 KB Output is correct
3 Correct 16 ms 18028 KB Output is correct
4 Correct 14 ms 18028 KB Output is correct
5 Correct 13 ms 18028 KB Output is correct
6 Correct 13 ms 18028 KB Output is correct
7 Correct 13 ms 18028 KB Output is correct
8 Correct 13 ms 18028 KB Output is correct
9 Correct 13 ms 18028 KB Output is correct
10 Correct 13 ms 18028 KB Output is correct
11 Correct 14 ms 18028 KB Output is correct
12 Correct 13 ms 18028 KB Output is correct
13 Correct 13 ms 18028 KB Output is correct
14 Correct 34 ms 18796 KB Output is correct
15 Correct 47 ms 19560 KB Output is correct
16 Correct 68 ms 20324 KB Output is correct
17 Correct 89 ms 21088 KB Output is correct
18 Correct 87 ms 21216 KB Output is correct
19 Correct 86 ms 21216 KB Output is correct
20 Correct 91 ms 21216 KB Output is correct
21 Correct 85 ms 21472 KB Output is correct
22 Correct 71 ms 20576 KB Output is correct
23 Correct 66 ms 20448 KB Output is correct
24 Correct 69 ms 20192 KB Output is correct
25 Correct 66 ms 21088 KB Output is correct
26 Correct 70 ms 20320 KB Output is correct
27 Correct 82 ms 20704 KB Output is correct
28 Correct 74 ms 20704 KB Output is correct
29 Correct 85 ms 21100 KB Output is correct
30 Correct 121 ms 22780 KB Output is correct
31 Correct 201 ms 24916 KB Output is correct
32 Correct 287 ms 27988 KB Output is correct
33 Runtime error 212 ms 50888 KB Execution killed with signal 11
34 Halted 0 ms 0 KB -