답안 #855883

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
855883 2023-10-02T06:30:24 Z rxlfd314 Examination (JOI19_examination) C++17
100 / 100
1399 ms 275488 KB
// haha lol wtf am I doing
#include <bits/stdc++.h>
using namespace std;

#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)

#define REP(a, b, c, d) for (int a = (b); (d) > 0 ? a <= (c) : a >= (c); a += (d))
#define FOR(a, b, c) REP(a, b, c, 1)
#define ROF(a, b, c) REP(a, b, c, -1)

#define chmax(a, b) a = a > (b) ? a : (b)
#define chmin(a, b) a = a < (b) ? a : (b)

struct ST {
  int val;
  ST *lft, *rht;
  ST() : lft(nullptr), rht(nullptr), val(0) {}
  void upd(int p, int v, int tl, int tr) {
    if (tl == tr)
      val += v;
    else {
      int tm = tl + tr >> 1;
      if (p > tm) {
        if (!rht)
          rht = new ST();
        rht->upd(p, v, tm+1, tr);
      } else {
        if (!lft)
          lft = new ST();
        lft->upd(p, v, tl, tm);
      } 
      val = (lft ? lft->val : 0) + (rht ? rht->val : 0);
    }
  }
  int qry(int ql, int qr, int tl, int tr) {
    if (tl > qr || tr < ql)
      return 0;
    if (ql <= tl && tr <= qr)
      return val;
    int tm = tl + tr >> 1;
    return (lft ? lft->qry(ql, qr, tl, tm) : 0) + (rht ? rht->qry(ql, qr, tm+1, tr) : 0);
  }
};

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int N, Q;
  cin >> N >> Q;
  array<int, 5> evs[N+Q];
  vt<int> cpa, cpb;
  FOR(i, 0, N-1) {
    int a, b;
    cin >> a >> b;
    cpa.push_back(a);
    cpb.push_back(b);
    evs[i] = {a + b, a, b, -1, -1};
  }
  
  FOR(i, 0, Q-1) {
    int x, y, z;
    cin >> x >> y >> z;
    evs[N+i] = {z, -1, x, y, i};
  }
  
  sort(evs, evs+N+Q);
  
  sort(all(cpa));
  cpa.erase(unique(all(cpa)), end(cpa));
  auto ind_a = [&](int v) {
    return int(lower_bound(all(cpa), v) - begin(cpa));
  };
  
  sort(all(cpb));
  cpb.erase(unique(all(cpb)), end(cpb));
  auto ind_b = [&](int v) {
    return int(lower_bound(all(cpb), v) - begin(cpb));
  };
  
  vt<ST> sgt(size(cpa), ST());
  auto upd = [&](int a, int b, int v) {
    a = ind_a(a), b = ind_b(b);
    for (; a < size(cpa); a += a+1 & -a-1)
      sgt[a].upd(b, v, 0, size(cpb)-1);
  };
  auto p_qry = [&](int i, int l, int r) {
    int ret = 0;
    for (; ~i; i -= i+1 & -i-1)
      ret += sgt[i].qry(l, r, 0, size(cpb)-1);
    return ret;
  };
  auto qry = [&](int a, int b, int c, int d) {
    a = ind_a(a), b = min(ind_a(b), size(cpa)-1), c = ind_b(c), d = ind_b(d);
    return p_qry(b, c, d) - p_qry(a-1, c, d);
  };
  FOR(i, 0, N+Q-1)
    if (~evs[i][1])
      upd(evs[i][1], evs[i][2], 1);
 
  int ans[Q];
  FOR(i, 0, N+Q-1) {
    if (~evs[i][1])
      upd(evs[i][1], evs[i][2], -1);
    else
      ans[evs[i][4]] = qry(evs[i][2], INT_MAX, evs[i][3], INT_MAX);
  }
  
  FOR(i, 0, Q-1)
    cout << ans[i] << '\n';
}

Compilation message

examination.cpp: In constructor 'ST::ST()':
examination.cpp:18:13: warning: 'ST::rht' will be initialized after [-Wreorder]
   18 |   ST *lft, *rht;
      |             ^~~
examination.cpp:17:7: warning:   'int ST::val' [-Wreorder]
   17 |   int val;
      |       ^~~
examination.cpp:19:3: warning:   when initialized here [-Wreorder]
   19 |   ST() : lft(nullptr), rht(nullptr), val(0) {}
      |   ^~
examination.cpp: In member function 'void ST::upd(int, int, int, int)':
examination.cpp:24:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |       int tm = tl + tr >> 1;
      |                ~~~^~~~
examination.cpp: In member function 'int ST::qry(int, int, int, int)':
examination.cpp:42:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |     int tm = tl + tr >> 1;
      |              ~~~^~~~
examination.cpp: In lambda function:
examination.cpp:86:33: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   86 |     for (; a < size(cpa); a += a+1 & -a-1)
      |                                ~^~
examination.cpp: In lambda function:
examination.cpp:91:22: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   91 |     for (; ~i; i -= i+1 & -i-1)
      |                     ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 13 ms 4700 KB Output is correct
8 Correct 13 ms 4952 KB Output is correct
9 Correct 13 ms 4700 KB Output is correct
10 Correct 5 ms 1412 KB Output is correct
11 Correct 6 ms 1372 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 14 ms 4792 KB Output is correct
14 Correct 13 ms 4700 KB Output is correct
15 Correct 13 ms 4956 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 3 ms 604 KB Output is correct
18 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1057 ms 233064 KB Output is correct
2 Correct 1035 ms 233012 KB Output is correct
3 Correct 1119 ms 233004 KB Output is correct
4 Correct 153 ms 25544 KB Output is correct
5 Correct 181 ms 26344 KB Output is correct
6 Correct 91 ms 7144 KB Output is correct
7 Correct 911 ms 232832 KB Output is correct
8 Correct 840 ms 232696 KB Output is correct
9 Correct 810 ms 232484 KB Output is correct
10 Correct 109 ms 11584 KB Output is correct
11 Correct 88 ms 9148 KB Output is correct
12 Correct 60 ms 6848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1057 ms 233064 KB Output is correct
2 Correct 1035 ms 233012 KB Output is correct
3 Correct 1119 ms 233004 KB Output is correct
4 Correct 153 ms 25544 KB Output is correct
5 Correct 181 ms 26344 KB Output is correct
6 Correct 91 ms 7144 KB Output is correct
7 Correct 911 ms 232832 KB Output is correct
8 Correct 840 ms 232696 KB Output is correct
9 Correct 810 ms 232484 KB Output is correct
10 Correct 109 ms 11584 KB Output is correct
11 Correct 88 ms 9148 KB Output is correct
12 Correct 60 ms 6848 KB Output is correct
13 Correct 1337 ms 233200 KB Output is correct
14 Correct 1267 ms 233256 KB Output is correct
15 Correct 1008 ms 232648 KB Output is correct
16 Correct 184 ms 26056 KB Output is correct
17 Correct 227 ms 26556 KB Output is correct
18 Correct 74 ms 7108 KB Output is correct
19 Correct 1346 ms 233300 KB Output is correct
20 Correct 1351 ms 233476 KB Output is correct
21 Correct 1239 ms 233336 KB Output is correct
22 Correct 102 ms 11456 KB Output is correct
23 Correct 84 ms 9160 KB Output is correct
24 Correct 59 ms 6940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 13 ms 4700 KB Output is correct
8 Correct 13 ms 4952 KB Output is correct
9 Correct 13 ms 4700 KB Output is correct
10 Correct 5 ms 1412 KB Output is correct
11 Correct 6 ms 1372 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 14 ms 4792 KB Output is correct
14 Correct 13 ms 4700 KB Output is correct
15 Correct 13 ms 4956 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 3 ms 604 KB Output is correct
18 Correct 3 ms 604 KB Output is correct
19 Correct 1057 ms 233064 KB Output is correct
20 Correct 1035 ms 233012 KB Output is correct
21 Correct 1119 ms 233004 KB Output is correct
22 Correct 153 ms 25544 KB Output is correct
23 Correct 181 ms 26344 KB Output is correct
24 Correct 91 ms 7144 KB Output is correct
25 Correct 911 ms 232832 KB Output is correct
26 Correct 840 ms 232696 KB Output is correct
27 Correct 810 ms 232484 KB Output is correct
28 Correct 109 ms 11584 KB Output is correct
29 Correct 88 ms 9148 KB Output is correct
30 Correct 60 ms 6848 KB Output is correct
31 Correct 1337 ms 233200 KB Output is correct
32 Correct 1267 ms 233256 KB Output is correct
33 Correct 1008 ms 232648 KB Output is correct
34 Correct 184 ms 26056 KB Output is correct
35 Correct 227 ms 26556 KB Output is correct
36 Correct 74 ms 7108 KB Output is correct
37 Correct 1346 ms 233300 KB Output is correct
38 Correct 1351 ms 233476 KB Output is correct
39 Correct 1239 ms 233336 KB Output is correct
40 Correct 102 ms 11456 KB Output is correct
41 Correct 84 ms 9160 KB Output is correct
42 Correct 59 ms 6940 KB Output is correct
43 Correct 1378 ms 275400 KB Output is correct
44 Correct 1399 ms 275376 KB Output is correct
45 Correct 1354 ms 275488 KB Output is correct
46 Correct 198 ms 32400 KB Output is correct
47 Correct 286 ms 33248 KB Output is correct
48 Correct 74 ms 7112 KB Output is correct
49 Correct 1145 ms 275296 KB Output is correct
50 Correct 1175 ms 275440 KB Output is correct
51 Correct 970 ms 275140 KB Output is correct
52 Correct 129 ms 15304 KB Output is correct
53 Correct 102 ms 10772 KB Output is correct