Submission #736257

# Submission time Handle Problem Language Result Execution time Memory
736257 2023-05-05T11:19:01 Z math_rabbit_1028 Radio Towers (IOI22_towers) C++17
18 / 100
1153 ms 38272 KB
#include "towers.h"
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> arr;
vector<int> upidx, downidx;

struct localmax {
  int idx, d, l, r;
  bool operator<(const localmax &other) const {
    if (d == other.d) return idx < other.idx;
    return d < other.d;
  }
  void update() {
    d = arr[upidx[idx]] - max(arr[l], arr[r]);
  } 
} local_value[101010];
int adj[101010][2];
set<localmax> local;

vector< pair<int, int> > Dlist;

struct Node {
  Node *lt, *rt;
  int val;
  Node() {
    lt = rt = NULL;
    val = 0;
  }
};
Node *root[101010];
int first[101010];

void build(Node *node, int st, int ed) {
  if (st == ed) {
    node->val = first[st];
    return;
  }
  int mid = (st + ed) / 2;
  node->lt = new Node();
  node->rt = new Node();
  build(node->lt, st, mid);
  build(node->rt, mid + 1, ed);
  node->val = node->lt->val + node->rt->val;
}

void update(Node *prev, Node *now, int st, int ed, int idx, int value) {
  if (st == ed) {
    now->val = value;
    return;
  }
  int mid = (st + ed) / 2;
  if (idx <= mid) {
    now->lt = new Node();
    now->rt = prev->rt;
    update(prev->lt, now->lt, st, mid, idx, value);
  }
  else {
    now->lt = prev->lt;
    now->rt = new Node();
    update(prev->rt, now->rt, mid + 1, ed, idx, value);
  }
  now->val = now->lt->val + now->rt->val;
}

int get(Node *node, int st, int ed, int gs, int ge) {
  if (st > ge || ed < gs) return 0;
  if (gs <= st && ed <= ge) return node->val;
  int mid = (st + ed) / 2;
  return get(node->lt, st, mid, gs, ge) + get(node->rt, mid + 1, ed, gs, ge);
}

void init(int N, vector<int> H) {
  n = N;
  for (int i = 0; i < n; i++) arr.push_back(H[i]);
  
  if (arr[0] < arr[1]) downidx.push_back(0);
  for (int i = 1; i < n - 1; i++) {
    if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) upidx.push_back(i);
    if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) downidx.push_back(i);
  }
  if (arr[n - 1] < arr[n - 2]) downidx.push_back(n - 1);

  for (int i = 0; i < upidx.size(); i++) {
    local_value[i] = {i, 0, downidx[i], downidx[i + 1]};
    local_value[i].update();
    local.insert(local_value[i]);
    adj[i][0] = i - 1;
    adj[i][1] = i + 1;
  }

  while (!local.empty()) {
    localmax now = *local.begin();
    Dlist.push_back({now.d, upidx[now.idx]});
    int next = (arr[now.l] < arr[now.r] ? now.l : now.r);
    local.erase(local.begin());
    if (adj[now.idx][0] >= 0) {
      set<localmax>::iterator iter = local.find(local_value[adj[now.idx][0]]);
      if (iter != local.end()) {
        local.erase(iter);
        local_value[adj[now.idx][0]].r = next;
        local_value[adj[now.idx][0]].update();
        local.insert(local_value[adj[now.idx][0]]);
        adj[adj[now.idx][0]][1] = adj[now.idx][1];
      }
    }
    if (adj[now.idx][1] < upidx.size()) {
      set<localmax>::iterator iter = local.find(local_value[adj[now.idx][1]]);
      if (iter != local.end()) {
        local.erase(iter);
        local_value[adj[now.idx][1]].l = next;
        local_value[adj[now.idx][1]].update();
        local.insert(local_value[adj[now.idx][1]]);
        adj[adj[now.idx][1]][0] = adj[now.idx][0];
      }
    }
  }

  for (int i = 0; i < upidx.size(); i++) first[upidx[i]] = 1;

  root[0] = new Node();
  build(root[0], 0, n - 1);
  for (int i = 0; i < Dlist.size(); i++) {
    root[i + 1] = new Node();
    update(root[i], root[i + 1], 0, n - 1, Dlist[i].second, 0);
  }


}

int max_towers(int L, int R, int D) {
  int d = lower_bound(Dlist.begin(), Dlist.end(), make_pair(D, 0)) - Dlist.begin();
  int ans = get(root[d], 0, n - 1, L + 1, R - 1) + 1;
  if (arr[L] < arr[L + 1]) {
    int near = lower_bound(upidx.begin(), upidx.end(), L) - upidx.begin();
    if (near < upidx.size() && arr[upidx[near]] < arr[L] + D) ans--;
  }
  if (arr[R] < arr[R - 1]) {
    int near = lower_bound(upidx.begin(), upidx.end(), R) - upidx.begin();
    if (near > 0 && arr[upidx[near - 1]] < arr[R] + D) ans--;
  }
  return max(ans, 1);
}

Compilation message

towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:85:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |   for (int i = 0; i < upidx.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~
towers.cpp:108:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     if (adj[now.idx][1] < upidx.size()) {
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towers.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |   for (int i = 0; i < upidx.size(); i++) first[upidx[i]] = 1;
      |                   ~~^~~~~~~~~~~~~~
towers.cpp:124:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |   for (int i = 0; i < Dlist.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:137:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |     if (near < upidx.size() && arr[upidx[near]] < arr[L] + D) ans--;
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 483 ms 4684 KB Output is correct
2 Correct 920 ms 7752 KB Output is correct
3 Correct 958 ms 7728 KB Output is correct
4 Correct 753 ms 7720 KB Output is correct
5 Correct 811 ms 7636 KB Output is correct
6 Correct 906 ms 7720 KB Output is correct
7 Correct 968 ms 7716 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 720 KB Output is correct
3 Incorrect 1 ms 720 KB 1st lines differ - on the 1st token, expected: '91', found: '90'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 720 KB Output is correct
3 Incorrect 1 ms 720 KB 1st lines differ - on the 1st token, expected: '91', found: '90'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 744 ms 27816 KB Output is correct
2 Correct 1118 ms 28100 KB Output is correct
3 Correct 1086 ms 28100 KB Output is correct
4 Correct 1115 ms 38076 KB Output is correct
5 Correct 1054 ms 38172 KB Output is correct
6 Correct 1153 ms 38184 KB Output is correct
7 Correct 1020 ms 38100 KB Output is correct
8 Correct 968 ms 7644 KB Output is correct
9 Correct 873 ms 7740 KB Output is correct
10 Correct 1053 ms 7712 KB Output is correct
11 Correct 949 ms 7732 KB Output is correct
12 Correct 856 ms 7744 KB Output is correct
13 Correct 932 ms 7712 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 81 ms 28080 KB Output is correct
18 Correct 118 ms 38148 KB Output is correct
19 Correct 121 ms 38272 KB Output is correct
20 Correct 23 ms 7720 KB Output is correct
21 Correct 18 ms 7700 KB Output is correct
22 Correct 81 ms 28096 KB Output is correct
23 Correct 117 ms 38168 KB Output is correct
24 Correct 117 ms 38192 KB Output is correct
25 Correct 28 ms 7716 KB Output is correct
26 Correct 26 ms 7716 KB Output is correct
27 Correct 2 ms 720 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 2 ms 848 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 2 ms 720 KB Output is correct
33 Correct 2 ms 848 KB Output is correct
34 Correct 2 ms 848 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 330 ms 6480 KB 3rd lines differ - on the 1st token, expected: '150', found: '149'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 720 KB Output is correct
3 Incorrect 1 ms 720 KB 1st lines differ - on the 1st token, expected: '91', found: '90'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 483 ms 4684 KB Output is correct
2 Correct 920 ms 7752 KB Output is correct
3 Correct 958 ms 7728 KB Output is correct
4 Correct 753 ms 7720 KB Output is correct
5 Correct 811 ms 7636 KB Output is correct
6 Correct 906 ms 7720 KB Output is correct
7 Correct 968 ms 7716 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 1 ms 720 KB Output is correct
13 Incorrect 1 ms 720 KB 1st lines differ - on the 1st token, expected: '91', found: '90'
14 Halted 0 ms 0 KB -