Submission #224659

# Submission time Handle Problem Language Result Execution time Memory
224659 2020-04-18T14:30:17 Z quocnguyen1012 Constellation 3 (JOI20_constellation3) C++14
35 / 100
1500 ms 32820 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5, inf = 1e9;

class fenwick_tree
{
public:
  vector<ll> cnt; int n;
  void upd(int i, ll v)
  {
    for(; i <= n; i += i & -i)
      cnt[i] += v;
  }
  void upd(int l, int r, ll v)
  {
    upd(l, v); upd(r + 1, -v);
  }
  ll sum(int i)
  {
    ll res = 0;
    for(; i; i -= i & -i)
      res += cnt[i];
    return res;
  }
  void init(int _n)
  {
    n = _n;
    cnt.assign(n + 5, 0);
  }
}ft;

int le[maxn], ri[maxn], par[maxn], sz[maxn];
int N, M;
ll f[maxn];
int a[maxn];
vector<ar<int, 2>> vec[maxn];
vector<int> same[maxn];
ll res = 0;

void init(void)
{
  ft.init(N);
  fill(sz + 1, sz + 1 + N, 1);
  iota(le + 1, le + 1 + N, 1);
  iota(ri + 1, ri + 1 + N, 1);
  iota(par + 1, par + 1 + N, 1);
}

int finds(int u)
{
  if(par[u] == u) return u;
  return par[u] = finds(par[u]);
}

void merges(int u, int v)
{
  if((u = finds(u)) == (v = finds(v)))
    return;
  if(sz[u] < sz[v]) swap(u, v);
  ft.upd(le[v], ri[v], f[u]);
  ft.upd(le[u], ri[u], f[v]);
  f[u] += f[v];
  sz[u] += sz[v];
  par[v] = u;
  le[u] = min(le[u], le[v]);
  ri[u] = max(ri[u], ri[v]);
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N; init();
  for(int i = 1; i <= N; ++i){
    cin >> a[i];
    same[a[i]].eb(i);
  }
  cin >> M;
  while(M--){
    int x, y, z; cin >> x >> y >> z;
    vec[y - 1].pb({x, z});
    res += z;
  }
  for(int y = 0; y <= N; ++y){
    for(int i : same[y]){
      if(i - 1 >= 1 && a[i - 1] <= a[i])
        merges(i, i - 1);
      if(i + 1 <= N && a[i + 1] <= a[i])
        merges(i, i + 1);
    }
    for(auto v : vec[y]){
      cerr << y << ' ' << v[0] << ' ' << v[1] + ft.sum(v[0]) << '\n';
      f[finds(v[0])] = max(f[finds(v[0])], v[1] + ft.sum(v[0]));
    }
  }
  cout << res - f[finds(1)];
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9856 KB Output is correct
2 Correct 13 ms 9856 KB Output is correct
3 Correct 14 ms 9856 KB Output is correct
4 Correct 13 ms 9856 KB Output is correct
5 Correct 13 ms 9856 KB Output is correct
6 Correct 14 ms 9856 KB Output is correct
7 Correct 13 ms 9856 KB Output is correct
8 Correct 13 ms 9856 KB Output is correct
9 Correct 15 ms 9856 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 13 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 13 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 13 ms 9856 KB Output is correct
16 Correct 13 ms 9856 KB Output is correct
17 Correct 13 ms 9856 KB Output is correct
18 Correct 13 ms 9856 KB Output is correct
19 Correct 13 ms 9856 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9856 KB Output is correct
2 Correct 13 ms 9856 KB Output is correct
3 Correct 14 ms 9856 KB Output is correct
4 Correct 13 ms 9856 KB Output is correct
5 Correct 13 ms 9856 KB Output is correct
6 Correct 14 ms 9856 KB Output is correct
7 Correct 13 ms 9856 KB Output is correct
8 Correct 13 ms 9856 KB Output is correct
9 Correct 15 ms 9856 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 13 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 13 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 13 ms 9856 KB Output is correct
16 Correct 13 ms 9856 KB Output is correct
17 Correct 13 ms 9856 KB Output is correct
18 Correct 13 ms 9856 KB Output is correct
19 Correct 13 ms 9856 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9884 KB Output is correct
23 Correct 37 ms 9984 KB Output is correct
24 Correct 35 ms 10104 KB Output is correct
25 Correct 33 ms 9984 KB Output is correct
26 Correct 35 ms 9984 KB Output is correct
27 Correct 34 ms 9984 KB Output is correct
28 Correct 33 ms 9984 KB Output is correct
29 Correct 35 ms 9984 KB Output is correct
30 Correct 34 ms 9984 KB Output is correct
31 Correct 34 ms 9984 KB Output is correct
32 Correct 34 ms 9984 KB Output is correct
33 Correct 34 ms 9984 KB Output is correct
34 Correct 34 ms 9984 KB Output is correct
35 Correct 36 ms 9984 KB Output is correct
36 Correct 44 ms 9984 KB Output is correct
37 Correct 34 ms 9984 KB Output is correct
38 Correct 34 ms 9984 KB Output is correct
39 Correct 34 ms 9984 KB Output is correct
40 Correct 34 ms 9984 KB Output is correct
41 Correct 34 ms 10016 KB Output is correct
42 Correct 43 ms 9984 KB Output is correct
43 Correct 33 ms 9984 KB Output is correct
44 Correct 34 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9856 KB Output is correct
2 Correct 13 ms 9856 KB Output is correct
3 Correct 14 ms 9856 KB Output is correct
4 Correct 13 ms 9856 KB Output is correct
5 Correct 13 ms 9856 KB Output is correct
6 Correct 14 ms 9856 KB Output is correct
7 Correct 13 ms 9856 KB Output is correct
8 Correct 13 ms 9856 KB Output is correct
9 Correct 15 ms 9856 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 13 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 13 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 13 ms 9856 KB Output is correct
16 Correct 13 ms 9856 KB Output is correct
17 Correct 13 ms 9856 KB Output is correct
18 Correct 13 ms 9856 KB Output is correct
19 Correct 13 ms 9856 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9884 KB Output is correct
23 Correct 37 ms 9984 KB Output is correct
24 Correct 35 ms 10104 KB Output is correct
25 Correct 33 ms 9984 KB Output is correct
26 Correct 35 ms 9984 KB Output is correct
27 Correct 34 ms 9984 KB Output is correct
28 Correct 33 ms 9984 KB Output is correct
29 Correct 35 ms 9984 KB Output is correct
30 Correct 34 ms 9984 KB Output is correct
31 Correct 34 ms 9984 KB Output is correct
32 Correct 34 ms 9984 KB Output is correct
33 Correct 34 ms 9984 KB Output is correct
34 Correct 34 ms 9984 KB Output is correct
35 Correct 36 ms 9984 KB Output is correct
36 Correct 44 ms 9984 KB Output is correct
37 Correct 34 ms 9984 KB Output is correct
38 Correct 34 ms 9984 KB Output is correct
39 Correct 34 ms 9984 KB Output is correct
40 Correct 34 ms 9984 KB Output is correct
41 Correct 34 ms 10016 KB Output is correct
42 Correct 43 ms 9984 KB Output is correct
43 Correct 33 ms 9984 KB Output is correct
44 Correct 34 ms 9984 KB Output is correct
45 Execution timed out 1594 ms 32820 KB Time limit exceeded
46 Halted 0 ms 0 KB -