#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmin(a,b) (a)=min((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1000000007;
struct MAT {
MAT(int a = 1, int b = 0, int c = 0, int d = 1)
: a(a), b(b), c(c), d(d) {}
int a, b, c, d;
void init() { a = d = 1; b = c = 0; }
MAT operator * (const MAT &t) const {
return MAT((ll(a)*t.a + ll(b)*t.c)%MOD, (ll(a)*t.b + ll(b)*t.d)%MOD
, (ll(c)*t.a + ll(d)*t.c)%MOD, (ll(c)*t.b + ll(d)*t.d)%MOD);
}
void cal(int ps, int pe, int s, int e) {
if(e < 0) return;
if(ps < 0) { init(); return; }
int l = s-pe-1, ct = (e-s)>>1;
a = (l+1)>>1; b = l>>1;
c = (ll(ct)*((l+1)>>1) + 1) % MOD;
d = (ll(ct)*(l>>1) + 1) % MOD;
}
};
struct NOD {
NOD(int si, int ei)
: p(0), l(0), r(0), si(si), ei(ei) {}
NOD *p, *l, *r;
MAT dt, pd;
int si, ei;
void upd() {
pd.init();
if(l) pd = l->pd;
pd = dt * pd;
if(r) pd = r->pd * pd;
}
void rot() {
NOD *c = p, *g = p->p;
if(this == p->l) {
p->l = r;
if(r) r->p = p;
r = p;
} else {
p->r = l;
if(l) l->p = p;
l = p;
}
p->p = this; p = g;
if(g) (g->l == c ? g->l : g->r) = this;
c->upd(); upd();
}
void cal(NOD *x) {
dt.cal(x->si, x->ei, si, ei);
upd();
}
};
struct SPT {
NOD *rt;
void init() {
rt = new NOD(-INF, -INF);
rt->r = new NOD(INF, -1);
rt->r->p = rt;
}
NOD* splay(NOD *x) {
for(NOD *g; x->p;) {
if(!(g = x->p->p)) {
x->rot();
break;
}
((g->l == x->p) == (x->p->l == x) ? x->p : x)->rot();
x->rot();
}
return rt = x;
}
NOD* find(int K) {
NOD *x = rt, *pv, *ret = 0;
for(; x;) {
pv = x;
if(K <= x->si) {
if(!ret || x->si < ret->si) ret = x;
x = x->l;
} else x = x->r;
}
splay(pv);
return splay(ret);
}
NOD* prev(NOD *x) {
if(!x->l) {
for(; x->p->l == x;) x = x->p;
return splay(x->p);
}
x = x->l;
for(; x->r;) x = x->r;
return splay(x);
}
NOD* next(NOD *x) {
if(!x->r) {
for(; x->p->r == x;) x = x->p;
return splay(x->p);
}
x = x->r;
for(; x->l;) x = x->l;
return splay(x);
}
void pop(NOD *x) {
splay(x);
NOD *l = x->l, *r = x->r;
delete x; x = 0;
l->p = r->p = 0; rt = l;
for(x = r; x->l;) x = x->l;
for(; l->r;) l = l->r;
x->cal(l);
for(NOD *p = x; p; p = p->p) p->upd();
l->r = r; r->p = l;
for(; l; l = l->p) l->upd();
splay(x);
}
void pop(int si, int ei) { pop(find(si)); }
NOD* push(int si, int ei) {
find(ei+1);
NOD *x = new NOD(si, ei), *l = rt->l;
rt->l = 0; x->l = l; l->p = x;
x->r = rt; rt->p = x;
rt->cal(x);
for(; l->r;) l = l->r;
x->cal(l);
for(NOD *p = l; p; p = p->p) p->upd();
splay(l);
return splay(x);
}
MAT get() { return rt->pd; }
} spt;
const int MAXN = 100005;
set<pii> CH;
set<pii>::iterator get(int X) { return prev(CH.upper_bound({X, INF})); }
bool has(int X) {
auto it = CH.upper_bound({X, INF});
if(CH.begin() == it) return false;
int s, e; tie(s, e) = *prev(it);
return s <= X && X <= e && (s&1) == (X&1);
}
void insert(int s, int e) {
bool flag = CH.insert({s, e}).second;
if(flag) spt.push(s, e);
}
void erase(set<pii>::iterator it) {
spt.pop(it->first, it->second);
CH.erase(it);
}
void push(int X) {
if(X < 1) return;
if(1 == X) X = 2;
if(!has(X)) {
if(has(X-1) && !has(X+1)) {
auto it = get(X-1);
int s, e; tie(s, e) = *it;
erase(it);
e -= 2;
if(s <= e) insert(s, e);
push(X+1);
return;
}
if(!has(X-1) && has(X+1)) {
auto it = get(X+1);
int s, e; tie(s, e) = *it;
erase(it);
push(e+1);
return;
}
if(has(X-1) && has(X+1)) {
auto it = get(X);
int s, e; tie(s, e) = *it;
erase(it);
insert(s, X-1);
push(e+1);
return;
}
int s = X, e = X;
if(has(X-2)) {
auto it = get(X-2);
int p, q; tie(p, q) = *it;
erase(it);
s = p;
}
if(has(X+2)) {
auto it = get(X+2);
int p, q; tie(p, q) = *it;
erase(it);
e = q;
}
insert(s, e);
return;
}
auto it = get(X);
int s, e; tie(s, e) = *it;
erase(it);
if(s+1 < X) insert(s+1, X-1);
push(e+1);
push(s-2);
}
int N;
ll getAns() {
if(CH.empty()) return 0;
MAT mat = spt.get();
int s, e; tie(s, e) = *CH.begin();
ll a = 0, b;
if(1 < s-2) a = (ll(s-4)/2 + 1) % MOD;
b = (1 + ll(s-2)/2 * ((e-s)/2)) % MOD;
ll ret = a*mat.a % MOD;
ret += b*mat.b % MOD;
ret += a*mat.c % MOD;
ret += b*mat.d % MOD;
return ret % MOD;
}
int main() {
ios::sync_with_stdio(false);
spt.init();
cin >> N;
for(int i = 0; i < N; i++) {
int x;
cin >> x;
push(x+1);
printf("%lld\n", getAns());
}
return 0;
}
Compilation message
fib.cpp: In function 'void insert(int, int)':
fib.cpp:100:8: warning: 'pv' may be used uninitialized in this function [-Wmaybe-uninitialized]
splay(pv);
~~~~~^~~~
fib.cpp:92:17: note: 'pv' was declared here
NOD *x = rt, *pv, *ret = 0;
^~
fib.cpp: In function 'void erase(std::set<std::pair<int, int> >::iterator)':
fib.cpp:100:8: warning: 'pv' may be used uninitialized in this function [-Wmaybe-uninitialized]
splay(pv);
~~~~~^~~~
fib.cpp:92:17: note: 'pv' was declared here
NOD *x = rt, *pv, *ret = 0;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
296 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
296 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
595 ms |
14940 KB |
Output is correct |
3 |
Correct |
639 ms |
12168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
296 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
595 ms |
14940 KB |
Output is correct |
26 |
Correct |
639 ms |
12168 KB |
Output is correct |
27 |
Correct |
158 ms |
4696 KB |
Output is correct |
28 |
Correct |
263 ms |
7624 KB |
Output is correct |
29 |
Correct |
60 ms |
632 KB |
Output is correct |
30 |
Correct |
260 ms |
6888 KB |
Output is correct |
31 |
Correct |
132 ms |
1400 KB |
Output is correct |
32 |
Correct |
206 ms |
5240 KB |
Output is correct |
33 |
Correct |
160 ms |
1912 KB |
Output is correct |
34 |
Correct |
150 ms |
1272 KB |
Output is correct |
35 |
Correct |
276 ms |
2552 KB |
Output is correct |
36 |
Correct |
316 ms |
2040 KB |
Output is correct |
37 |
Correct |
310 ms |
1572 KB |
Output is correct |
38 |
Correct |
605 ms |
14980 KB |
Output is correct |
39 |
Correct |
89 ms |
808 KB |
Output is correct |
40 |
Correct |
134 ms |
1016 KB |
Output is correct |
41 |
Correct |
472 ms |
1964 KB |
Output is correct |
42 |
Correct |
597 ms |
14708 KB |
Output is correct |
43 |
Correct |
158 ms |
2424 KB |
Output is correct |
44 |
Correct |
232 ms |
2388 KB |
Output is correct |
45 |
Correct |
311 ms |
3472 KB |
Output is correct |
46 |
Correct |
252 ms |
2044 KB |
Output is correct |
47 |
Correct |
513 ms |
9976 KB |
Output is correct |
48 |
Correct |
325 ms |
2416 KB |
Output is correct |
49 |
Correct |
468 ms |
3596 KB |
Output is correct |
50 |
Correct |
654 ms |
13688 KB |
Output is correct |