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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| #include <bits/stdc++.h> using namespace std;
#define Getmod
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
const int maxn = 2e5 + 5; const int maxm = maxn << 1;
struct Matrix { Tm a[2][2]; Matrix(void) { a[0][0] = a[0][1] = a[1][0] = a[1][1] = 0; }
void init() { a[0][0] = 0; a[0][1] = a[1][0] = a[1][1] = 1;
}
Matrix operator * (const Matrix &z) const { Matrix res; for(int i = 0; i < 2; ++ i) { for(int j = 0; j < 2; ++ j) { for(int k = 0; k < 2; ++ k) { res.a[i][j] += a[i][k] * z.a[k][j]; } } } return res; } Matrix operator + (const Matrix &z) const { Matrix res; for(int i = 0; i < 2; ++ i) { for(int j = 0; j < 2; ++ j) { res.a[i][j] = a[i][j] + z.a[i][j]; } } return res; }
void print() { for(int i = 0; i < 2; ++ i) { for(int j = 0; j < 2; ++ j) { printf("(%d, %d) = %d\n", i, j, a[i][j].z); } } puts("\n ---------- \n"); }
}Fir, tmp;
struct Node { Matrix tag, mt; bool empty() { return (bool)(tag.a[0][0] == 1 && tag.a[1][1] == 1 && tag.a[0][1] + tag.a[1][0] == 0); } }t[maxn << 2];
Matrix ksm(Matrix a,int mi) { Matrix res; res.a[0][0] = res.a[1][1] = 1; while(mi) { if(mi & 1) res = res * a; mi >>= 1; a = a * a; } return res; }
int v[maxn];
struct Seg { #define ls (p << 1) #define rs (p << 1 | 1) #define mid ((l + r) >> 1)
void pushup(int p) { t[p].mt = t[ls].mt + t[rs].mt; }
void build(int p,int l,int r) { t[p].tag.a[0][0] = t[p].tag.a[1][1] = 1; t[p].tag.a[0][1] = t[p].tag.a[1][0] = 0; if(l == r) return t[p].mt = Fir * ksm(tmp, v[l] - 1), void(); build(ls, l, mid), build(rs, mid + 1, r); pushup(p); }
void Add(int p,const Matrix& c) { t[p].mt = t[p].mt * c; t[p].tag = t[p].tag * c; }
void pushdown(int p) { if(t[p].empty()) return ; Add(ls, t[p].tag), Add(rs, t[p].tag); t[p].tag.a[0][0] = t[p].tag.a[1][1] = 1; t[p].tag.a[0][1] = t[p].tag.a[1][0] = 0; }
void change(int p,int l,int r,int ll,int rr, const Matrix& c) { if(ll <= l && r <= rr) return Add(p, c); pushdown(p); if(ll <= mid) change(ls, l, mid, ll, rr, c); if(mid < rr) change(rs, mid + 1, r, ll, rr, c); pushup(p); }
Matrix Ask(int p,int l,int r,int ll,int rr) { if(ll <= l && r <= rr) return t[p].mt; pushdown(p); Matrix res; if(ll <= mid) res = res + Ask(ls, l, mid, ll, rr); if(mid < rr) res = res + Ask(rs, mid + 1, r, ll, rr); pushup(p); return res; }
}T;
int n, m;
signed main() {
int i, j; Fir.a[0][0] = Fir.a[0][1] = 1; tmp.init();
r1(n, m); for(i = 1; i <= n; ++ i) r1(v[i]); T.build(1, 1, n); while(m --) { int opt, l, r, x; r1(opt, l, r); if(opt == 1) { r1(x); T.change(1, 1, n, l, r, ksm(tmp, x)); } else { printf("%d\n", T.Ask(1, 1, n, l, r).a[0][0].z); } } return 0; }
|