답안 #434247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434247 2021-06-20T19:13:16 Z mat_v 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
1160 ms 60880 KB
#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define pii pair<int,int>
#define xx first
#define yy second
#define pb push_back

using namespace std;
typedef long long ll;

int n,k;
pii niz[200005];
pii val[200005];
int seg[4 * 200005];
void init(int node, int l, int r){
    if(l == r){
        seg[node] = val[l].yy;
        return;
    }
    int mid = (l + r) / 2;
    init(node * 2, l, mid);
    init(node * 2 + 1, mid + 1, r);
    seg[node] = max(seg[node * 2], seg[node * 2 + 1]);
}
int kveri(int node, int l, int r, int levo, int desno){
    if(l > r || levo > desno || l > desno || levo > r)return 0;
    if(l >= levo && r <= desno)return seg[node];
    int mid = (l + r) / 2;
    return max(kveri(node * 2, l, mid, levo, desno), kveri(node * 2 + 1, mid + 1, r, levo, desno));
}

vector<int> kv[600005];
bool cmp2(pii a, pii b){
    return a.yy < b.yy;
}

map<int,int>poj;
int bit[600005];
pii orig[600005];
void update(int x){
    while(x>0){
        bit[x]++;
        x -= (x&-x);
    }
}
int query(int x){
    int ans = 0;
    while(x<=600000){
        ans += bit[x];
        x += (x&-x);
    }
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    ff(i,1,n){
        cin >> niz[i].xx;
        cin >> niz[i].yy;
        //orig[i] = niz[i];
    }
    ff(i,1,k){
        cin >> val[i].xx;
        val[i].yy = i;
    }
    sort(val + 1, val + k + 1);
    init(1,1,k);

    ff(i,1,n){
        int mn = min(niz[i].xx,niz[i].yy);
        int mx = max(niz[i].xx,niz[i].yy);
        mx--;
        int l = 1;
        int r = k;
        int levo = -1;
        if(val[k].xx >= mn){
            while(l < r){
                int mid = (l + r) / 2;
                if(val[mid].xx >= mn)r = mid;
                else l = mid + 1;
            }
            levo = l;
        }

        int desno = -1;
        l = 1;
        r = k;
        if(val[1].xx <= mx){
            while(l < r){
                int mid = (l + r + 1) / 2;
                if(val[mid].xx <= mx)l = mid;

                else r = mid - 1;
            }
            desno = l;
        }
        //cout << levo << " " << desno << "\n";
        if(desno == -1 || levo == -1 || levo > desno){
            //cout << i << "\n";
            kv[1].pb(i);
            continue;
        }
        if(levo <= desno){
            kv[kveri(1,1,k,levo,desno)].pb(i);
            if(niz[i].xx <= niz[i].yy)swap(niz[i].xx, niz[i].yy);
            continue;
        }
    }

    sort(val + 1, val + k + 1, cmp2);

    vector<int> svi;
    ff(i,1,k)svi.pb(val[i].xx);
    ff(i,1,n){
        svi.pb(niz[i].xx);
        svi.pb(niz[i].yy);
    }
    ff(i,1,n)orig[i] = niz[i];
    sort(svi.begin(), svi.end());
    int last = -1;
    int br=1;
    for(auto c:svi){
        if(c == last)continue;
        poj[c] = br;
        br++;
        last = c;
    }
    ff(i,1,n){
        niz[i].xx = poj[niz[i].xx];
        niz[i].yy = poj[niz[i].yy];
    }
    ff(i,1,k)val[i].xx = poj[val[i].xx];
    fb(i,k,1){
        update(val[i].xx);
        for(auto c:kv[i]){
            int sta = query(max(niz[c].xx, niz[c].yy));
            if(sta%2 == 1)swap(orig[c].xx,orig[c].yy);
        }

    }
    ll sum = 0;
    ff(i,1,n)sum += orig[i].xx;

    cout << sum << "\n";
    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:60:5: note: in expansion of macro 'ff'
   60 |     ff(i,1,n){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:65:5: note: in expansion of macro 'ff'
   65 |     ff(i,1,k){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:72:5: note: in expansion of macro 'ff'
   72 |     ff(i,1,n){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:116:5: note: in expansion of macro 'ff'
  116 |     ff(i,1,k)svi.pb(val[i].xx);
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:117:5: note: in expansion of macro 'ff'
  117 |     ff(i,1,n){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:121:5: note: in expansion of macro 'ff'
  121 |     ff(i,1,n)orig[i] = niz[i];
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:131:5: note: in expansion of macro 'ff'
  131 |     ff(i,1,n){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:135:5: note: in expansion of macro 'ff'
  135 |     ff(i,1,k)val[i].xx = poj[val[i].xx];
      |     ^~
fortune_telling2.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
fortune_telling2.cpp:136:5: note: in expansion of macro 'fb'
  136 |     fb(i,k,1){
      |     ^~
fortune_telling2.cpp:2:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    2 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
fortune_telling2.cpp:145:5: note: in expansion of macro 'ff'
  145 |     ff(i,1,n)sum += orig[i].xx;
      |     ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14412 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 10 ms 14564 KB Output is correct
4 Correct 11 ms 14540 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 13 ms 14540 KB Output is correct
8 Correct 11 ms 14652 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 11 ms 14540 KB Output is correct
11 Correct 12 ms 14540 KB Output is correct
12 Correct 11 ms 14540 KB Output is correct
13 Correct 11 ms 14572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14412 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 10 ms 14564 KB Output is correct
4 Correct 11 ms 14540 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 13 ms 14540 KB Output is correct
8 Correct 11 ms 14652 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 11 ms 14540 KB Output is correct
11 Correct 12 ms 14540 KB Output is correct
12 Correct 11 ms 14540 KB Output is correct
13 Correct 11 ms 14572 KB Output is correct
14 Correct 35 ms 16392 KB Output is correct
15 Correct 87 ms 18516 KB Output is correct
16 Correct 101 ms 20424 KB Output is correct
17 Correct 146 ms 22592 KB Output is correct
18 Correct 143 ms 22740 KB Output is correct
19 Correct 143 ms 22528 KB Output is correct
20 Correct 132 ms 22724 KB Output is correct
21 Correct 126 ms 22524 KB Output is correct
22 Correct 103 ms 20520 KB Output is correct
23 Correct 91 ms 19512 KB Output is correct
24 Correct 82 ms 18760 KB Output is correct
25 Correct 92 ms 21044 KB Output is correct
26 Correct 91 ms 20080 KB Output is correct
27 Correct 119 ms 20644 KB Output is correct
28 Correct 104 ms 20640 KB Output is correct
29 Correct 110 ms 21748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14412 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 10 ms 14564 KB Output is correct
4 Correct 11 ms 14540 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 13 ms 14540 KB Output is correct
8 Correct 11 ms 14652 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 11 ms 14540 KB Output is correct
11 Correct 12 ms 14540 KB Output is correct
12 Correct 11 ms 14540 KB Output is correct
13 Correct 11 ms 14572 KB Output is correct
14 Correct 35 ms 16392 KB Output is correct
15 Correct 87 ms 18516 KB Output is correct
16 Correct 101 ms 20424 KB Output is correct
17 Correct 146 ms 22592 KB Output is correct
18 Correct 143 ms 22740 KB Output is correct
19 Correct 143 ms 22528 KB Output is correct
20 Correct 132 ms 22724 KB Output is correct
21 Correct 126 ms 22524 KB Output is correct
22 Correct 103 ms 20520 KB Output is correct
23 Correct 91 ms 19512 KB Output is correct
24 Correct 82 ms 18760 KB Output is correct
25 Correct 92 ms 21044 KB Output is correct
26 Correct 91 ms 20080 KB Output is correct
27 Correct 119 ms 20644 KB Output is correct
28 Correct 104 ms 20640 KB Output is correct
29 Correct 110 ms 21748 KB Output is correct
30 Correct 268 ms 30196 KB Output is correct
31 Correct 428 ms 35380 KB Output is correct
32 Correct 650 ms 42036 KB Output is correct
33 Correct 1160 ms 54916 KB Output is correct
34 Correct 251 ms 30912 KB Output is correct
35 Correct 1084 ms 60700 KB Output is correct
36 Correct 1091 ms 60676 KB Output is correct
37 Correct 1119 ms 60668 KB Output is correct
38 Correct 1068 ms 60660 KB Output is correct
39 Correct 1086 ms 60880 KB Output is correct
40 Correct 934 ms 60360 KB Output is correct
41 Correct 1068 ms 60776 KB Output is correct
42 Correct 1062 ms 60752 KB Output is correct
43 Correct 530 ms 59024 KB Output is correct
44 Correct 523 ms 59204 KB Output is correct
45 Correct 529 ms 59564 KB Output is correct
46 Correct 505 ms 43436 KB Output is correct
47 Correct 431 ms 38508 KB Output is correct
48 Correct 652 ms 50716 KB Output is correct
49 Correct 674 ms 50544 KB Output is correct