Submission #788627

# Submission time Handle Problem Language Result Execution time Memory
788627 2023-07-20T12:20:36 Z WLZ Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
100 ms 16360 KB
#include <bits/stdc++.h>
using namespace std;

const long long MOD = (long long) 1e9 + 7;

class dsu {
  private:
    vector<int> p, rank;
    vector<long long> sz;
  public:
    dsu(int n, const vector<long long> &_sz) : sz(_sz) {
      p.assign(n, -1);
      rank.assign(n, 0);
    }

    int root(int x) {
      if (p[x] == -1) {
        return x;
      }
      return (p[x] = root(p[x]));
    }

    bool same_set(int x, int y) {
      return (root(x) == root(y));
    }

    void connect(int x, int y) {
      x = root(x); y = root(y);
      if (x != y) {
        if (rank[x] > rank[y]) {
          p[y] = x;
          sz[x] = (sz[y] + sz[x]) % MOD;
        } else {
          p[x] = y;
          sz[y] = (sz[x] + sz[y]) % MOD;
          if (rank[x] == rank[y]) {
            rank[y]++;
          }
        }
      }
    }

    long long st_sz(int x) {
      return sz[root(x)];
    }
};

long long modpow(long long b, long long p) {
  if (p == 0) {
    return 1;
  }
  long long ans = modpow((b * b) % MOD, p / 2);
  if (p % 2 == 1) {
    ans = (ans * b) % MOD;
  }
  return ans;
}
 
long long inv(long long x) {
  return modpow(x, MOD - 2);
}
 
long long mul(long long a, long long b) {
  return (a * b) % MOD;
}


long long f(long long h, long long w) {
  return mul(h, mul(h + 1, mul(w, mul(w + 1, inv(4)))));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> h(n), w(n);
  map<long long, vector<long long>, greater<long long> > mp;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    mp[h[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  dsu g(n, w);
  vector<bool> b(n, false);
  long long ans = 0;
  for (auto& p : mp) {
    for (auto& x : p.second) {
      b[x] = true;
      if (x > 0 && b[x - 1] && !g.same_set(x, x - 1)) {
        ans = ((ans - f(p.first, g.st_sz(x - 1))) % MOD + MOD) % MOD;
        g.connect(x, x - 1);
      }
      if (x < n - 1 && b[x + 1] && !g.same_set(x, x + 1)) {
        ans = ((ans - f(p.first, g.st_sz(x + 1))) % MOD + MOD) % MOD;
        g.connect(x, x + 1);
      }
      ans = (ans + f(p.first, g.st_sz(x))) % MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 24 ms 2872 KB Output is correct
4 Correct 46 ms 5580 KB Output is correct
5 Correct 46 ms 5568 KB Output is correct
6 Correct 46 ms 5488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 7 ms 988 KB Output is correct
3 Correct 25 ms 3284 KB Output is correct
4 Correct 49 ms 6140 KB Output is correct
5 Correct 52 ms 6312 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 6 ms 852 KB Output is correct
4 Correct 27 ms 3284 KB Output is correct
5 Correct 50 ms 6088 KB Output is correct
6 Correct 52 ms 6304 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 7 ms 1868 KB Output is correct
9 Correct 30 ms 5456 KB Output is correct
10 Correct 72 ms 16160 KB Output is correct
11 Correct 75 ms 16308 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 452 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 23 ms 2896 KB Output is correct
12 Correct 46 ms 5632 KB Output is correct
13 Correct 48 ms 5580 KB Output is correct
14 Correct 45 ms 5516 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 888 KB Output is correct
17 Correct 25 ms 3276 KB Output is correct
18 Correct 50 ms 6092 KB Output is correct
19 Correct 51 ms 6256 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 7 ms 1876 KB Output is correct
22 Correct 30 ms 5456 KB Output is correct
23 Correct 75 ms 16272 KB Output is correct
24 Correct 71 ms 16288 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 328 KB Output is correct
30 Correct 8 ms 1740 KB Output is correct
31 Correct 12 ms 1888 KB Output is correct
32 Correct 27 ms 3404 KB Output is correct
33 Correct 46 ms 8324 KB Output is correct
34 Correct 100 ms 15564 KB Output is correct
35 Correct 65 ms 6188 KB Output is correct
36 Correct 94 ms 16360 KB Output is correct
37 Correct 85 ms 12096 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 85 ms 10724 KB Output is correct
40 Correct 85 ms 16080 KB Output is correct
41 Correct 70 ms 16332 KB Output is correct
42 Correct 75 ms 16312 KB Output is correct