답안 #842245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842245 2023-09-02T16:00:59 Z WLZ Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
91 ms 15312 KB
#include <bits/stdc++.h>
using namespace std;

#include <iostream>

/**
 * Compute the greatest common divisor d of a and b together 
 * with the x and y such that ax + by = d using the extended Euclidean algorithm
 * 
 * @param integers a, b whose gcd will be computed, references x, y where values will be stored
 * @returns greatest common divisor of a and b
*/
template<typename T1, typename T2>
T1 extgcd(const T1& a, const T1& b, T2 &x, T2 &y) {
    x = 1; y = 0;
    T1 a1(a), b1(b);
    T2 x1 = 0, y1 = 1;
    while (b1 != 0) {
        T1 q = a1 / b1;
        tie(x, x1) = make_pair(x1, x - q * x1);
        tie(y, y1) = make_pair(y1, y - q * y1);
        tie(a1, b1) = make_pair(b1, a1 - q * b1);
    }
    return a1;
}

/**
 * Modular integer implementation
 * NOT completely tested!
*/
template<const int MOD>
class modint {
    private: int x;

    public:
    modint() : x(0) {}

    template<typename T>
    void set(const T _x, bool raw = false) {
        if (raw) x = _x;
        else {
            x = _x % MOD;
            if (x < 0) x += MOD;
        }
    }

    template<typename T>
    modint(const T &_x, bool raw = false) {
        set(_x, raw);
    }

    template<typename T>
    modint<MOD> &operator=(const T &_x) {
        set(_x);
        return *this;
    }

    modint<MOD> &operator+=(const modint<MOD> &rhs) {
        x += rhs.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }
        
    friend modint<MOD> operator+(modint<MOD> lhs, const modint<MOD> &rhs) {
        lhs += rhs;
        return lhs;
    }

    modint<MOD> &operator-=(const modint<MOD> &rhs) {
        x -= rhs.x;
        if (x < 0) x += MOD;
        return *this;
    }
        
    friend modint<MOD> operator-(modint<MOD> lhs, const modint<MOD> &rhs) {
        lhs -= rhs;
        return lhs;
    }

    modint<MOD> &operator*=(const modint<MOD> &rhs) {
        x = (unsigned long long) x * rhs.x % MOD;
        return *this;
    }

    friend modint<MOD> operator*(modint<MOD> lhs, const modint<MOD> &rhs) {
        lhs *= rhs;
        return lhs;
    }

    modint<MOD> inv() const {
        modint x1, y1;
        extgcd(x, MOD, x1, y1);
        return move(x1);
    }

    modint<MOD> &operator/=(const modint<MOD> &rhs) {
        operator*=(rhs.inv());
        return *this;
    }

    friend modint<MOD> operator/(modint<MOD> lhs, const modint<MOD> &rhs) {
        lhs /= rhs;
        return lhs;
    }

    modint<MOD> &operator++() {
        operator+=(1);
        return *this;
    }

    modint<MOD> operator++(int) {
        modint<MOD> old = *this;
        operator++();
        return old;
    }

    modint<MOD> &operator--() {
        operator-=(1);
        return *this;
    }

    modint<MOD> operator--(int) {
        modint<MOD> old = *this;
        operator--();
        return old;
    }

    template<typename T>
    operator T() const {
        return x;
    }

    int val() const {
        return x;
    }
};

template<const int MOD, typename T>
modint<MOD> pow(modint<MOD> a, T b) {
    modint<MOD> ans(1, true);
    while (b != 0) {
        if (b & 1) ans *= a;
        a *= a;
        b >>= 1;
    }
    return ans;
}

template<const int MOD>
std::istream &operator>>(std::istream &is, modint<MOD> &x) {
    unsigned int _x;
    is >> _x;
    x.set(_x, true);
    return is;
}

template<const int MOD>
std::ostream &operator<<(std::ostream &os, const modint<MOD> &x) {
    os << x.val();
    return os;
}

using mint1000000007 = modint<1000000007>;
using mint998244353 = modint<998244353>;

using mint = mint1000000007;

mint f(mint h, mint w) {
    return h * (h + 1) * w * (w + 1) / 4;
}

class dsu {
  private:
    vector<int> p, rank;
    vector<mint> sz;
  public:
    dsu(int n, const vector<mint> &_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];
        } else {
          p[x] = y;
          sz[y] += sz[x];
          if (rank[x] == rank[y]) {
            rank[y]++;
          }
        }
      }
    }

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<mint> h(n), w(n);
  map<int, vector<mint>, greater<int> > 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);
  mint ans = 0;
  for (auto& p : mp) {
    for (auto& x : p.second) {
      b[x] = true;
      if (x.val() > 0 && b[x - 1] && !g.same_set(x, x - 1)) {
        ans -= f(p.first, g.st_sz(x - 1));
        g.connect(x, x - 1);
      }
      if (x.val() < n - 1 && b[x + 1] && !g.same_set(x, x + 1)) {
        ans -= f(p.first, g.st_sz(x + 1));
        g.connect(x, x + 1);
      }
      ans += f(p.first, g.st_sz(x));
    }
  }
  cout << ans << '\n';
  return 0;
}

Compilation message

fancyfence.cpp: In instantiation of 'modint<MOD> modint<MOD>::inv() const [with int MOD = 1000000007]':
fancyfence.cpp:97:24:   required from 'modint<MOD>& modint<MOD>::operator/=(const modint<MOD>&) [with int MOD = 1000000007]'
fancyfence.cpp:102:13:   required from 'modint<1000000007> operator/(modint<1000000007>, const modint<1000000007>&)'
fancyfence.cpp:169:40:   required from here
fancyfence.cpp:93:23: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
   93 |         return move(x1);
      |                       ^
fancyfence.cpp:93:23: note: remove 'std::move' call
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 11 ms 2140 KB Output is correct
4 Correct 21 ms 4180 KB Output is correct
5 Correct 21 ms 4164 KB Output is correct
6 Correct 22 ms 4088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 3 ms 664 KB Output is correct
3 Correct 12 ms 2528 KB Output is correct
4 Correct 24 ms 4840 KB Output is correct
5 Correct 25 ms 4816 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 3 ms 836 KB Output is correct
4 Correct 12 ms 2552 KB Output is correct
5 Correct 24 ms 4828 KB Output is correct
6 Correct 35 ms 4960 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 7 ms 1872 KB Output is correct
9 Correct 26 ms 4700 KB Output is correct
10 Correct 46 ms 15196 KB Output is correct
11 Correct 61 ms 15152 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 488 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 13 ms 2140 KB Output is correct
12 Correct 22 ms 3992 KB Output is correct
13 Correct 24 ms 4056 KB Output is correct
14 Correct 21 ms 4088 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 908 KB Output is correct
17 Correct 14 ms 2588 KB Output is correct
18 Correct 26 ms 4716 KB Output is correct
19 Correct 28 ms 4828 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 5 ms 1824 KB Output is correct
22 Correct 22 ms 4656 KB Output is correct
23 Correct 49 ms 15044 KB Output is correct
24 Correct 49 ms 15076 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 528 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 6 ms 1848 KB Output is correct
31 Correct 6 ms 1884 KB Output is correct
32 Correct 15 ms 2464 KB Output is correct
33 Correct 35 ms 7772 KB Output is correct
34 Correct 88 ms 14604 KB Output is correct
35 Correct 30 ms 4560 KB Output is correct
36 Correct 91 ms 15312 KB Output is correct
37 Correct 73 ms 10784 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 63 ms 9724 KB Output is correct
40 Correct 64 ms 15136 KB Output is correct
41 Correct 44 ms 15140 KB Output is correct
42 Correct 47 ms 15148 KB Output is correct