Submission #197279

# Submission time Handle Problem Language Result Execution time Memory
197279 2020-01-20T03:32:14 Z quocnguyen1012 Examination (JOI19_examination) C++14
100 / 100
1451 ms 91956 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 5;

class BIT2Dx {
  public:
  const int INF = 2e9;
  int n;
  vector<vector<int>> nodes, f;

  void fakeUpdate(int u, int v){
    for (int x = u; x > 0; x -= x & -x)
      nodes[x].push_back(v);
  }

  void fakeGet(int u, int v){
    for (int x = u; x <= n; x += x & -x)
      nodes[x].push_back(v);
  }

  void update(int u, int v){
    for (int x = u; x > 0; x -= x & -x)
      for (int y = upper_bound(nodes[x].begin(), nodes[x].end(), v) - nodes[x].begin(); y > 0; y -= y & -y)
        f[x][y]++;
  }

  int get(int u, int v){
    int res = 0;
    for (int x = u; x <= n; x += x & -x)
      for (int y = lower_bound(nodes[x].begin(), nodes[x].end(), v) - nodes[x].begin() + 1; y <= nodes[x].size(); y += y & -y)
        res += f[x][y];
    return res;
  }
  void init(int _n){
      n = _n;
      nodes.assign(n + 5, {});
      f.assign(n + 5, {});
  }
  void normalize(void){
    for (int i = 1; i <= n; ++i){
      nodes[i].push_back(INF);
      sort(nodes[i].begin(), nodes[i].end());
      f[i].resize(nodes[i].size() + 3);
    }
  }
}ft;

int N, Q;
pair<int, int> stu[maxn];
int ans[maxn];
vector<int> val;

int compress(int x)
{
  return lower_bound(val.begin(), val.end(), x) - val.begin() + 1;
}

class Query
{
  public:
  int A, B, C, id;
}query[maxn];

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> Q;
  for (int i = 1; i <= N; ++i){
    cin >> stu[i].fi >> stu[i].se;
    val.pb(stu[i].fi); val.pb(stu[i].se); val.pb(stu[i].fi + stu[i].se);
  }
  for (int i = 1; i <= Q; ++i){
    query[i].id = i;
    cin >> query[i].A >> query[i].B >> query[i].C;
    val.pb(query[i].A); val.pb(query[i].B); val.pb(query[i].C);
  }
  sort(val.begin(), val.end()); val.erase(unique(val.begin(), val.end()), val.end());
  ft.init(val.size());
  sort(stu + 1, stu + 1 + N, greater<pair<int, int>>());
  sort(query + 1, query + 1 + N, [&](const Query & x, const Query & y){
    return x.A > y.A;
  });
  int j = 1;
  for (int i = 1; i <= Q; ++i){
    while (j <= N && stu[j].fi >= query[i].A){
      ft.fakeUpdate(compress(stu[j].se), compress(stu[j].fi + stu[j].se));
      ++j;
    }
    ft.fakeGet(compress(query[i].B), compress(query[i].C));
  }
  ft.normalize();
  j = 1;
  for (int i = 1; i <= Q; ++i){
    while (j <= N && stu[j].fi >= query[i].A){
      ft.update(compress(stu[j].se), compress(stu[j].fi + stu[j].se));
      ++j;
    }
    ans[query[i].id] = ft.get(compress(query[i].B), compress(query[i].C));
    //cerr << ft.get(compress(query[i].B), compress(query[i].C));
  }
  for (int i = 1; i <= Q; ++i){
    cout << ans[i] << '\n';
  }
}

Compilation message

examination.cpp: In member function 'int BIT2Dx::get(int, int)':
examination.cpp:38:95: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int y = lower_bound(nodes[x].begin(), nodes[x].end(), v) - nodes[x].begin() + 1; y <= nodes[x].size(); y += y & -y)
                                                                                             ~~^~~~~~~~~~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
examination.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 20 ms 2808 KB Output is correct
8 Correct 21 ms 2808 KB Output is correct
9 Correct 21 ms 2808 KB Output is correct
10 Correct 16 ms 2296 KB Output is correct
11 Correct 17 ms 2168 KB Output is correct
12 Correct 7 ms 760 KB Output is correct
13 Correct 17 ms 2428 KB Output is correct
14 Correct 16 ms 2424 KB Output is correct
15 Correct 16 ms 2424 KB Output is correct
16 Correct 11 ms 1528 KB Output is correct
17 Correct 15 ms 1912 KB Output is correct
18 Correct 5 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 704 ms 36344 KB Output is correct
2 Correct 693 ms 36684 KB Output is correct
3 Correct 714 ms 36664 KB Output is correct
4 Correct 345 ms 31360 KB Output is correct
5 Correct 532 ms 31328 KB Output is correct
6 Correct 128 ms 10976 KB Output is correct
7 Correct 668 ms 36500 KB Output is correct
8 Correct 654 ms 37456 KB Output is correct
9 Correct 600 ms 36320 KB Output is correct
10 Correct 448 ms 30156 KB Output is correct
11 Correct 321 ms 30036 KB Output is correct
12 Correct 96 ms 8288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 704 ms 36344 KB Output is correct
2 Correct 693 ms 36684 KB Output is correct
3 Correct 714 ms 36664 KB Output is correct
4 Correct 345 ms 31360 KB Output is correct
5 Correct 532 ms 31328 KB Output is correct
6 Correct 128 ms 10976 KB Output is correct
7 Correct 668 ms 36500 KB Output is correct
8 Correct 654 ms 37456 KB Output is correct
9 Correct 600 ms 36320 KB Output is correct
10 Correct 448 ms 30156 KB Output is correct
11 Correct 321 ms 30036 KB Output is correct
12 Correct 96 ms 8288 KB Output is correct
13 Correct 937 ms 39304 KB Output is correct
14 Correct 908 ms 36532 KB Output is correct
15 Correct 753 ms 36652 KB Output is correct
16 Correct 719 ms 31732 KB Output is correct
17 Correct 711 ms 31576 KB Output is correct
18 Correct 154 ms 10848 KB Output is correct
19 Correct 986 ms 39180 KB Output is correct
20 Correct 1010 ms 37708 KB Output is correct
21 Correct 999 ms 39268 KB Output is correct
22 Correct 466 ms 30176 KB Output is correct
23 Correct 318 ms 30112 KB Output is correct
24 Correct 96 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 20 ms 2808 KB Output is correct
8 Correct 21 ms 2808 KB Output is correct
9 Correct 21 ms 2808 KB Output is correct
10 Correct 16 ms 2296 KB Output is correct
11 Correct 17 ms 2168 KB Output is correct
12 Correct 7 ms 760 KB Output is correct
13 Correct 17 ms 2428 KB Output is correct
14 Correct 16 ms 2424 KB Output is correct
15 Correct 16 ms 2424 KB Output is correct
16 Correct 11 ms 1528 KB Output is correct
17 Correct 15 ms 1912 KB Output is correct
18 Correct 5 ms 632 KB Output is correct
19 Correct 704 ms 36344 KB Output is correct
20 Correct 693 ms 36684 KB Output is correct
21 Correct 714 ms 36664 KB Output is correct
22 Correct 345 ms 31360 KB Output is correct
23 Correct 532 ms 31328 KB Output is correct
24 Correct 128 ms 10976 KB Output is correct
25 Correct 668 ms 36500 KB Output is correct
26 Correct 654 ms 37456 KB Output is correct
27 Correct 600 ms 36320 KB Output is correct
28 Correct 448 ms 30156 KB Output is correct
29 Correct 321 ms 30036 KB Output is correct
30 Correct 96 ms 8288 KB Output is correct
31 Correct 937 ms 39304 KB Output is correct
32 Correct 908 ms 36532 KB Output is correct
33 Correct 753 ms 36652 KB Output is correct
34 Correct 719 ms 31732 KB Output is correct
35 Correct 711 ms 31576 KB Output is correct
36 Correct 154 ms 10848 KB Output is correct
37 Correct 986 ms 39180 KB Output is correct
38 Correct 1010 ms 37708 KB Output is correct
39 Correct 999 ms 39268 KB Output is correct
40 Correct 466 ms 30176 KB Output is correct
41 Correct 318 ms 30112 KB Output is correct
42 Correct 96 ms 8540 KB Output is correct
43 Correct 1303 ms 85956 KB Output is correct
44 Correct 1312 ms 90820 KB Output is correct
45 Correct 1289 ms 91040 KB Output is correct
46 Correct 915 ms 68916 KB Output is correct
47 Correct 972 ms 66912 KB Output is correct
48 Correct 210 ms 12508 KB Output is correct
49 Correct 1255 ms 90668 KB Output is correct
50 Correct 1451 ms 91956 KB Output is correct
51 Correct 1260 ms 91792 KB Output is correct
52 Correct 813 ms 56528 KB Output is correct
53 Correct 366 ms 45792 KB Output is correct