답안 #535855

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535855 2022-03-11T14:39:03 Z cig32 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
58 ms 28564 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
template <typename T>
T Max(T a, T b) {
  if(a < b) return b;
  return a;
}
 
template <typename T>
T Min(T a, T b) {
  if(a < b) return a;
  return b;
}
 
template <typename T>
T Abs(T a) {
  if(a < 0) return -a;
  return a;
}
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
void SetIO() {
#ifdef BLAT
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif // BLAT
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
 
const int MOD = 1e9 + 7;
const int INVMOD = (1e9 + 8) / 2;
const int INF = 1e9;
const int N = 1e5;
const int LOG = 20;
 
pair <int, int> p[5 + N];
ll sp[5 + N];
int n;
 
class RMQ {
  private:
    pair <int, int> r[5 + LOG][5 + N];
    int log[5 + N];
 
  public:
    void Compute_RMQ() {
      log[1] = 0;
      for(int i = 2; i <= n; i++) log[i] = 1 + log[i >> 1];
      for(int i = 1; i <= n; i++) r[0][i] = make_pair(p[i].first, i);
 
      for(int i = 1; i <= log[n]; i++) {
        for(int j = 1; j + (1 << i) - 1 <= n; j++) {
          if(r[i - 1][j].first <= r[i - 1][j + (1 << (i - 1))].first)
            r[i][j] = r[i - 1][j];
          else r[i][j] = r[i - 1][j + (1 << (i - 1))];
 
          //cerr << "i = " << i << "; j = " << j << '\n';
          //cerr << r[i][j].first << " " << r[i][j].second << " ";
        }
        //cerr << '\n';
      }
    }
 
    pair <int, int> Get_Minimum(int from, int to) {
      int lg = log[to - from + 1];
      
      //cerr << "lg = " << lg << '\n';
      //cerr << "r[lg][from].first = " << r[lg][from].first << '\n';
      //cerr << "r[lg][to - (1 << lg) + 1].first = " << r[lg][to - (1 << lg) + 1].first << '\n';
      if(r[lg][from].first <= r[lg][to - (1 << lg) + 1].first)
        return r[lg][from];
      return r[lg][to - (1 << lg) + 1];
    }
};
RMQ rmq;
 
 
ll Solve_From_To(int from, int to, int level = 0) {
  if(from > to) return 0;
  //cerr << from << " " << to << " " << level << '\n';
 
  pair <int, int> minim = rmq.Get_Minimum(from, to);
  //cerr << minim.first << " " << minim.second << '\n';
 
  ll x = (sp[to] - sp[from - 1]) % MOD;
  ll y = (minim.first - level) % MOD;
  // level + 1 + level + 2 + ... + level + h = level * h + h * (h + 1) / 2 = h * ((h + 1) / 2 + level);
 
  x = ((1LL * x * (x + 1)) % MOD) * INVMOD % MOD;
  y = ((1LL * (y + 1) * INVMOD) % MOD + 1LL * level) * y % MOD;
 
  ll rect = (x * y) % MOD;
  //cerr << rect << '\n';
  return (rect + Solve_From_To(from, minim.second - 1, minim.first) % MOD + Solve_From_To(minim.second + 1, to, minim.first) % MOD ) % MOD;
}
 
int main() {
  SetIO();
 
  cin >> n;
 
  for(int i = 1; i <= n; i++) cin >> p[i].first;
  for(int i = 1; i <= n; i++) {
    cin >> p[i].second;
    sp[i] = sp[i - 1] + 1LL * p[i].second;
  }
 
  rmq.Compute_RMQ();
 
  cout << Solve_From_To(1, n);
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19796 KB Output is correct
2 Correct 9 ms 19920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19808 KB Output is correct
2 Correct 9 ms 19796 KB Output is correct
3 Correct 9 ms 19916 KB Output is correct
4 Correct 9 ms 19892 KB Output is correct
5 Correct 11 ms 19908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19916 KB Output is correct
2 Correct 11 ms 19872 KB Output is correct
3 Correct 23 ms 22352 KB Output is correct
4 Correct 33 ms 26748 KB Output is correct
5 Correct 31 ms 24776 KB Output is correct
6 Correct 33 ms 23384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19924 KB Output is correct
2 Correct 12 ms 20692 KB Output is correct
3 Correct 25 ms 24140 KB Output is correct
4 Correct 48 ms 28376 KB Output is correct
5 Correct 43 ms 28564 KB Output is correct
6 Correct 9 ms 19916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19836 KB Output is correct
2 Correct 11 ms 19952 KB Output is correct
3 Correct 13 ms 20728 KB Output is correct
4 Correct 28 ms 24148 KB Output is correct
5 Correct 48 ms 28364 KB Output is correct
6 Correct 45 ms 28504 KB Output is correct
7 Correct 10 ms 19920 KB Output is correct
8 Correct 12 ms 20692 KB Output is correct
9 Correct 32 ms 24212 KB Output is correct
10 Correct 58 ms 28240 KB Output is correct
11 Correct 39 ms 28408 KB Output is correct
12 Correct 12 ms 19924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19920 KB Output is correct
2 Correct 10 ms 19844 KB Output is correct
3 Correct 8 ms 19796 KB Output is correct
4 Correct 9 ms 19908 KB Output is correct
5 Correct 9 ms 19824 KB Output is correct
6 Correct 9 ms 19844 KB Output is correct
7 Correct 9 ms 19908 KB Output is correct
8 Correct 12 ms 19892 KB Output is correct
9 Correct 10 ms 19924 KB Output is correct
10 Correct 10 ms 19948 KB Output is correct
11 Correct 9 ms 19796 KB Output is correct
12 Correct 9 ms 19924 KB Output is correct
13 Correct 10 ms 19904 KB Output is correct
14 Correct 9 ms 19924 KB Output is correct
15 Correct 10 ms 19944 KB Output is correct
16 Correct 9 ms 19876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19796 KB Output is correct
2 Correct 9 ms 19920 KB Output is correct
3 Correct 8 ms 19796 KB Output is correct
4 Correct 9 ms 19920 KB Output is correct
5 Correct 9 ms 19908 KB Output is correct
6 Correct 9 ms 19924 KB Output is correct
7 Correct 9 ms 19824 KB Output is correct
8 Correct 9 ms 19912 KB Output is correct
9 Correct 9 ms 19796 KB Output is correct
10 Correct 9 ms 19920 KB Output is correct
11 Correct 19 ms 22384 KB Output is correct
12 Correct 31 ms 26716 KB Output is correct
13 Correct 32 ms 24828 KB Output is correct
14 Correct 28 ms 23380 KB Output is correct
15 Correct 8 ms 19944 KB Output is correct
16 Correct 12 ms 20708 KB Output is correct
17 Correct 21 ms 24104 KB Output is correct
18 Correct 35 ms 28388 KB Output is correct
19 Correct 38 ms 28488 KB Output is correct
20 Correct 10 ms 19920 KB Output is correct
21 Correct 12 ms 20692 KB Output is correct
22 Correct 23 ms 24204 KB Output is correct
23 Correct 38 ms 28312 KB Output is correct
24 Correct 35 ms 28360 KB Output is correct
25 Correct 8 ms 19796 KB Output is correct
26 Correct 8 ms 19924 KB Output is correct
27 Correct 9 ms 19928 KB Output is correct
28 Correct 9 ms 19936 KB Output is correct
29 Correct 10 ms 19924 KB Output is correct
30 Correct 11 ms 20180 KB Output is correct
31 Correct 11 ms 20252 KB Output is correct
32 Correct 28 ms 21704 KB Output is correct
33 Correct 28 ms 21832 KB Output is correct
34 Correct 33 ms 23500 KB Output is correct
35 Correct 32 ms 23580 KB Output is correct
36 Correct 34 ms 23740 KB Output is correct
37 Correct 35 ms 23684 KB Output is correct
38 Correct 9 ms 19868 KB Output is correct
39 Correct 39 ms 23740 KB Output is correct
40 Correct 36 ms 23820 KB Output is correct
41 Correct 41 ms 24644 KB Output is correct
42 Correct 39 ms 28476 KB Output is correct