Submission #263528

# Submission time Handle Problem Language Result Execution time Memory
263528 2020-08-13T18:43:12 Z fivefourthreeone Bulldozer (JOI17_bulldozer) C++17
100 / 100
1555 ms 50144 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
 
using namespace std;
 
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> pll;
 
const int KOOSAGA_SZ = 1 << 11;
ll koosagap[2 * KOOSAGA_SZ];
ll koosagas[2 * KOOSAGA_SZ];
ll koosagam[2 * KOOSAGA_SZ];
ll koosagat[2 * KOOSAGA_SZ];
 
void upd(int idx, ll val) {
  idx += KOOSAGA_SZ;
  koosagat[idx] = val;
  koosagap[idx] = val;
  koosagas[idx] = val;
  koosagam[idx] = val;
  while(idx > 1) {
    idx /= 2;
    koosagap[idx] = max(koosagap[2*idx], koosagat[2*idx] + koosagap[2*idx+1]);
    koosagas[idx] = max(koosagas[2*idx+1], koosagat[2*idx+1] + koosagas[2*idx]);
    koosagat[idx] = koosagat[2*idx] + koosagat[2*idx+1];
    koosagam[idx] = max(koosagam[2*idx], koosagam[2*idx+1]);
    koosagam[idx] = max(koosagam[idx], koosagas[2*idx] + koosagap[2*idx+1]);
  }
}
 
struct state {
  int x, y, v, i;
};
bool operator<(state a, state b) {
  return pii(a.x, a.y) < pii(b.x, b.y);
}
 
struct event {
  ll dx, dy;
  int i, j;
  event(){}
  event(ll a, ll b, int c, int d) {
    ll gcd = __gcd(a, b);
    a /= gcd;
    b /= gcd;
    if(a < 0 || (a == 0 && b < 0)) {
      a = -a;
      b = -b;
    }
    dx=a;
    dy=b;
    i=c;
    j=d;
  }
};
bool operator<(event a, event b) {
  ll ret = a.dx * b.dy - a.dy * b.dx;
  return ret > 0;
}
 
state l[KOOSAGA_SZ];
int loc[KOOSAGA_SZ]; // item i should be at loc[i]
int n;
 
int par[KOOSAGA_SZ];
int lastid[KOOSAGA_SZ];
int seen[KOOSAGA_SZ];
int currid;
int find(int x) {
  if(lastid[x] != currid) {
    lastid[x] = currid;
    par[x] = x;
  }
  return par[x] == x ? x : par[x] = find(par[x]);
}
void merge(int x, int y) {
  par[find(x)] = find(y);
}
 
bool locsort(int a, int b) {
  return loc[a] < loc[b];
}
void realswap(int a, int b) {
  assert(l[loc[a]].i == a);
  assert(l[loc[b]].i == b);
  swap(l[loc[a]], l[loc[b]]);
  swap(loc[a], loc[b]);
  upd(loc[a], l[loc[a]].v);
  upd(loc[b], l[loc[b]].v);
}
void solve() {
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> l[i].x >> l[i].y >> l[i].v;
    l[i].i = i;
  }
  vector<event> v;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < i; j++) {
      event e(l[j].x - l[i].x, l[j].y - l[i].y, i, j);
      v.push_back(e);
    }
  }
  sort(l, l+n);
  sort(all(v));
  for(int i = 0; i < n; i++) {
    loc[l[i].i] = i;
  }
  for(int i = 0; i < n; i++) assert(loc[l[i].i] == i);
  for(int i = 0; i < n; i++) {
    upd(i, l[i].v);
  }
  ll ret = koosagam[1];
  for(int i = 0; i < sz(v);) {
    currid++;
    int j = i+1;
    while(j < sz(v) && v[i].dx == v[j].dx && v[i].dy == v[j].dy) {
      j++;
    }
    for(int k = i; k < j; k++) {
        //cout<<v[k].i<<" "<<v[k].j<<"\n";
      merge(v[k].i, v[k].j);
    }
    map<int, vector<int>> dp;
    for(int k = i; k < j; k++) {
      if(seen[v[k].i] != currid)  {
        seen[v[k].i] = currid;
        dp[find(v[k].i)].push_back(v[k].i);
      }
      if(seen[v[k].j] != currid)  {
        seen[v[k].j] = currid;
        dp[find(v[k].j)].push_back(v[k].j);
      }
    }
    for(auto out: dp) {
      sort(all(out.second), locsort);
      for(int k: out.second) {
          //cout<<k<<" ";
      }
      //cout<<"\n";
      int lhs = 0;
      int rhs = sz(out.second)-1;
      while(lhs < rhs) {
        realswap(out.second[lhs++], out.second[rhs--]);
      }
    }
    ret = max(ret, koosagam[1]);
    i = j;
  }
  cout << ret << "\n";
}
 
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}

Compilation message

bulldozer.cpp: In function 'void solve()':
bulldozer.cpp:158:15: warning: unused variable 'k' [-Wunused-variable]
  158 |       for(int k: out.second) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 704 KB Output is correct
2 Correct 1 ms 704 KB Output is correct
3 Correct 1 ms 704 KB Output is correct
4 Correct 1 ms 704 KB Output is correct
5 Correct 1 ms 704 KB Output is correct
6 Correct 1 ms 704 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 704 KB Output is correct
10 Correct 1 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 4 ms 680 KB Output is correct
5 Correct 5 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 5 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 5 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 5 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 4 ms 704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 4 ms 680 KB Output is correct
5 Correct 5 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 5 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 5 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 5 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 4 ms 704 KB Output is correct
33 Correct 1487 ms 49708 KB Output is correct
34 Correct 1502 ms 49836 KB Output is correct
35 Correct 1503 ms 49836 KB Output is correct
36 Correct 1523 ms 49772 KB Output is correct
37 Correct 1522 ms 49860 KB Output is correct
38 Correct 1485 ms 49964 KB Output is correct
39 Correct 1511 ms 50092 KB Output is correct
40 Correct 1528 ms 49840 KB Output is correct
41 Correct 1520 ms 49964 KB Output is correct
42 Correct 1512 ms 49836 KB Output is correct
43 Correct 1446 ms 49836 KB Output is correct
44 Correct 1430 ms 49836 KB Output is correct
45 Correct 1429 ms 49964 KB Output is correct
46 Correct 1427 ms 49964 KB Output is correct
47 Correct 1439 ms 49836 KB Output is correct
48 Correct 1449 ms 49836 KB Output is correct
49 Correct 1461 ms 49836 KB Output is correct
50 Correct 1468 ms 49836 KB Output is correct
51 Correct 1458 ms 49964 KB Output is correct
52 Correct 1451 ms 49836 KB Output is correct
53 Correct 1460 ms 49836 KB Output is correct
54 Correct 1472 ms 49836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 4 ms 680 KB Output is correct
5 Correct 5 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 5 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 5 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 5 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 4 ms 704 KB Output is correct
33 Correct 1487 ms 49708 KB Output is correct
34 Correct 1502 ms 49836 KB Output is correct
35 Correct 1503 ms 49836 KB Output is correct
36 Correct 1523 ms 49772 KB Output is correct
37 Correct 1522 ms 49860 KB Output is correct
38 Correct 1485 ms 49964 KB Output is correct
39 Correct 1511 ms 50092 KB Output is correct
40 Correct 1528 ms 49840 KB Output is correct
41 Correct 1520 ms 49964 KB Output is correct
42 Correct 1512 ms 49836 KB Output is correct
43 Correct 1446 ms 49836 KB Output is correct
44 Correct 1430 ms 49836 KB Output is correct
45 Correct 1429 ms 49964 KB Output is correct
46 Correct 1427 ms 49964 KB Output is correct
47 Correct 1439 ms 49836 KB Output is correct
48 Correct 1449 ms 49836 KB Output is correct
49 Correct 1461 ms 49836 KB Output is correct
50 Correct 1468 ms 49836 KB Output is correct
51 Correct 1458 ms 49964 KB Output is correct
52 Correct 1451 ms 49836 KB Output is correct
53 Correct 1460 ms 49836 KB Output is correct
54 Correct 1472 ms 49836 KB Output is correct
55 Correct 1518 ms 49964 KB Output is correct
56 Correct 1507 ms 49836 KB Output is correct
57 Correct 1489 ms 49860 KB Output is correct
58 Correct 1492 ms 49836 KB Output is correct
59 Correct 1506 ms 49836 KB Output is correct
60 Correct 1523 ms 49836 KB Output is correct
61 Correct 1498 ms 49836 KB Output is correct
62 Correct 1504 ms 49836 KB Output is correct
63 Correct 1506 ms 49836 KB Output is correct
64 Correct 1511 ms 49872 KB Output is correct
65 Correct 1498 ms 49840 KB Output is correct
66 Correct 1504 ms 49964 KB Output is correct
67 Correct 1481 ms 49980 KB Output is correct
68 Correct 1490 ms 49976 KB Output is correct
69 Correct 1487 ms 49852 KB Output is correct
70 Correct 1500 ms 49840 KB Output is correct
71 Correct 1505 ms 49964 KB Output is correct
72 Correct 1490 ms 49836 KB Output is correct
73 Correct 1493 ms 49804 KB Output is correct
74 Correct 1511 ms 49840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 704 KB Output is correct
2 Correct 1 ms 704 KB Output is correct
3 Correct 1 ms 704 KB Output is correct
4 Correct 1 ms 704 KB Output is correct
5 Correct 1 ms 704 KB Output is correct
6 Correct 1 ms 704 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 704 KB Output is correct
10 Correct 1 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 704 KB Output is correct
17 Correct 4 ms 704 KB Output is correct
18 Correct 4 ms 704 KB Output is correct
19 Correct 4 ms 680 KB Output is correct
20 Correct 5 ms 704 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 4 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 4 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 4 ms 704 KB Output is correct
37 Correct 5 ms 704 KB Output is correct
38 Correct 4 ms 704 KB Output is correct
39 Correct 5 ms 704 KB Output is correct
40 Correct 4 ms 704 KB Output is correct
41 Correct 4 ms 704 KB Output is correct
42 Correct 5 ms 704 KB Output is correct
43 Correct 4 ms 704 KB Output is correct
44 Correct 4 ms 704 KB Output is correct
45 Correct 4 ms 704 KB Output is correct
46 Correct 4 ms 704 KB Output is correct
47 Correct 4 ms 704 KB Output is correct
48 Correct 1487 ms 49708 KB Output is correct
49 Correct 1502 ms 49836 KB Output is correct
50 Correct 1503 ms 49836 KB Output is correct
51 Correct 1523 ms 49772 KB Output is correct
52 Correct 1522 ms 49860 KB Output is correct
53 Correct 1485 ms 49964 KB Output is correct
54 Correct 1511 ms 50092 KB Output is correct
55 Correct 1528 ms 49840 KB Output is correct
56 Correct 1520 ms 49964 KB Output is correct
57 Correct 1512 ms 49836 KB Output is correct
58 Correct 1446 ms 49836 KB Output is correct
59 Correct 1430 ms 49836 KB Output is correct
60 Correct 1429 ms 49964 KB Output is correct
61 Correct 1427 ms 49964 KB Output is correct
62 Correct 1439 ms 49836 KB Output is correct
63 Correct 1449 ms 49836 KB Output is correct
64 Correct 1461 ms 49836 KB Output is correct
65 Correct 1468 ms 49836 KB Output is correct
66 Correct 1458 ms 49964 KB Output is correct
67 Correct 1451 ms 49836 KB Output is correct
68 Correct 1460 ms 49836 KB Output is correct
69 Correct 1472 ms 49836 KB Output is correct
70 Correct 1518 ms 49964 KB Output is correct
71 Correct 1507 ms 49836 KB Output is correct
72 Correct 1489 ms 49860 KB Output is correct
73 Correct 1492 ms 49836 KB Output is correct
74 Correct 1506 ms 49836 KB Output is correct
75 Correct 1523 ms 49836 KB Output is correct
76 Correct 1498 ms 49836 KB Output is correct
77 Correct 1504 ms 49836 KB Output is correct
78 Correct 1506 ms 49836 KB Output is correct
79 Correct 1511 ms 49872 KB Output is correct
80 Correct 1498 ms 49840 KB Output is correct
81 Correct 1504 ms 49964 KB Output is correct
82 Correct 1481 ms 49980 KB Output is correct
83 Correct 1490 ms 49976 KB Output is correct
84 Correct 1487 ms 49852 KB Output is correct
85 Correct 1500 ms 49840 KB Output is correct
86 Correct 1505 ms 49964 KB Output is correct
87 Correct 1490 ms 49836 KB Output is correct
88 Correct 1493 ms 49804 KB Output is correct
89 Correct 1511 ms 49840 KB Output is correct
90 Correct 1555 ms 49868 KB Output is correct
91 Correct 1504 ms 49796 KB Output is correct
92 Correct 1512 ms 49836 KB Output is correct
93 Correct 1487 ms 49824 KB Output is correct
94 Correct 1493 ms 49836 KB Output is correct
95 Correct 1497 ms 49836 KB Output is correct
96 Correct 1507 ms 49868 KB Output is correct
97 Correct 1492 ms 49836 KB Output is correct
98 Correct 1485 ms 49888 KB Output is correct
99 Correct 1495 ms 49836 KB Output is correct
100 Correct 755 ms 49860 KB Output is correct
101 Correct 774 ms 49836 KB Output is correct
102 Correct 766 ms 49832 KB Output is correct
103 Correct 771 ms 49832 KB Output is correct
104 Correct 766 ms 49836 KB Output is correct
105 Correct 969 ms 49904 KB Output is correct
106 Correct 949 ms 49844 KB Output is correct
107 Correct 950 ms 49816 KB Output is correct
108 Correct 951 ms 49836 KB Output is correct
109 Correct 948 ms 49836 KB Output is correct
110 Correct 1118 ms 49964 KB Output is correct
111 Correct 1121 ms 49836 KB Output is correct
112 Correct 1134 ms 49964 KB Output is correct
113 Correct 1127 ms 49836 KB Output is correct
114 Correct 1134 ms 49836 KB Output is correct
115 Correct 1115 ms 49880 KB Output is correct
116 Correct 1124 ms 49932 KB Output is correct
117 Correct 1135 ms 49836 KB Output is correct
118 Correct 1122 ms 49844 KB Output is correct
119 Correct 1133 ms 49836 KB Output is correct
120 Correct 1 ms 384 KB Output is correct
121 Correct 1 ms 384 KB Output is correct
122 Correct 1402 ms 49832 KB Output is correct
123 Correct 1385 ms 50144 KB Output is correct
124 Correct 1378 ms 49840 KB Output is correct
125 Correct 1366 ms 49836 KB Output is correct
126 Correct 1376 ms 50000 KB Output is correct
127 Correct 1421 ms 49836 KB Output is correct
128 Correct 1378 ms 49860 KB Output is correct
129 Correct 1363 ms 49836 KB Output is correct
130 Correct 1390 ms 49840 KB Output is correct
131 Correct 1376 ms 49836 KB Output is correct
132 Correct 1383 ms 49800 KB Output is correct
133 Correct 1367 ms 49840 KB Output is correct