Submission #769928

# Submission time Handle Problem Language Result Execution time Memory
769928 2023-06-30T14:10:08 Z raysh07 Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
281 ms 22976 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

struct point {
    int a, b, i, pos;
};

int n, q;
const int N = 2e5 + 69;
int t[N];
int s1[4 * N], f[N];
vector <pair<int, int>> t1;

void Build(int l, int r, int pos){
    if (l == r){
        s1[pos] = t1[l - 1].second;
        return;
    }
    
    int mid = (l + r)/2;
    Build(l, mid, pos*2);
    Build(mid + 1, r, pos*2 + 1);
    
    s1[pos] = max(s1[pos * 2], s1[pos * 2 + 1]);
}

int query1(int l, int r, int pos, int ql, int qr){
    if (l >= ql && r <= qr) return s1[pos];
    else if (l > qr || r < ql) return -INF;
    
    int mid = (l + r)/2;
    return max(query1(l, mid, pos*2, ql, qr), query1(mid + 1, r, pos*2 + 1, ql, qr));
}

void upd(int x){
    for (int i = x; i <= q; i += i & (-i)){
        f[i]++;
    }
}

int query(int x){
    int res = 0;
    for (int i = x; i; i -= i & (-i)){
        res += f[i];
    }
    
    return res;
}

bool comp(point a, point b){
    return max(a.a, a.b) > max(b.a, b.b);
}

void Solve() 
{
    cin >> n >> q;
    
    vector <point> a(n);
    
    for (int i = 0; i < n; i++){
        cin >> a[i].a >> a[i].b;
        a[i].i = i;
    }
    
    for (int i = 1; i <= q; i++){
        cin >> t[i];
        t1.push_back({t[i], i});
    }
    
    sort(t1.begin(), t1.end());
    Build(1, q, 1);
    
    
    for (int i = 0; i < n; i++){
        if (lower_bound(t1.begin(), t1.end(), make_pair(a[i].a, 0LL)) == lower_bound(t1.begin(), t1.end(), make_pair(a[i].b, 0LL))){
          //  cout << i << "\n";
            a[i].pos = 0;
            continue;
        }
        
        if (a[i].a < a[i].b) swap(a[i].a, a[i].b);
        int id1 = lower_bound(t1.begin(), t1.end(), make_pair(a[i].b, 0LL)) - t1.begin();
        int id2 = lower_bound(t1.begin(), t1.end(), make_pair(a[i].a, 0LL)) - t1.begin();
        id1++;
        
      //  cout << id1 << " " << id2 << "\n";
        
        a[i].pos = query1(1, q, 1, id1, id2);
     //   cout << a[i].pos << "\n";
    }
    // for (int i = 0; i < n; i++){
    //     cout << a[i].a << " " << a[i].b << " " << a[i].pos << "\n";
    // }
    
    sort(a.begin(), a.end(), comp);
    reverse(t1.begin(), t1.end());
    
    int ptr = 0;
    int ans = 0;
    for (int i = 0; i < n; i++){
        while (ptr != q && t1[ptr].first >= max(a[i].a, a[i].b)){
          //  cout << "ADDED " << t1[ptr].second << "\n";
            upd(t1[ptr].second);
            ptr++;
        }
        
     //   cout << a[i].a << " " << a[i].b << " ";
        
        int flip = query(q) - query(a[i].pos);
     //   cout << flip << "\n";
        if (flip & 1) swap(a[i].a, a[i].b);
        ans += a[i].a;
     //   cout << "HOLY " << a[i].a << "\n";
    }
    
    cout << ans << "\n";
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
 //   cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 10 ms 1628 KB Output is correct
15 Correct 21 ms 2816 KB Output is correct
16 Correct 33 ms 3648 KB Output is correct
17 Correct 45 ms 5052 KB Output is correct
18 Correct 45 ms 5160 KB Output is correct
19 Correct 45 ms 5160 KB Output is correct
20 Correct 47 ms 5116 KB Output is correct
21 Correct 41 ms 5036 KB Output is correct
22 Correct 28 ms 4660 KB Output is correct
23 Correct 28 ms 4656 KB Output is correct
24 Correct 35 ms 4680 KB Output is correct
25 Correct 27 ms 4696 KB Output is correct
26 Correct 33 ms 4808 KB Output is correct
27 Correct 40 ms 5140 KB Output is correct
28 Correct 35 ms 5140 KB Output is correct
29 Correct 31 ms 5128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 10 ms 1628 KB Output is correct
15 Correct 21 ms 2816 KB Output is correct
16 Correct 33 ms 3648 KB Output is correct
17 Correct 45 ms 5052 KB Output is correct
18 Correct 45 ms 5160 KB Output is correct
19 Correct 45 ms 5160 KB Output is correct
20 Correct 47 ms 5116 KB Output is correct
21 Correct 41 ms 5036 KB Output is correct
22 Correct 28 ms 4660 KB Output is correct
23 Correct 28 ms 4656 KB Output is correct
24 Correct 35 ms 4680 KB Output is correct
25 Correct 27 ms 4696 KB Output is correct
26 Correct 33 ms 4808 KB Output is correct
27 Correct 40 ms 5140 KB Output is correct
28 Correct 35 ms 5140 KB Output is correct
29 Correct 31 ms 5128 KB Output is correct
30 Correct 50 ms 13268 KB Output is correct
31 Correct 95 ms 15208 KB Output is correct
32 Correct 152 ms 17816 KB Output is correct
33 Correct 262 ms 22820 KB Output is correct
34 Correct 40 ms 12728 KB Output is correct
35 Correct 267 ms 22976 KB Output is correct
36 Correct 259 ms 22824 KB Output is correct
37 Correct 262 ms 22876 KB Output is correct
38 Correct 260 ms 22948 KB Output is correct
39 Correct 281 ms 22900 KB Output is correct
40 Correct 230 ms 22576 KB Output is correct
41 Correct 255 ms 22900 KB Output is correct
42 Correct 257 ms 22824 KB Output is correct
43 Correct 127 ms 21076 KB Output is correct
44 Correct 126 ms 21308 KB Output is correct
45 Correct 130 ms 21668 KB Output is correct
46 Correct 152 ms 20924 KB Output is correct
47 Correct 157 ms 20864 KB Output is correct
48 Correct 211 ms 22972 KB Output is correct
49 Correct 199 ms 22944 KB Output is correct