序列[CCPC Wannafly WC Day7]

题目链接

链接

题解

显然考虑数对 $(i,j)$ 给哪些数字产生了贡献。

不妨设 $p_i < p_j$(大于的同理),

这一数对给 $(p_i,p_j)$ 这个区间都贡献了 $w=2^{i-1}\times 2^{n-j}$。

因为这两个数选了以后,中间的不能选,两边的随便选。

因此只需维护一个差分数组 $f$,给 $f_{p_i+1}+=w,f_{p_j}-=w$。

那么枚举 $j$,用平衡树找到满足 $i<j$ 且 $p_i<p_j$ 中所有的 $i$ 。

对于 $f_{p_i+1}+=w$,只需要在平衡树上给 $p_i$ 打一个 $+2^{n-j}$ 的标记,最后乘上 $2^{i-1}$ 就可以了。

对于 $f_{p_j} -= w$,只需要在平衡树上记录 $2^{i-1}$ 的和就可以了。

时间复杂度 $O(n\log n)$。

听说有线段树做法?

程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <utility>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define ll long long
#define ull unsigned ll
#define db double
#define lb long db
#define pb push_back
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define com complex<db>
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define cle(x) memset(x,0,sizeof(x))
#define lowbit(x) (x&(-x))
#define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++)
#define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--)

const ll mod=1e9+7;//module
template<typename T>
inline void Add(T &x,T y){x+=y; if(x>=mod) x-=mod;}
template<typename T>
inline void Mul(T &x,T y){(x*=y)%=mod;}
inline ll Pow(ll x,int y)
{
ll ans=1;
for(;y;y>>=1,x=x*x%mod) if(y&1) ans=ans*x%mod;
return ans;
}
inline int read()
{
int x=0; char ch=getchar(); bool f=0;
for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
return f?-x:x;
}
#define N 200005
namespace Treap{
int son[N][2],val[N],siz[N],rnd[N],cnt;
ll t[N],w[N],sz[N],Val[N];
inline int newnode(int v,int va)
{
int u=++cnt;
val[u]=v; siz[u]=1; rnd[u]=rand();
sz[u]=Val[u]=va;
return u;
}
inline void pushtag(int u,ll tag)
{
if(u) Add(t[u],tag),Add(w[u],tag);
}
inline void pushdown(int u)
{
if(!t[u]) return;
fo(j,0,1) pushtag(son[u][j],t[u]);
t[u]=0;
}
inline void pushup(int u)
{
if(!u) return;
siz[u]=siz[son[u][0]]+siz[son[u][1]]+1;
sz[u]=(Val[u]+sz[son[u][0]]+sz[son[u][1]])%mod;
}
int merge(int x,int y)
{
if(!x||!y) return x+y;
pushdown(x); pushdown(y);
if(rnd[x]<rnd[y]) {son[x][1]=merge(son[x][1],y);pushup(x);return x;}
else {son[y][0]=merge(x,son[y][0]);pushup(y);return y;}
}
void split_val(int u,int k,int &x,int &y)
{
if(!u) return void(x=y=0);
pushdown(u);
if(val[u]<=k) x=u,split_val(son[x][1],k,son[x][1],y);
else y=u,split_val(son[y][0],k,x,son[y][0]);
pushup(u);
}
void split_rank(int u,int k,int &x,int &y)
{
if(!u) return void(x=y=0);
pushdown(u);
if(k<=siz[son[u][0]]) y=u,split_rank(son[y][0],k,x,son[y][0]);
else x=u,split_rank(son[x][1],k-siz[son[u][0]]-1,son[x][1],y);
pushup(u);
}
void ins(int &u,int v)
{
int x,y,z;
split_val(u,val[v],x,y);
u=merge(merge(x,v),y);
}
void dfs(int u)
{
if(!u) return;
pushdown(u);
fo(i,0,1) dfs(son[u][i]);
}
void print(int u)
{
if(!u) return;
printf("%d ",u);
fo(i,0,1) print(son[u][i]);
}
}
using namespace Treap;
ll f[N],p2[N],i2[N];
inline void solve(int &u,int l,int r,ll w,int pos)
{
if(l>r) return;
int x=0,y=0,z=0;
split_val(u,l-1,x,y);
split_val(y,r,y,z);
if(y&&siz[y]) f[pos]=(f[pos]+(mod-w)*sz[y])%mod,pushtag(y,w);
u=merge(merge(x,y),z);
}
int rt,n,a[N];
void init()
{
fo(i,1,cnt) siz[i]=son[i][0]=son[i][1]=rnd[i]=sz[i]=t[i]=w[i]=0;
cnt=0;
}
void work()
{
fo(i,1,n) newnode(a[i],p2[i-1]);
rt=1;
int x,y;
fo(i,2,n)
{
solve(rt,1,a[i]-2,p2[n-i],a[i]);
ins(rt,i);
}
dfs(rt);
fo(i,1,n) Add(f[a[i]+1],w[i]*p2[i-1]%mod);
}

int main()
{
FO(A);
srand(19260817+1);
n=read();
p2[0]=1;
fo(i,1,n) a[i]=read(),p2[i]=p2[i-1]*2%mod;
work();
fo(i,1,(n>>1)) swap(a[i],a[n-i+1]);
init();
work();
fo(i,1,n) Add(f[i],f[i-1]),printf("%lld\n",f[i]);
return 0;
}