This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
array<int,3> niz[mxN];
bool bio[mxN];
int pom[mxN];
int add(int a, int b){ a += b; if(a >= mod)a -= mod; return a; }
int sub(int a, int b){ a -= b; if(a < 0)a += mod; return a; }
int mul(int a, int b){ return (1ll * a * b) % mod; }
int power(int a, int b){
if(!b)return 1;
int p = power(a, b / 2);
p = mul(p, p);
if(b & 1)p = mul(p, a);
return p;
}
int inv(int a){
return power(a, mod - 2);
}
int len[mxN];
int par[mxN];
int L[mxN], R[mxN];
int findpar(int x){
return (x == par[x] ? x : par[x] = findpar(par[x]));
}
void unite(int x, int y){
x = findpar(x), y = findpar(y);
if(x == y)return;
par[y] = x; len[x] = add(len[x], len[y]);
L[x] = min(L[x], L[y]); R[x] = max(R[x], R[y]);
}
void init(){
ff(i,1,n){
par[i] = i;
len[i] = niz[i][1];
L[i] = i;
R[i] = i;
}
}
int sum(int a){
return mul(mul(a, a + 1), inv(2));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n)cin >> niz[i][0], pom[i] = niz[i][0];
ff(i,1,n)cin >> niz[i][1], niz[i][2] = i;
init();
sort(niz + 1, niz + n + 1);
int j = n; int rez = 0;
fb(i,n,1){
int h = niz[i][0];
bio[niz[i][2]] = 1;
while(j - 1 >= 1 && niz[j - 1][0] == h){
j -= 1; bio[niz[j][2]] = 1;
}
ff(l,j,i){
int X = niz[l][2];
if(X > 1 && bio[X - 1] == 1)unite(X - 1, X);
if(X < n && bio[X + 1] == 1)unite(X, X + 1);
}
set<int> s;
ff(l,j,i)s.insert(findpar(niz[l][2]));
for(auto c : s){
int l = L[c];
int r = R[c];
ll d = len[c];
int mx = max(pom[l - 1], pom[r + 1]);
int kol = mul(sum(d), sub(sum(h), sum(mx)));
rez = add(rez, kol);
}
i = j;
}
cout << rez << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |