Submission #331823

# Submission time Handle Problem Language Result Execution time Memory
331823 2020-11-30T10:08:30 Z AlexPop28 Restore Array (RMI19_restore) C++11
100 / 100
537 ms 25220 KB
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

const int INF = (int)1e9 + 10;
    
struct Graph {
  int n;
  vector<bool> inq;
  vector<int> dist, cnt_push;
  vector<vector<pair<int, int>>> adj;
  
  Graph(int n_) : n(n_), inq(n), dist(n, INF), cnt_push(n), adj(n) {}

  void AddEdge(int from, int to, int cost) {
    adj[from].emplace_back(to, cost);
  }

  vector<int> Solve() {
    queue<int> q;
    q.emplace(0);
    dist[0] = 0;
    inq[0] = true;

    while (!q.empty()) {
      int node = q.front(); q.pop();

      for (auto &e : adj[node]) {
        int x, c; tie(x, c) = e;

        if (dist[node] + c < dist[x]) {
          dist[x] = dist[node] + c;
          if (++cnt_push[x] > n) {
            cout << "-1";
            exit(0);
          }

          q.emplace(x);
        }
      }
    }

    return dist;
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m; cin >> n >> m;
  Graph g(1 + n);
  for (int i = 0; i < m; ++i) {
    int l, r, k, val; cin >> l >> r >> k >> val; ++l, ++r;
    if (val == 0) {
      // at most r - l + 1 - k 1s
      k = r - l + 1 - k;
      // pref[r] - pref[l - 1] <= k
      // pref[r] <= pref[l - 1] + k
      g.AddEdge(l - 1, r, k);
    } else {
      // at least r - l + 1 - k + 1 1s
      k = r - l + 1 - k + 1;
      // pref[r] - pref[l - 1] >= k
      // pref[l - 1] <= pref[r] - k
      g.AddEdge(r, l - 1, -k);
    }
  }

  for (int i = 1; i <= n; ++i) {
    // pref[i] <= pref[i - 1] + 1
    g.AddEdge(i - 1, i, 1);
    // pref[i] >= pref[i - 1]
    g.AddEdge(i, i - 1, 0);
  }

  auto pref = g.Solve();
  for (int i = 1; i <= n; ++i) {
    cout << pref[i] - pref[i - 1] << " \n"[i == n];
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 748 KB Output is correct
2 Correct 14 ms 892 KB Output is correct
3 Correct 14 ms 748 KB Output is correct
4 Correct 12 ms 748 KB Output is correct
5 Correct 502 ms 23620 KB Output is correct
6 Correct 537 ms 23880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 748 KB Output is correct
2 Correct 14 ms 892 KB Output is correct
3 Correct 14 ms 748 KB Output is correct
4 Correct 12 ms 748 KB Output is correct
5 Correct 502 ms 23620 KB Output is correct
6 Correct 537 ms 23880 KB Output is correct
7 Correct 13 ms 1004 KB Output is correct
8 Correct 15 ms 1004 KB Output is correct
9 Correct 23 ms 1028 KB Output is correct
10 Correct 11 ms 1004 KB Output is correct
11 Correct 506 ms 24388 KB Output is correct
12 Correct 477 ms 23948 KB Output is correct
13 Correct 17 ms 1004 KB Output is correct
14 Correct 27 ms 1004 KB Output is correct
15 Correct 33 ms 1004 KB Output is correct
16 Correct 103 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 13 ms 748 KB Output is correct
12 Correct 14 ms 892 KB Output is correct
13 Correct 14 ms 748 KB Output is correct
14 Correct 12 ms 748 KB Output is correct
15 Correct 502 ms 23620 KB Output is correct
16 Correct 537 ms 23880 KB Output is correct
17 Correct 13 ms 1004 KB Output is correct
18 Correct 15 ms 1004 KB Output is correct
19 Correct 23 ms 1028 KB Output is correct
20 Correct 11 ms 1004 KB Output is correct
21 Correct 506 ms 24388 KB Output is correct
22 Correct 477 ms 23948 KB Output is correct
23 Correct 17 ms 1004 KB Output is correct
24 Correct 27 ms 1004 KB Output is correct
25 Correct 33 ms 1004 KB Output is correct
26 Correct 103 ms 1132 KB Output is correct
27 Correct 7 ms 1004 KB Output is correct
28 Correct 7 ms 1004 KB Output is correct
29 Correct 8 ms 1004 KB Output is correct
30 Correct 8 ms 1004 KB Output is correct
31 Correct 8 ms 1024 KB Output is correct
32 Correct 8 ms 1004 KB Output is correct
33 Correct 510 ms 25028 KB Output is correct
34 Correct 508 ms 25220 KB Output is correct
35 Correct 8 ms 1004 KB Output is correct
36 Correct 8 ms 1004 KB Output is correct