Submission #86358

# Submission time Handle Problem Language Result Execution time Memory
86358 2018-11-26T08:08:32 Z KCSC Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
946 ms 83544 KB
#include <bits/stdc++.h>
using namespace std;
 
const int DIM = 600005;
 
pair<int, int> seg[DIM], num[DIM]; vector<int> aux, lst[DIM];
int sgt[DIM << 2], bit[DIM], arr[DIM], pos[DIM];
 
inline bool cmp(pair<int, int> sg1, pair<int, int> sg2)
{
    return max(sg1.first, sg1.second) < max(sg2.first, sg2.second);
}
 
void update(int nod, int lef, int rig, int pos, int val)
{
    if (lef == rig)
        sgt[nod] = max(sgt[nod], val);
    else {
        int mid = (lef + rig) >> 1;
        
        if (pos <= mid)
            update(nod << 1, lef, mid, pos, val);
        else
            update(nod << 1 | 1, mid + 1, rig, pos, val);
        
        sgt[nod] = max(sgt[nod << 1], sgt[nod << 1 | 1]);
    }
}
 
int query(int nod, int lef, int rig, int _lef, int _rig)
{
    if (_lef <= lef and rig <= _rig)
        return sgt[nod];
    else {
        int mid = (lef + rig) >> 1;
        
        if (_rig <= mid)
            return query(nod << 1, lef, mid, _lef, _rig);
        if (_lef >  mid)
            return query(nod << 1 | 1, mid + 1, rig, _lef, _rig);
        
        return max(query(nod << 1, lef, mid, _lef, _rig),
                   query(nod << 1 | 1, mid + 1, rig, _lef, _rig));
    }
}
 
void update(int pos, int n)
{
    for (int i = pos; i <= n; i += (i & -i))
        bit[i] ^= 1;
}
 
int query(int pos, int x = 0)
{
    for (int i = pos; i >= 1; i -= (i & -i))
        x ^= bit[i];
    return x;
}
 
long long bruteForce(int n, int k)
{
    for (int i = 1; i <= n; ++i)
        cin >> seg[i].first >> seg[i].second;
    
    while (k--) {
        int x;
        cin >> x;
        
        for (int i = 1; i <= n; ++i)
            if (seg[i].first <= x)
                swap(seg[i].first, seg[i].second);
    }
    
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
        ans += seg[i].first;
    
    return ans;
}
 
long long solve(int n, int k)
{
    long long ans = 0;
    
    int sum = 0, nr = 0, nr2 = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> seg[i].first >> seg[i].second;
        
        if (seg[i].first == seg[i].second) {
            ans += seg[i--].first; --n;
            continue;
        }
       /*
        int a = seg[i].first, b = seg[i].second;
        if (a > b) swap(a, b);
        
        if (a <= 58 and 58 < b)
            ++nr, sum += b;
        else
        if (b <= 58)
            sum += seg[i].second, nr2++;
        else
            sum += seg[i].first;
        */
        aux.push_back(seg[i].first);
        aux.push_back(seg[i].second);
    }
    
   // cerr << nr << endl << nr2 << endl << sum + ans;
    
    if (!n)
        return ans;
    
    for (int i = 1; i <= k; ++i) {
        cin >> pos[i];
        aux.push_back(pos[i]);
    }
    
    sort(aux.begin(), aux.end());
    aux.resize(unique(aux.begin(), aux.end()) - aux.begin()); int m = (int) aux.size();
    
    sort(seg + 1, seg + n + 1, cmp);
    
    for (int i = 1; i <= n; ++i) {
        num[i] = seg[i];
        seg[i].first = (int) (lower_bound(aux.begin(), aux.end(), seg[i].first) - aux.begin() + 1);
        seg[i].second = (int) (lower_bound(aux.begin(), aux.end(), seg[i].second) - aux.begin() + 1);
    }
    
    for (int i = 1; i <= k; ++i) {
        pos[i] = (int) (lower_bound(aux.begin(), aux.end(), pos[i]) - aux.begin() + 1);
        update(1, 1, m, pos[i], i);
    }
    
    for (int i = 1; i <= n; ++i) {
        int a = seg[i].first, b = seg[i].second;
        
        if (a > b)
            swap(a, b);
        lst[query(1, 1, m, a, b - 1)].push_back(i);
    }
    
    for (; k >= 0; --k) {
        for (int x : lst[k]) {
            bool val = query(x);
            if (k) val ^= (num[x].second > num[x].first);
            ans += val ? num[x].second : num[x].first;
        }
        
        int psx = 0;
        for (int lef = 1, rig = n; lef <= rig; ) {
            int mid = (lef + rig) >> 1;
            
            if (max(seg[mid].second, seg[mid].first) <= pos[k])
                lef = mid + 1, psx = mid;
            else
                rig = mid - 1;
        }
        
        if (psx != 0)
            update(1, n), update(psx + 1, n);
    }
    
    return ans;
}
 
void generate(int n, int k)
{
    srand(unsigned(time(0)));
    cout << n << " " << k << endl;
    
    for (int i = 1; i <= n; ++i)
        cout << 1LL * rand() * rand() % 100 + 1 << " "
             << 1LL * rand() * rand() % 100 + 1 << endl;
    
    for (int i = 1; i <= k; ++i)
        cout << 1LL * rand() * rand() % 100 + 1 << endl;
    exit(0);
}
 
int main(void)
{
   // freopen("test.in", "r", stdin);
   // freopen("test.out", "w", stdout);
    
    int n, k;
    cin >> n >> k;
    
 //   generate(1000, 1);
    
    if (n <= 1000 and k <= 1000)
        cout << bruteForce(n, k) << endl;
    else
        cout << solve(n, k) << endl;
    
    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'long long int solve(int, int)':
fortune_telling2.cpp:85:9: warning: unused variable 'sum' [-Wunused-variable]
     int sum = 0, nr = 0, nr2 = 0;
         ^~~
fortune_telling2.cpp:85:18: warning: unused variable 'nr' [-Wunused-variable]
     int sum = 0, nr = 0, nr2 = 0;
                  ^~
fortune_telling2.cpp:85:26: warning: unused variable 'nr2' [-Wunused-variable]
     int sum = 0, nr = 0, nr2 = 0;
                          ^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 17 ms 14464 KB Output is correct
3 Correct 21 ms 14688 KB Output is correct
4 Correct 22 ms 14688 KB Output is correct
5 Correct 19 ms 14768 KB Output is correct
6 Correct 19 ms 14776 KB Output is correct
7 Correct 21 ms 14852 KB Output is correct
8 Correct 17 ms 14884 KB Output is correct
9 Correct 17 ms 14916 KB Output is correct
10 Correct 15 ms 14940 KB Output is correct
11 Correct 19 ms 14952 KB Output is correct
12 Correct 19 ms 15000 KB Output is correct
13 Correct 20 ms 15032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 17 ms 14464 KB Output is correct
3 Correct 21 ms 14688 KB Output is correct
4 Correct 22 ms 14688 KB Output is correct
5 Correct 19 ms 14768 KB Output is correct
6 Correct 19 ms 14776 KB Output is correct
7 Correct 21 ms 14852 KB Output is correct
8 Correct 17 ms 14884 KB Output is correct
9 Correct 17 ms 14916 KB Output is correct
10 Correct 15 ms 14940 KB Output is correct
11 Correct 19 ms 14952 KB Output is correct
12 Correct 19 ms 15000 KB Output is correct
13 Correct 20 ms 15032 KB Output is correct
14 Correct 42 ms 16088 KB Output is correct
15 Correct 71 ms 17400 KB Output is correct
16 Correct 105 ms 19064 KB Output is correct
17 Correct 137 ms 20640 KB Output is correct
18 Correct 166 ms 21800 KB Output is correct
19 Correct 129 ms 22960 KB Output is correct
20 Correct 133 ms 24120 KB Output is correct
21 Correct 138 ms 25392 KB Output is correct
22 Correct 127 ms 25912 KB Output is correct
23 Correct 109 ms 26172 KB Output is correct
24 Correct 105 ms 26940 KB Output is correct
25 Correct 107 ms 28224 KB Output is correct
26 Correct 109 ms 28904 KB Output is correct
27 Correct 125 ms 30240 KB Output is correct
28 Correct 123 ms 31476 KB Output is correct
29 Correct 124 ms 32700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 17 ms 14464 KB Output is correct
3 Correct 21 ms 14688 KB Output is correct
4 Correct 22 ms 14688 KB Output is correct
5 Correct 19 ms 14768 KB Output is correct
6 Correct 19 ms 14776 KB Output is correct
7 Correct 21 ms 14852 KB Output is correct
8 Correct 17 ms 14884 KB Output is correct
9 Correct 17 ms 14916 KB Output is correct
10 Correct 15 ms 14940 KB Output is correct
11 Correct 19 ms 14952 KB Output is correct
12 Correct 19 ms 15000 KB Output is correct
13 Correct 20 ms 15032 KB Output is correct
14 Correct 42 ms 16088 KB Output is correct
15 Correct 71 ms 17400 KB Output is correct
16 Correct 105 ms 19064 KB Output is correct
17 Correct 137 ms 20640 KB Output is correct
18 Correct 166 ms 21800 KB Output is correct
19 Correct 129 ms 22960 KB Output is correct
20 Correct 133 ms 24120 KB Output is correct
21 Correct 138 ms 25392 KB Output is correct
22 Correct 127 ms 25912 KB Output is correct
23 Correct 109 ms 26172 KB Output is correct
24 Correct 105 ms 26940 KB Output is correct
25 Correct 107 ms 28224 KB Output is correct
26 Correct 109 ms 28904 KB Output is correct
27 Correct 125 ms 30240 KB Output is correct
28 Correct 123 ms 31476 KB Output is correct
29 Correct 124 ms 32700 KB Output is correct
30 Correct 350 ms 36092 KB Output is correct
31 Correct 360 ms 42380 KB Output is correct
32 Correct 534 ms 47896 KB Output is correct
33 Correct 730 ms 61296 KB Output is correct
34 Correct 214 ms 61296 KB Output is correct
35 Correct 769 ms 69068 KB Output is correct
36 Correct 738 ms 74692 KB Output is correct
37 Correct 946 ms 80616 KB Output is correct
38 Correct 719 ms 83008 KB Output is correct
39 Correct 792 ms 83008 KB Output is correct
40 Correct 700 ms 83544 KB Output is correct
41 Correct 754 ms 83544 KB Output is correct
42 Correct 775 ms 83544 KB Output is correct
43 Correct 557 ms 83544 KB Output is correct
44 Correct 519 ms 83544 KB Output is correct
45 Correct 527 ms 83544 KB Output is correct
46 Correct 543 ms 83544 KB Output is correct
47 Correct 526 ms 83544 KB Output is correct
48 Correct 612 ms 83544 KB Output is correct
49 Correct 606 ms 83544 KB Output is correct