#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
// snuke's mod int
template<ll mod>
struct modint{
ll x;
modint(ll x=0):x((x%mod+mod)%mod){}
modint& operator+=(const modint a){if((x+=a.x)>=mod)x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod)x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n/=2;
}
return res;
}
};
using mint=modint<1000000007>;
struct N{
int len;
mint wys;
N(int _len=0,mint _wys=0):
len(_len),wys(_wys){}
};
N op(N l,N r){
if(l.len!=r.len){
return l.len>r.len?l:r;
}
l.wys+=r.wys;
return l;
}
N e(){
return {0,0};
}
int op1(int l,int r){
return l+r;
}
int e1(){
return 0;
}
template<class S,S (*op)(S,S),S (*e)()>
struct segtree{
int n;
vec(S) seg;
segtree(int _n){
init(_n);
}
void init(int _n){
n=1;
while(n<=_n){
n*=2;
}
seg=vec(S)(2*n);
n=_n;
}
S prod(int node,int l,int r,int _l,int _r){
if(l>=_r or r<=_l){
return e();
}
if(_l<=l and r<=_r){
return seg[node];
}
int m=(l+r)>>1;
return op(prod(node*2,l,m,_l,_r),prod(node*2+1,m,r,_l,_r));
}
S prod(int l,int r){
return prod(1,0,n,l,r);
}
void set(int node,int l,int r,int pos,const S&x){
if(l==r-1){
seg[node]=x;
return;
}
int m=(l+r)/2;
if(pos<m){
set(node*2,l,m,pos,x);
}else{
set(node*2+1,m,r,pos,x);
}
seg[node]=op(seg[node*2],seg[node*2+1]);
}
void set(int pos,const S&x){
set(1,0,n,pos,x);
}
S get(int pos){
return prod(1,0,n,pos,pos+1);
}
};
signed main(){
_3phCa4T;
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
vi tmp=a;
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()),tmp.end());
const int m=sz(tmp);
rep(i,n){
a[i]=lower_bound(tmp.begin(), tmp.end(),a[i])-tmp.begin();
}
vec(N) rbts(n);
segtree<N,op,e> seg(m);
int len=0;
per(i,n){
int v=a[i];
N res=seg.prod(v+1,m);
if(res.len==0){
res.len=1;
res.wys=1;
}else{
res.len+=1;
}
seg.set(v,op(res,seg.get(v)));
rbts[i]=res;
len=max(len,res.len+(v>0));
}
segtree<int,op1,e1> seg1(m);
vi dl(n);
{
rep(i,n){
int v=a[i];
dl[i]=seg1.prod(0,v);
seg1.set(v,seg1.get(v)+1);
}
rep(i,m){
seg1.set(i,0);
}
}
vi dr(n);
{
per(i,n){
int v=a[i];
dr[i]=seg1.prod(0,v);
seg1.set(v,seg1.get(v)+1);
}
rep(i,m){
seg1.set(i,0);
}
}
mint wys=0;
rep(i,n){
N now=rbts[i];
int v=a[i];
if(v){
if(now.len==len-1 and !dl[i]){
wys+=now.wys*mint(dl[i])*mint(2).pow(n-now.len-1);
wys+=now.wys*mint(dr[i])*mint(2).pow(n-now.len-1);
}
}else{
if(now.len==len){
wys+=now.wys*mint(2).pow(n-now.len);
}
}
}
print(len,wys.x);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Correct |
246 ms |
15644 KB |
Output is correct |
12 |
Correct |
181 ms |
14840 KB |
Output is correct |
13 |
Correct |
165 ms |
9400 KB |
Output is correct |
14 |
Incorrect |
258 ms |
14928 KB |
Output isn't correct |
15 |
Incorrect |
298 ms |
15876 KB |
Output isn't correct |
16 |
Incorrect |
360 ms |
16796 KB |
Output isn't correct |
17 |
Incorrect |
282 ms |
16868 KB |
Output isn't correct |
18 |
Incorrect |
264 ms |
16852 KB |
Output isn't correct |
19 |
Incorrect |
327 ms |
16920 KB |
Output isn't correct |
20 |
Incorrect |
264 ms |
16840 KB |
Output isn't correct |