#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const int MOD = 1000000007; int dziel; inline int DOD(int a, int b){ return (a + b >= MOD ? a + b - MOD : a + b); } inline int ODJ(int a, int b){ return (a - b < 0 ? a - b + MOD : a - b); } inline int MNO(int a, int b){ return (1ll*a*b)%MOD; } inline int D2(int a){ return MNO(a, dziel); } int modpow(int a, int e){ int ret = 1; while(e > 0){ if(e&1) ret = (1ll*ret*a)%MOD; a = (1ll*a*a)%MOD; e>>=1; } return ret; } constexpr int N = 300005; int n; int A[N]; int suf[N][3]; int suforg[N][3]; int wei[N][3]; int weiorg[N][3]; int weians[N][3]; int nextpar[N][3]; void wrrrr(){ FOR(j, 3) nextpar[n-1][j] = n-1; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ nextpar[i][j] = ((A[i]-j) % 2 == 0 ? i : nextpar[i+1][0]); } } suf[n-1][0] = 0; suforg[n-1][0] = 1; int odd = 1; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ suforg[i][j] = DOD(MNO(A[i]-j, suforg[i+1][0]), 1); int myodd = odd; if((A[i]-j)%2 == 0) myodd = 0; myodd++; suf[i][j] = D2(ODJ(suforg[i][j], myodd)); } if(A[i] % 2 == 0) odd = 0; odd++; } odd = 1; int odj = 0; wei[n-1][0] = 0; weiorg[n-1][0] = 0; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ weiorg[i][j] = MNO(DOD(weiorg[i+1][0], MNO(2, suforg[i+1][0])), A[i]-j); weians[i][j] = MNO(DOD(weians[i+1][0], suforg[i+1][0]), A[i]-j); int myodd = odd; int myodj = odj; if((A[i]-j)%2 == 0) myodd = 0, myodj = 0; myodj = DOD(myodj, MNO(2, myodd)); myodd++; wei[i][j] = D2(ODJ(weiorg[i][j], myodj)); } if(A[i] % 2 == 0) odd = 0, odj = 0; odj = DOD(odj, MNO(2, odd)); odd++; } } int getsuf(int v, int init, int min){ return DOD(wei[v][min], MNO(suf[v][min], init)); } int getans(int v, int init, int min){ return DOD(weians[v][min], MNO(suforg[v][min], init)); } PII prze(int v, int init, int min){ int ile = nextpar[v][min] - v; return MP(init, init + ile*2); } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); dziel = modpow(2, MOD-2); int q; std::cin >> n >> q; FOR(i, n-1) std::cin >> A[i]; wrrrr(); VI d(4*n); while(q--){ int a, b, lc; std::cin >> a >> b >> lc; a--;b--;lc--; std::fill(d.begin(), d.end(), 0); int ans = 0; int odj = 0; auto set = [&](int i){ assert(i < 4*n); if(d[i]){ odj = DOD(odj, i); } d[i] ^= 1; }; auto add = [&](int lev, int init, int min){ odj = DOD(odj, getsuf(lev, init, min)); auto pp = prze(lev, init, min); for(int i = pp.X; i <= pp.Y; i += 2){ set(i); } }; auto dda = [&](int lev, int init, int min){ ans = DOD(ans, getans(lev, init, min)); }; int pa = a; int dst = 0; while(pa >= 0){ dda(pa, dst, (pa == a ? 0 : 1)); dst++; pa--; } dst = a + b - 2*lc; pa = a; while(pa > lc){ add(pa, dst, (pa == a ? 0 : 1)); pa--; } int pb = b; while(pb > lc){ add(pb, dst, (pb == b ? 0 : 1)); pb--; } add(lc, dst, 2 - (a == lc) - (b == lc)); lc--; dst += 2; while(lc >= 0){ add(lc, dst, 1); lc--; dst += 2; } int mam = 0; for(int i = 4*n-1; i >= 0; --i){ if(d[i]){ odj = DOD(odj, i*mam); mam ^= 1; } } std::cout << (ans-odj+MOD)%MOD << "\n"; } return 0; }
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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const int MOD = 1000000007; int dziel; inline int DOD(int a, int b){ return (a + b >= MOD ? a + b - MOD : a + b); } inline int ODJ(int a, int b){ return (a - b < 0 ? a - b + MOD : a - b); } inline int MNO(int a, int b){ return (1ll*a*b)%MOD; } inline int D2(int a){ return MNO(a, dziel); } int modpow(int a, int e){ int ret = 1; while(e > 0){ if(e&1) ret = (1ll*ret*a)%MOD; a = (1ll*a*a)%MOD; e>>=1; } return ret; } constexpr int N = 300005; int n; int A[N]; int suf[N][3]; int suforg[N][3]; int wei[N][3]; int weiorg[N][3]; int weians[N][3]; int nextpar[N][3]; void wrrrr(){ FOR(j, 3) nextpar[n-1][j] = n-1; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ nextpar[i][j] = ((A[i]-j) % 2 == 0 ? i : nextpar[i+1][0]); } } suf[n-1][0] = 0; suforg[n-1][0] = 1; int odd = 1; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ suforg[i][j] = DOD(MNO(A[i]-j, suforg[i+1][0]), 1); int myodd = odd; if((A[i]-j)%2 == 0) myodd = 0; myodd++; suf[i][j] = D2(ODJ(suforg[i][j], myodd)); } if(A[i] % 2 == 0) odd = 0; odd++; } odd = 1; int odj = 0; wei[n-1][0] = 0; weiorg[n-1][0] = 0; for(int i = n-2; i >= 0; --i){ FOR(j, 3){ weiorg[i][j] = MNO(DOD(weiorg[i+1][0], MNO(2, suforg[i+1][0])), A[i]-j); weians[i][j] = MNO(DOD(weians[i+1][0], suforg[i+1][0]), A[i]-j); int myodd = odd; int myodj = odj; if((A[i]-j)%2 == 0) myodd = 0, myodj = 0; myodj = DOD(myodj, MNO(2, myodd)); myodd++; wei[i][j] = D2(ODJ(weiorg[i][j], myodj)); } if(A[i] % 2 == 0) odd = 0, odj = 0; odj = DOD(odj, MNO(2, odd)); odd++; } } int getsuf(int v, int init, int min){ return DOD(wei[v][min], MNO(suf[v][min], init)); } int getans(int v, int init, int min){ return DOD(weians[v][min], MNO(suforg[v][min], init)); } PII prze(int v, int init, int min){ int ile = nextpar[v][min] - v; return MP(init, init + ile*2); } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); dziel = modpow(2, MOD-2); int q; std::cin >> n >> q; FOR(i, n-1) std::cin >> A[i]; wrrrr(); VI d(4*n); while(q--){ int a, b, lc; std::cin >> a >> b >> lc; a--;b--;lc--; std::fill(d.begin(), d.end(), 0); int ans = 0; int odj = 0; auto set = [&](int i){ assert(i < 4*n); if(d[i]){ odj = DOD(odj, i); } d[i] ^= 1; }; auto add = [&](int lev, int init, int min){ odj = DOD(odj, getsuf(lev, init, min)); auto pp = prze(lev, init, min); for(int i = pp.X; i <= pp.Y; i += 2){ set(i); } }; auto dda = [&](int lev, int init, int min){ ans = DOD(ans, getans(lev, init, min)); }; int pa = a; int dst = 0; while(pa >= 0){ dda(pa, dst, (pa == a ? 0 : 1)); dst++; pa--; } dst = a + b - 2*lc; pa = a; while(pa > lc){ add(pa, dst, (pa == a ? 0 : 1)); pa--; } int pb = b; while(pb > lc){ add(pb, dst, (pb == b ? 0 : 1)); pb--; } add(lc, dst, 2 - (a == lc) - (b == lc)); lc--; dst += 2; while(lc >= 0){ add(lc, dst, 1); lc--; dst += 2; } int mam = 0; for(int i = 4*n-1; i >= 0; --i){ if(d[i]){ odj = DOD(odj, i*mam); mam ^= 1; } } std::cout << (ans-odj+MOD)%MOD << "\n"; } return 0; } |