Submission #308891

# Submission time Handle Problem Language Result Execution time Memory
308891 2020-10-02T07:25:59 Z Matteo_Verz Fancy Fence (CEOI20_fancyfence) C++11
100 / 100
51 ms 30200 KB
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#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;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19968 KB Output is correct
2 Correct 13 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19968 KB Output is correct
2 Correct 12 ms 19968 KB Output is correct
3 Correct 12 ms 19968 KB Output is correct
4 Correct 13 ms 20096 KB Output is correct
5 Correct 13 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19968 KB Output is correct
2 Correct 13 ms 19968 KB Output is correct
3 Correct 25 ms 22776 KB Output is correct
4 Correct 40 ms 28024 KB Output is correct
5 Correct 38 ms 25512 KB Output is correct
6 Correct 38 ms 23672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20096 KB Output is correct
2 Correct 15 ms 20992 KB Output is correct
3 Correct 28 ms 24960 KB Output is correct
4 Correct 45 ms 30072 KB Output is correct
5 Correct 49 ms 30200 KB Output is correct
6 Correct 12 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19964 KB Output is correct
2 Correct 14 ms 19968 KB Output is correct
3 Correct 16 ms 20992 KB Output is correct
4 Correct 30 ms 24992 KB Output is correct
5 Correct 47 ms 30072 KB Output is correct
6 Correct 51 ms 30200 KB Output is correct
7 Correct 13 ms 19968 KB Output is correct
8 Correct 15 ms 20992 KB Output is correct
9 Correct 31 ms 25080 KB Output is correct
10 Correct 45 ms 29944 KB Output is correct
11 Correct 46 ms 30072 KB Output is correct
12 Correct 14 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19968 KB Output is correct
2 Correct 12 ms 19968 KB Output is correct
3 Correct 12 ms 19968 KB Output is correct
4 Correct 13 ms 19968 KB Output is correct
5 Correct 12 ms 19968 KB Output is correct
6 Correct 12 ms 19968 KB Output is correct
7 Correct 12 ms 19968 KB Output is correct
8 Correct 13 ms 19968 KB Output is correct
9 Correct 12 ms 19968 KB Output is correct
10 Correct 13 ms 19968 KB Output is correct
11 Correct 12 ms 19968 KB Output is correct
12 Correct 13 ms 19968 KB Output is correct
13 Correct 13 ms 19968 KB Output is correct
14 Correct 12 ms 19968 KB Output is correct
15 Correct 13 ms 19968 KB Output is correct
16 Correct 12 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19968 KB Output is correct
2 Correct 13 ms 19968 KB Output is correct
3 Correct 12 ms 19968 KB Output is correct
4 Correct 12 ms 19968 KB Output is correct
5 Correct 12 ms 19968 KB Output is correct
6 Correct 12 ms 19968 KB Output is correct
7 Correct 12 ms 19968 KB Output is correct
8 Correct 13 ms 19968 KB Output is correct
9 Correct 12 ms 19968 KB Output is correct
10 Correct 12 ms 19968 KB Output is correct
11 Correct 25 ms 22776 KB Output is correct
12 Correct 40 ms 28024 KB Output is correct
13 Correct 40 ms 25592 KB Output is correct
14 Correct 39 ms 23672 KB Output is correct
15 Correct 14 ms 19968 KB Output is correct
16 Correct 16 ms 20992 KB Output is correct
17 Correct 31 ms 24952 KB Output is correct
18 Correct 46 ms 30072 KB Output is correct
19 Correct 48 ms 30200 KB Output is correct
20 Correct 13 ms 19968 KB Output is correct
21 Correct 19 ms 20992 KB Output is correct
22 Correct 31 ms 25088 KB Output is correct
23 Correct 44 ms 29944 KB Output is correct
24 Correct 45 ms 30072 KB Output is correct
25 Correct 12 ms 19968 KB Output is correct
26 Correct 12 ms 19968 KB Output is correct
27 Correct 13 ms 19968 KB Output is correct
28 Correct 12 ms 19968 KB Output is correct
29 Correct 13 ms 19968 KB Output is correct
30 Correct 15 ms 20288 KB Output is correct
31 Correct 16 ms 20480 KB Output is correct
32 Correct 31 ms 21752 KB Output is correct
33 Correct 28 ms 21884 KB Output is correct
34 Correct 42 ms 23672 KB Output is correct
35 Correct 41 ms 23672 KB Output is correct
36 Correct 44 ms 23800 KB Output is correct
37 Correct 43 ms 23800 KB Output is correct
38 Correct 12 ms 19968 KB Output is correct
39 Correct 44 ms 23800 KB Output is correct
40 Correct 42 ms 23928 KB Output is correct
41 Correct 43 ms 25080 KB Output is correct
42 Correct 45 ms 30072 KB Output is correct