#pragma GCC optimize "O3,unroll-loops,trapv"
#include <bits/stdc++.h>
#define REP(v, i, j) for (int v = i; v != j; v++)
#define FORI(v) for (auto i : v)
#define FORJ(v) for (auto j : v)
#define OUT(v, a) \
FORI(v) \
cout << i << a;
#define OUTS(v, a, b) \
cout << v.size() << a; \
OUT(v, b)
#define in(a, n) \
REP(i, 0, n) \
cin >> a[i];
#define SORT(v) sort(begin(v), end(v))
#define REV(v) reverse(begin(v), end(v))
#define MEMSET(m) memset(m, -1, sizeof m)
#define pb push_back
#define fi first
#define se second
#define detachIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
bool operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
if(__x.size() != __y.size()) return false;
return std::equal(__x.begin(), __x.end(), __y.begin());
}
typedef pair<long long, long long> pii;
typedef pair<pii, long long> piii;
typedef pair<pii, pii> piiii;
const int MOD = 1e9+7;
struct modint {
long long val;
modint() = default;
modint(int _val): val(_val){}
modint operator+(modint b){ return ((this->val + b.val)%MOD); }
modint operator-(modint b){ return ((MOD + this->val - b.val)%MOD); }
modint operator*(modint b){ return ((this->val * b.val)%MOD); }
modint operator^(int a){
if(a==0)return 1;
if(a==1)return *this;
return (((*this)*(*this))^(a>>1))*((*this)^(a&1));
}
};
modint invert(modint a){
return a^(MOD-2);
}
modint operator/(modint a, modint b){
return a*invert(b);
}
struct Point {
int x, y;
Point(): x(0), y(0) {}
Point(int _x, int _y): x(_x),y(_y) {}
};
bool operator<(Point a, Point b){
return a.x < b.x || (a.x==b.x && a.y<b.y);
}
bool operator==(Point a, Point b){
return a.x==b.x && a.y==b.y;
}
int dot(Point a, Point b){
return a.x*b.x + a.y*b.y;
}
int cross(Point a, Point b){
return (a.x*b.y - a.y*b.x);
}
int mag2(Point a){
return dot(a,a);
}
Point operator-(Point a, Point b){
return {a.x-b.x,a.y-b.y};
}
// Sudut bac (radian)
double angle(Point a, Point b, Point c) {
return acosl(dot(a-b,c-b));
}
struct Line {
int a, b, c;
// membuat a, b, dan c menjadi standar
void normalise(){
if(a<0)a*=-1,b*=-1,c*=-1;
int _g=std::__gcd(std::__gcd(abs(a),abs(b)),abs(c));
if(_g == 0)return;
a/=_g,b/=_g,c/=_g;
}
Line() = default;
Line(Point A, Point B){
a = B.y-A.y;
b = A.x-B.x;
c = -(A.x*a+A.y*b);
this->normalise();
}
Line(int _a, int _b, int _c){
a=_a;
b=_b;
c=_c;
this->normalise();
}
};
bool operator==(Line a, Line b){
a.normalise();
b.normalise();
return (a.a==b.a)&&(a.b==b.b)&&(a.c==b.c);
}
bool intersects(Point a, Point b, Point c, Point d){
bool linear=true;
if(cross(a-b,c-a)==cross(a-b,d-a)&&(cross(a-b,d-a)!=0))return false;
if(cross(a-b,c-a))linear=false;
if(cross(a-b,d-a))linear=false;
swap(a,b);
if(cross(a-b,c-a)==cross(a-b,d-a)&&(cross(a-b,d-a)!=0))return false;
if(cross(a-b,c-a))linear=false;
if(cross(a-b,d-a))linear=false;
swap(a,c);swap(b,d);
if(cross(a-b,c-a)==cross(a-b,d-a)&&(cross(a-b,d-a)!=0))return false;
if(cross(a-b,c-a))linear=false;
if(cross(a-b,d-a))linear=false;
swap(a,b);
if(cross(a-b,c-a)==cross(a-b,d-a)&&(cross(a-b,d-a)!=0))return false;
if(cross(a-b,c-a))linear=false;
if(cross(a-b,d-a))linear=false;
if(!linear)return true;
if(a.x > b.x)swap(a,b);
if(c.x > d.x)swap(c,d);
if(a.x > c.x)swap(a,c),swap(b,d);
if(b.x >= c.x)return true;
return false;
}
vector<Point> convexhull(vector<Point> coordinates){
vector<Point> stck;
REP(i,0,coordinates.size()){
while(stck.size()>=2 && cross(stck.end()[-1]-coordinates[i],stck.end()[-2]-coordinates[i])<0)stck.pop_back();
stck.pb(coordinates[i]);
}
REV(coordinates);
REP(i,0,coordinates.size()){
while(stck.size()>=2 && cross(stck.end()[-1]-coordinates[i],stck.end()[-2]-coordinates[i])<0)stck.pop_back();
stck.pb(coordinates[i]);
}
return stck;
}
vector<int> adj[200100][1];
void _find_scc(int cur, int par, int* disc, int* low, bool* reachable, stack<int>& stck, int graph_id, vector<vector<int>>& scc){
static int t=1;
disc[cur]=low[cur]=t++;
stck.push(cur);
FORI(adj[cur][graph_id]){
// if(i==par)continue;
if(reachable[i]){
assert(disc[i]);
continue;
}
if(disc[i]==0){
_find_scc(i,cur,disc,low,reachable,stck,graph_id,scc);
low[cur]=min(low[cur],low[i]);
} else {
low[cur]=min(low[cur],disc[i]);
}
}
if(disc[cur]==low[cur]){
scc.push_back({});
do{
scc.back().pb(stck.top());
reachable[stck.top()]=true;
stck.pop();
} while(scc.back().back()!=cur);
}
}
vector<vector<int>> find_scc(int cur, int par, int graph_id, bool clear=false){
static int disc[200100];
static int low[200100];
static bool reachable[200100];
static stack<int> stck;
vector<vector<int>> scc;
if(clear){
memset(disc,0,sizeof disc);
memset(low,0,sizeof low);
memset(reachable,0,sizeof reachable);
}
_find_scc(cur,par,disc,low,reachable,stck,graph_id,scc);
}
struct segment_tree {
long long n;
typedef int T;
vector<T> seg;
const T e = 0;
T merge(T a, T b){
if(a==e)return b;
if(b==e)return a;
// Processing here...
}
segment_tree(long long _n): n(_n),seg(4*_n+10,e) /** BUG: GCC does not initialize the elements properly this way */ {
for(auto &i:seg)i=e;
}
void update(long long pos, long long l, long long r, long long idx, T val){
if(r<idx)return;
if(l>idx)return;
if(l==r){
// Merge logic...
return;
}
update(pos*2+1,l,(l+r)/2,idx,val);
update(pos*2+2,((l+r)/2)+1,r,idx,val);
seg[pos]=merge(seg[pos*2+1],seg[pos*2+2]);
}
T query(long long pos, long long l, long long r, long long ql, long long qr){
if(l > qr)return e;
if(ql > r)return e;
if(ql <= l && r <= qr)return seg[pos];
if(l==r)return e;
return merge(query(pos*2+1,l,(l+r)/2,ql,qr),query(pos*2+2,((l+r)/2)+1,r,ql,qr));
}
};
long long count(pii p){
return (((((p.se)*(p.se+1))/2)%MOD)*p.fi) % MOD;
}
int main(){
detachIO;
vector<pii> stck;
vector<pii> vec; // width, height
long long n;cin>>n;
REP(i,0,n){
long long x;cin>>x;vec.pb({0,x});
}
REP(i,0,n){
long long x;cin>>x;vec[i].fi=x;
}
long long sum=0;
long long ans=0;
FORI(vec){
long long cnt=0;
while(stck.size()&&stck.back().se>=i.se)cnt+=stck.back().fi,cnt%=MOD,sum-=count(stck.back()),stck.pop_back(),sum%=MOD;
stck.pb({cnt,i.se});
sum+=count(stck.back());sum%=MOD;
ans+=sum*i.fi;ans%=MOD;
sum-=count(stck.back());sum%=MOD;
stck.pop_back();
ans+=(((((i.se)*(i.se+1))/2)%MOD) * ((((i.fi)*(i.fi+1))/2)%MOD))%MOD;ans%=MOD;
stck.pb({cnt+i.fi,i.se});
sum+=count(stck.back());sum%=MOD;
}
cout<<ans<<'\n';
}
컴파일 시 표준 에러 (stderr) 메시지
fancyfence.cpp: In function 'std::vector<std::vector<int> > find_scc(int, int, int, bool)':
fancyfence.cpp:216:1: warning: no return statement in function returning non-void [-Wreturn-type]
216 | }
| ^
# | 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... |