Submission #361928

# Submission time Handle Problem Language Result Execution time Memory
361928 2021-02-01T08:46:53 Z AlexLuchianov Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
688 ms 22920 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 200000;
int const inf = 1000000000;

struct Card{
  int x;
  int y;
  int id;
  bool operator < (Card oth) const {
    return y < oth.y;
  }
} v[1 + nmax];

int base[1 + nmax];
struct Node{
  int smin;
  int coef;
  int id;
  Node operator + (Node const oth) const{
    Node result;
    if(smin <= oth.smin) {
      result.smin = smin; 
      result.id = id;
      result.coef = coef;
    } else if(oth.smin < smin) { 
      result.smin = oth.smin;
      result.coef = oth.coef;
      result.id = oth.id;
    }
    return result;
  }
};

class SegmentTree{
  public:
    std::vector<Node> aint;
    std::vector<int> lazy;
    SegmentTree(int n) {
      aint.resize(1 + 4 * n);
      lazy.resize(1 + 4 * n);
    }
    void cleannode(int node, int from, int to) {
      if(from < to) {
        int mid = (from + to) / 2;
        lazy[node * 2] ^= lazy[node];
        lazy[node * 2 + 1] ^= lazy[node];
      }
      aint[node].coef ^= lazy[node];
      lazy[node] = 0;
    }

    void update(int node, int from, int to, int x , int y, int val) {
      if(from == x && to == y) {
        lazy[node] ^= val;
        cleannode(node, from, to);
      } else {
        int mid = (from + to) / 2;
        cleannode(node * 2, from, mid);
        cleannode(node * 2 + 1, mid + 1, to);

        if(from <= mid)
          update(node * 2, from, mid, x, std::min(y, mid), val);
        if(mid + 1 <= y)
          update(node * 2 + 1, mid + 1, to, std::max(x, mid + 1), y, val);
        aint[node] = aint[node * 2] + aint[node * 2 + 1];
      }
    }
    Node query(int node, int from, int to, int x, int y) {
      cleannode(node, from, to);
      if(from == x && to == y)
        return aint[node];
      else {
        int mid = (from + to) / 2;
        if(x <= mid && y <= mid)
          return query(node * 2, from, mid, x, y);
        else if(mid + 1 <= x && mid + 1 <= y)
          return query(node * 2 + 1, mid + 1, to, x, y);
        else
          return query(node * 2, from, mid, x, mid) + query(node * 2 + 1, mid + 1, to, mid + 1, y);
      }
    }

    void setvalue(int node, int from, int to, int x, Node val) {
      if(from < to) {
        int mid = (from + to) / 2;
        cleannode(node * 2, from, mid);
        cleannode(node * 2 + 1, mid + 1, to);

        if(x <= mid)
          setvalue(node * 2, from, mid, x, val);
        else
          setvalue(node * 2 + 1, mid + 1, to, x, val);
        aint[node] = aint[node * 2] + aint[node * 2 + 1];
      } else
        aint[node] = val;
    }
};


int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  int n, q;
  std::cin >> n >> q;
  for(int i = 1;i <= n; i++) {
    std::cin >> v[i].x >> v[i].y;
    v[i].id = i;
    if(v[i].y < v[i].x) {
      base[i] = 1;
      std::swap(v[i].x, v[i].y);
    }
  }
  std::sort(v + 1, v + n + 1);
  std::vector<int> queries;
  for(int i = 1;i <= q; i++) {
    int val;
    std::cin >> val;
    queries.push_back(val);
  }
  SegmentTree aint(n);
  for(int i = 1;i <= n; i++)
    aint.setvalue(1, 1, n, i, {v[i].x, 0, i});

  std::reverse(queries.begin(), queries.end());

  ll result = 0;

  for(int i = 0; i < queries.size(); i++) {
    int x = 0;
    int target = queries[i];
    for(int jump = (1 << 20); 0 < jump; jump /= 2)
      if(x + jump <= n && v[x + jump].y <= target)
        x += jump;
    if(0 < x)
      aint.update(1, 1, n, 1, x, 1);
    if(x < n) {
      bool flag;
      do {
        flag = false;
        Node oth = aint.query(1, 1, n, x + 1, n);
        if(oth.smin <= target) {
          flag = true;
          aint.setvalue(1, 1, n, oth.id, {inf + 1, 0, 0});
          if(oth.coef == 0)
            result += v[oth.id].y;
          else
            result += v[oth.id].x;
        }
      } while(flag);
    }
  }

  bool flag;
  do {
    flag = false;
    Node oth = aint.query(1, 1, n, 1, n);
    if(oth.smin <= inf) {
      flag = true;
      aint.setvalue(1, 1, n, oth.id, {inf + 1, 0, 0}); 
      oth.coef ^= base[v[oth.id].id];
      if(oth.coef == 1)
        result += v[oth.id].y;
      else
        result += v[oth.id].x;
    }
  } while(flag);
  std::cout << result; 
  return 0;
}

Compilation message

fortune_telling2.cpp: In member function 'void SegmentTree::cleannode(int, int, int)':
fortune_telling2.cpp:53:13: warning: unused variable 'mid' [-Wunused-variable]
   53 |         int mid = (from + to) / 2;
      |             ^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:138:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |   for(int i = 0; i < queries.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 21 ms 1516 KB Output is correct
15 Correct 46 ms 2668 KB Output is correct
16 Correct 65 ms 3820 KB Output is correct
17 Correct 98 ms 5104 KB Output is correct
18 Correct 98 ms 5104 KB Output is correct
19 Correct 82 ms 4976 KB Output is correct
20 Correct 88 ms 5104 KB Output is correct
21 Correct 65 ms 4976 KB Output is correct
22 Correct 72 ms 4336 KB Output is correct
23 Correct 67 ms 4336 KB Output is correct
24 Correct 75 ms 4336 KB Output is correct
25 Correct 53 ms 4336 KB Output is correct
26 Correct 73 ms 4464 KB Output is correct
27 Correct 85 ms 5104 KB Output is correct
28 Correct 74 ms 4976 KB Output is correct
29 Correct 85 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 21 ms 1516 KB Output is correct
15 Correct 46 ms 2668 KB Output is correct
16 Correct 65 ms 3820 KB Output is correct
17 Correct 98 ms 5104 KB Output is correct
18 Correct 98 ms 5104 KB Output is correct
19 Correct 82 ms 4976 KB Output is correct
20 Correct 88 ms 5104 KB Output is correct
21 Correct 65 ms 4976 KB Output is correct
22 Correct 72 ms 4336 KB Output is correct
23 Correct 67 ms 4336 KB Output is correct
24 Correct 75 ms 4336 KB Output is correct
25 Correct 53 ms 4336 KB Output is correct
26 Correct 73 ms 4464 KB Output is correct
27 Correct 85 ms 5104 KB Output is correct
28 Correct 74 ms 4976 KB Output is correct
29 Correct 85 ms 4976 KB Output is correct
30 Correct 174 ms 4072 KB Output is correct
31 Correct 269 ms 8236 KB Output is correct
32 Correct 403 ms 12904 KB Output is correct
33 Correct 626 ms 22760 KB Output is correct
34 Correct 34 ms 3136 KB Output is correct
35 Correct 665 ms 22760 KB Output is correct
36 Correct 688 ms 22880 KB Output is correct
37 Correct 610 ms 22760 KB Output is correct
38 Correct 503 ms 22888 KB Output is correct
39 Correct 596 ms 22760 KB Output is correct
40 Correct 381 ms 22504 KB Output is correct
41 Correct 550 ms 22760 KB Output is correct
42 Correct 587 ms 22760 KB Output is correct
43 Correct 235 ms 21352 KB Output is correct
44 Correct 235 ms 21352 KB Output is correct
45 Correct 253 ms 21352 KB Output is correct
46 Correct 484 ms 20260 KB Output is correct
47 Correct 573 ms 19976 KB Output is correct
48 Correct 499 ms 22920 KB Output is correct
49 Correct 469 ms 22840 KB Output is correct