Submission #307942

# Submission time Handle Problem Language Result Execution time Memory
307942 2020-09-29T15:33:31 Z AlexLuchianov Constellation 3 (JOI20_constellation3) C++14
100 / 100
468 ms 107384 KB
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <algorithm>
#include <set>

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

class SegmentTree{
private:
  std::vector<std::pair<int,int>> aint;
public:
  SegmentTree(int n) {
    aint.resize(1 + 4 * n);
  }
  void computenode(int node, int from, int to) {
    aint[node] = std::max(aint[node * 2] , aint[node * 2 + 1]);
  }
  
  void build(int node, int from, int to, std::vector<int> &aux) {
    if(from < to) {
      int mid = (from + to) / 2;
      build(node * 2, from, mid, aux);
      build(node * 2 + 1, mid + 1, to, aux);
      computenode(node, from, to);
    } else
      aint[node] = {aux[from], from};
  }

  std::pair<int,int> query(int node, int from, int to, int x, int y) {
    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);
      if(mid + 1 <= x && mid + 1 <= y)
        return query(node * 2 + 1, mid + 1, to, x, y);
      else
        return std::max(query(node * 2, from, mid, x, mid),
                        query(node * 2 + 1, mid + 1, to, mid + 1, y));
    }
  }
};

int const nmax = 200000;

ll const inf = 1000000000000000000;

std::vector<std::vector<std::pair<int,int>>> event;

class SpecialSet{
public:
  std::set<std::pair<int, ll>> myset;
  ll offset = 0;
  void update(ll val) {
    offset += val;
  }
  void _insert(std::pair<int, ll> val) {
    val.second -= offset;
    std::set<std::pair<int,ll>>::iterator it = myset.upper_bound(val);
    if(it == myset.begin()) 
      myset.insert(val);
    else {
      it--;
      if(val.second < it->second)
        myset.insert(val);
    }
    it = myset.upper_bound(val);
    while(it != myset.end() && val.second <= it->second)
      myset.erase(it++);
  }
  ll extract(int wall) {
    std::set<std::pair<int,ll>>::iterator it = myset.lower_bound({wall + 1, -inf});
    assert(it != myset.begin());
    it--;
    return it->second + offset;
  }
  int size() {
    return myset.size();
  }
  void absorb(SpecialSet oth) {
    std::set<std::pair<int, ll>>::iterator it = oth.myset.begin();
    while(it != oth.myset.end()) {
      _insert({it->first, it->second + oth.offset});
      it++;
    }
  }
  void print() {
    std::cout << "Print\n";
    for(std::set<std::pair<int, ll>>::iterator it = myset.begin(); it != myset.end(); it++)
      std::cout << it->first << " " << it->second + offset << '\n';
    std::cout << '\n';
  }
};

int n;
std::vector<int> v;

void divide(int from, int to, SegmentTree &aint, SpecialSet &myset) {
  if(from == to) {
    ll result = 0;
    for(int h = 0; h < event[from].size(); h++)
      result += event[from][h].second;
    myset._insert({0, result});
    for(int h = 0; h < event[from].size(); h++) 
      myset._insert({event[from][h].first, result - event[from][h].second});
   
  } else {
    int mid = aint.query(1, 1, n, from, to).second;
    int target = v[mid];
    if(mid == to) 
      mid--;
    SpecialSet myset1, myset2;
    divide(from, mid, aint, myset1);
    divide(mid + 1, to, aint, myset2);
    ll upper1 = myset2.extract(target), upper2 = myset1.extract(target);
    myset1.update(upper1);
    myset2.update(upper2);

    if(myset1.size() < myset2.size())
      std::swap(myset1, myset2);
    myset1.absorb(myset2);
    std::swap(myset, myset1);
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  std::cin >> n;
  event.resize(1 + n);

  v = std::vector<int>(1 + n);
  for(int i = 1;i <= n; i++)
    std::cin >> v[i];
  int q;
  std::cin >> q;
  SegmentTree aint(n);
  aint.build(1, 1, n, v);

  for(int i = 1;i <= q; i++) {
    int x, y, cost;
    std::cin >> x >> y >> cost;
    event[x].push_back({y, cost});
  }
  SpecialSet myset;
  divide(1, n, aint, myset);
  std::cout << myset.extract(n);
  return 0;
}

Compilation message

constellation3.cpp: In function 'void divide(int, int, SegmentTree&, SpecialSet&)':
constellation3.cpp:106:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for(int h = 0; h < event[from].size(); h++)
      |                    ~~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int h = 0; h < event[from].size(); h++)
      |                    ~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 3 ms 1280 KB Output is correct
33 Correct 3 ms 1280 KB Output is correct
34 Correct 3 ms 1152 KB Output is correct
35 Correct 3 ms 1024 KB Output is correct
36 Correct 3 ms 1024 KB Output is correct
37 Correct 3 ms 1024 KB Output is correct
38 Correct 3 ms 1408 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 4 ms 1280 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 4 ms 1280 KB Output is correct
44 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 3 ms 1280 KB Output is correct
33 Correct 3 ms 1280 KB Output is correct
34 Correct 3 ms 1152 KB Output is correct
35 Correct 3 ms 1024 KB Output is correct
36 Correct 3 ms 1024 KB Output is correct
37 Correct 3 ms 1024 KB Output is correct
38 Correct 3 ms 1408 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 4 ms 1280 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 4 ms 1280 KB Output is correct
44 Correct 3 ms 512 KB Output is correct
45 Correct 264 ms 21880 KB Output is correct
46 Correct 245 ms 21632 KB Output is correct
47 Correct 253 ms 21752 KB Output is correct
48 Correct 250 ms 21752 KB Output is correct
49 Correct 248 ms 21368 KB Output is correct
50 Correct 248 ms 21240 KB Output is correct
51 Correct 248 ms 21240 KB Output is correct
52 Correct 274 ms 22008 KB Output is correct
53 Correct 245 ms 21608 KB Output is correct
54 Correct 333 ms 102904 KB Output is correct
55 Correct 331 ms 85240 KB Output is correct
56 Correct 301 ms 73592 KB Output is correct
57 Correct 294 ms 65272 KB Output is correct
58 Correct 272 ms 62968 KB Output is correct
59 Correct 278 ms 63356 KB Output is correct
60 Correct 269 ms 107384 KB Output is correct
61 Correct 295 ms 21432 KB Output is correct
62 Correct 468 ms 102056 KB Output is correct
63 Correct 280 ms 21404 KB Output is correct
64 Correct 285 ms 20728 KB Output is correct
65 Correct 456 ms 103032 KB Output is correct
66 Correct 287 ms 21496 KB Output is correct