답안 #719841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719841 2023-04-06T20:39:24 Z bane Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
27 ms 4432 KB
#include<bits/stdc++.h>
using namespace std;
           
using ll = long long;
using db = long double;
using llu = unsigned long long;
using str = string;
           
// pairs
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdb = pair<db,db>;
#define mp make_pair
#define fr first
#define sc second
           
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pll>;
using vpd = V<pdb>;
#define ms0(x) memset(x , 0, sizeof(x))
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
           
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
           
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
           
const int MOD = (int)2019201949; // 998244353;
const int MX = (int)2e5+5;
const ll BIG = 1e18; 
const db PI = acos((db)-1);
using ld = long double;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};

void setIO(string name = "") {
    cin.tie(0)->sync_with_stdio(0); 
    if (sz(name)) {
        freopen((name + ".in").c_str(), "r", stdin); 
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

template <int MOD=1'000'000'007>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
  friend bool operator>=(Modular const& a, Modular const& b) {return a.value >= b.value;}
  friend bool operator>(Modular const& a, Modular const& b) {return a.value >= b.value;}
};
const int mod = (int)1e9 + 7;
ll inv2 = 500000004;
#define xx first
#define yy sc
void solve(){
	int n;
	cin>>n;
	
	vector<ll> h(n+1,0), w(n+1,0);
	for(int i=0;i<n;++i) cin>>h[i];
	for(int i=0;i<n;++i) cin>>w[i];
	n++;
	
	vector<pair<ll,ll>> t;
	t.push_back({0,0});
	
	ll ans=0;
	for(int i=0;i<n;++i) {
		ll w_len=0;
		while(t.back().xx>h[i]) { 
			ll h_prev=max(h[i],t[(int)t.size()-2].xx); 
			ll h_diff=t.back().xx-h_prev;
			w_len=(w_len+t.back().yy)%mod;
			t.pop_back();
			ans+=(h_diff*(h_diff+1)%mod*inv2%mod+h_diff*h_prev%mod)*(w_len*(w_len+1)%mod*inv2%mod)%mod;
			ans%=mod;
		}
		if(t.back().xx==h[i]) {
			t.back().yy=(t.back().yy+w_len+w[i])%mod;
		}else {
			t.push_back({h[i], (w_len+w[i])%mod});
		}
	}
	cout<<ans;
}
 
int main() {
	setIO("");
 // freopen("input.txt", "r", stdin);
// // freopen("output.txt", "w", stdout);
	solve();
    return 0;   
}

Compilation message

fancyfence.cpp: In function 'void setIO(std::string)':
fancyfence.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 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 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 9 ms 1364 KB Output is correct
4 Correct 18 ms 2240 KB Output is correct
5 Correct 17 ms 2112 KB Output is correct
6 Correct 20 ms 2088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 11 ms 1288 KB Output is correct
4 Correct 20 ms 2136 KB Output is correct
5 Correct 23 ms 2072 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 11 ms 1300 KB Output is correct
5 Correct 22 ms 2084 KB Output is correct
6 Correct 23 ms 2088 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 3 ms 988 KB Output is correct
9 Correct 13 ms 1996 KB Output is correct
10 Correct 24 ms 4300 KB Output is correct
11 Correct 26 ms 4432 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 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 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 8 ms 1348 KB Output is correct
12 Correct 16 ms 2048 KB Output is correct
13 Correct 18 ms 2120 KB Output is correct
14 Correct 19 ms 2140 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 12 ms 1392 KB Output is correct
18 Correct 22 ms 2156 KB Output is correct
19 Correct 24 ms 2136 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 980 KB Output is correct
22 Correct 12 ms 2000 KB Output is correct
23 Correct 24 ms 4284 KB Output is correct
24 Correct 22 ms 4288 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 672 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 13 ms 1236 KB Output is correct
33 Correct 18 ms 1364 KB Output is correct
34 Correct 22 ms 2132 KB Output is correct
35 Correct 22 ms 2120 KB Output is correct
36 Correct 22 ms 2068 KB Output is correct
37 Correct 22 ms 2132 KB Output is correct
38 Correct 0 ms 320 KB Output is correct
39 Correct 23 ms 2132 KB Output is correct
40 Correct 27 ms 2136 KB Output is correct
41 Correct 22 ms 2528 KB Output is correct
42 Correct 22 ms 3304 KB Output is correct