Submission #381738

# Submission time Handle Problem Language Result Execution time Memory
381738 2021-03-25T19:45:15 Z mohamedsobhi777 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
463 ms 28764 KB
#include <bits/stdc++.h>

using namespace std;

#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound

using ll = long long;
using ld = long double;

const int N = 6e5 + 7;
const ll mod = 1e9 + 7;

int n, k;
int act[N];

struct fenwick
{
       int bit[N];
       fenwick()
       {
              memset(bit, 0, sizeof bit);
       }
       void add(int x, int v)
       {
              ++x;
              for (; x < N; x += x & -x)
                     bit[x] += v;
       }
       int upto(int x)
       {
              ++x;
              int ret = 0;
              for (; x; x -= x & -x)
                     ret += bit[x];
              return ret;
       }
       inline int get(int l, int r) { return upto(r) - upto(l - 1); }
} f1;

template <class T>
struct segtree
{
       T tree[4 * N];
       segtree()
       {
              memset(tree, -1, sizeof tree);
       }
       T eval(T x, T y) { return max(x, y); }
       void update(int ix, T val, int node = 1, int L = 0, int R = N - 1)
       {
              if (L == R)
                     return void(tree[node] = val);
              int mid = (L + R) >> 1;
              if (ix <= mid)
                     update(ix, val, node * 2, L, mid);
              else
                     update(ix, val, node * 2 + 1, mid + 1, R);
              tree[node] = eval(tree[node * 2], tree[node * 2 + 1]);
       }
       T query(int l, int r, int node = 1, int L = 0, int R = N - 1)
       {
              if (l > r || l > R || r < L)
                     return -1;
              if (L >= l && R <= r)
                     return tree[node];
              int mid = (L + R) >> 1;
              return eval(query(l, r, node * 2, L, mid), query(l, r, node * 2 + 1, mid + 1, R));
       }
};

segtree<int> sg;

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       cin >> n >> k;
       vii a(n);
       for (int i = 0; i < n; ++i)
              cin >> a[i].f >> a[i].s;
       vi b(k);
       for (auto &u : b)
              cin >> u;

       vector<int> v;
       for (auto u : a)
       {
              v.pb(u.f);
              v.pb(u.s);
       }
       for (auto u : b)
              v.pb(u);

       sort(all(v));

       v.erase(unique(all(v)), v.end());

       for (int i = 0; i < sz(v); ++i)
       {
              act[i] = v[i];
       }
       auto get = [&](int x) -> int {
              return lb(all(v), x) - v.begin();
       };
       for (int i = 0; i < n; ++i)
       {
              a[i].f = get(a[i].f);
              a[i].s = get(a[i].s);
       }
       for (auto &u : b)
              u = get(u);

       sort(all(a), [&](pii x, pii y) {
              return max(x.f, x.s) > max(y.f, y.s);
       });

       vii b2;
       for (int i = 0; i < k; ++i)
       {
              b2.push_back({b[i], i});
              sg.update(b[i], i);
       }
       sort(all(b2));
       ll sum = 0;

       for (int i = 0; i < n; ++i)
       {

              while (sz(b2) && b2.back().f >= max(a[i].f, a[i].s))
              {
                     f1.add(b2.back().s, 1);
                     b2.pop_back();
              }
              int L = min(a[i].f, a[i].s);
              int R = max(a[i].f, a[i].s);

              int lst = sg.query(L, R - 1);

              if (~lst)
              {
                     if (a[i].f < a[i].s)
                            swap(a[i].f, a[i].s);
                     if (f1.get(lst + 1, N - 2) & 1)
                     {
                            swap(a[i].f, a[i].s);
                     }
              }
              else
              {
                     if (f1.get(0, N - 2) & 1)
                     {
                            swap(a[i].f, a[i].s);
                     }
              }
              sum += act[a[i].f];
       }

       cout << sum;
       return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 9 ms 12268 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 9 ms 12268 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 24 ms 12652 KB Output is correct
15 Correct 44 ms 13160 KB Output is correct
16 Correct 71 ms 13668 KB Output is correct
17 Correct 86 ms 14316 KB Output is correct
18 Correct 86 ms 15468 KB Output is correct
19 Correct 81 ms 15468 KB Output is correct
20 Correct 80 ms 15468 KB Output is correct
21 Correct 81 ms 15596 KB Output is correct
22 Correct 65 ms 14828 KB Output is correct
23 Correct 64 ms 14700 KB Output is correct
24 Correct 63 ms 14700 KB Output is correct
25 Correct 65 ms 14956 KB Output is correct
26 Correct 64 ms 15084 KB Output is correct
27 Correct 72 ms 15340 KB Output is correct
28 Correct 67 ms 15340 KB Output is correct
29 Correct 73 ms 15340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 9 ms 12268 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 24 ms 12652 KB Output is correct
15 Correct 44 ms 13160 KB Output is correct
16 Correct 71 ms 13668 KB Output is correct
17 Correct 86 ms 14316 KB Output is correct
18 Correct 86 ms 15468 KB Output is correct
19 Correct 81 ms 15468 KB Output is correct
20 Correct 80 ms 15468 KB Output is correct
21 Correct 81 ms 15596 KB Output is correct
22 Correct 65 ms 14828 KB Output is correct
23 Correct 64 ms 14700 KB Output is correct
24 Correct 63 ms 14700 KB Output is correct
25 Correct 65 ms 14956 KB Output is correct
26 Correct 64 ms 15084 KB Output is correct
27 Correct 72 ms 15340 KB Output is correct
28 Correct 67 ms 15340 KB Output is correct
29 Correct 73 ms 15340 KB Output is correct
30 Correct 165 ms 19092 KB Output is correct
31 Correct 218 ms 21604 KB Output is correct
32 Correct 299 ms 23784 KB Output is correct
33 Correct 451 ms 28636 KB Output is correct
34 Correct 143 ms 18540 KB Output is correct
35 Correct 456 ms 28636 KB Output is correct
36 Correct 463 ms 28628 KB Output is correct
37 Correct 452 ms 28508 KB Output is correct
38 Correct 438 ms 28636 KB Output is correct
39 Correct 431 ms 28764 KB Output is correct
40 Correct 418 ms 28380 KB Output is correct
41 Correct 426 ms 28764 KB Output is correct
42 Correct 431 ms 28508 KB Output is correct
43 Correct 320 ms 28124 KB Output is correct
44 Correct 313 ms 28252 KB Output is correct
45 Correct 315 ms 27996 KB Output is correct
46 Correct 323 ms 25820 KB Output is correct
47 Correct 303 ms 25052 KB Output is correct
48 Correct 354 ms 27868 KB Output is correct
49 Correct 343 ms 27868 KB Output is correct