#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
using namespace std;
const int N = 1e5+7;
typedef long long ll;
const ll MOD = 1e9+7;
const int INF = 2e9;
const int MAXA = 1e9+1e6;
int A[N];
ll X(int n){
if (n<0){
return 0;
}
return (n+2)/2;
}
struct tnode{
tnode *l,*r;
int mi, mx, sum;
ll dp[2][2];
tnode(int _sum){
sum = _sum;
l = nullptr;
r = nullptr;
memset(dp,0,sizeof(dp));
mi = INF, mx = -INF;
}
void merge(tnode &pl,tnode &pr){
sum = pl.sum+pr.sum;
mi = min(pl.mi,pr.mi);
mx = max(pl.mx,pr.mx);
if (pl.sum==0){
for(int l = 0;l<2;l+=1){
for(int r = 0;r<2;r+=1){
dp[l][r] = pr.dp[l][r];
}
}
return;
}
if (pr.sum==0){
for(int l = 0;l<2;l+=1){
for(int r = 0;r<2;r+=1){
dp[l][r] = pl.dp[l][r];
}
}
return;
}
for(int l = 0;l<2;l+=1){
for(int r = 0;r<2;r+=1){
dp[l][r] = (pl.dp[l][0]+pl.dp[l][1])*pr.dp[0][r] +
(pl.dp[l][0]*X(pr.mi-2-pl.mx-1)+pl.dp[l][1]*X(pr.mi-2-(pl.mx-1)-1))%MOD*pr.dp[1][r];
dp[l][r] %= MOD;
}
}
}
void update(int tl,int tr,int pos,int val){
if (tl>pos || tr<pos){
return;
}
if (tl==tr){
sum += val;
memset(dp,0,sizeof(dp));
if (sum==0){
mi = INF, mx = -INF;
}
else{
mi = mx = tl;
dp[0][0] = 1;
if (tl>=2){
dp[1][1] = 1;
}
}
return;
}
if (l==nullptr){
l = new tnode(0);
}
if (r==nullptr){
r = new tnode(0);
}
int tm = (tl+tr)/2;
l->update(tl,tm,pos,val);
r->update(tm+1,tr,pos,val);
merge(*l,*r);
if (l->sum==0){
delete l;
l = nullptr;
}
if (r->sum==0){
delete r;
r = nullptr;
}
}
int get(int tl,int tr,int pos){
if (tl>pos || tr<pos){
return 0;
}
if (tl==tr){
return sum;
}
int tm = (tl+tr)/2;
int ret = 0;
if (l!=nullptr) ret += l->get(tl,tm,pos);
if (r!=nullptr) ret += r->get(tm+1,tr,pos);
return ret;
}
};
void normalize(int *A,int &n){
multiset<int> S;
for(int i = 1;i<=n;i+=1){
S.insert(A[i]);
}
int ptr = 0;
while(1){
bool flag = false;
for(auto it = S.rbegin();it!=S.rend();it = next(it)){
int v = *it;
if (S.count(v-1)){
S.erase(S.lower_bound(v));
S.erase(S.lower_bound(v-1));
S.insert(v+1);
flag = true;
break;
}
else if (v>=2 && S.count(v)>=2){
S.erase(S.lower_bound(v));
S.erase(S.lower_bound(v));
S.insert(v+1);
S.insert(v-2);
flag = true;
break;
}
else if (S.count(v)==2 && v==1){
S.erase(S.lower_bound(1));
S.erase(S.lower_bound(1));
S.insert(0);
S.insert(2);
flag = true;
break;
}
else if (S.count(v)==2 && v==0){
S.erase(S.lower_bound(0));
S.erase(S.lower_bound(0));
S.insert(1);
flag = true;
break;
}
}
if (!flag){
break;
}
}
for(int to:S){
A[++ptr] = to;
}
n = ptr;
}
tnode *root;
ll solve(int &n){
normalize(A,n);
A[0] = -1;
for(int i = 0;i<=n;i+=1){
root->update(0,MAXA,A[i]+1,1);
}
ll ret = 0;
for(int l = 0;l<2;l+=1){
for(int r = 0;r<2;r+=1){
ret += root->dp[l][r];
}
}
ret %= MOD;
for(int i = 0;i<=n;i+=1){
root->update(0,MAXA,A[i]+1,-1);
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
root = new tnode(0);
int sz = 0;
for(int i = 1;i<=n;i+=1){
cin>>A[++sz];
A[sz] -= 1;
cout<<solve(sz)<<'\n';
/* cout<<"GDB ";
for(int i = 1;i<=sz;i+=1){
cout<<A[i]<<' ';
}
cout<<'\n';*/
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
324 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
12 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
324 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
12 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
10 ms |
356 KB |
Output is correct |
18 |
Correct |
12 ms |
476 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
7 ms |
328 KB |
Output is correct |
21 |
Correct |
9 ms |
300 KB |
Output is correct |
22 |
Correct |
9 ms |
328 KB |
Output is correct |
23 |
Correct |
7 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
556 KB |
Output is correct |
2 |
Execution timed out |
4054 ms |
3240 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
324 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
12 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
10 ms |
356 KB |
Output is correct |
18 |
Correct |
12 ms |
476 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
7 ms |
328 KB |
Output is correct |
21 |
Correct |
9 ms |
300 KB |
Output is correct |
22 |
Correct |
9 ms |
328 KB |
Output is correct |
23 |
Correct |
7 ms |
324 KB |
Output is correct |
24 |
Correct |
17 ms |
556 KB |
Output is correct |
25 |
Execution timed out |
4054 ms |
3240 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |