#include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <unordered_map> #include <memory> using namespace std; const int MAXN = 500000; const int MAXQ = 500000; const long long MOD = 1000000007; const long long INV2 = 500000004; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long A[MAXN + 1]; int a1[MAXN], b1[MAXN], w1[MAXN]; int order[MAXN]; long long ile[MAXN + 1]; long long sumdyst[MAXN + 1]; long long odpowiedz[MAXN + 1]; int nieparzyste[MAXN]; vector<int> V; void push(int p) { V.push_back(p); // cerr << "PUSH " << p << endl; } struct vset { shared_ptr<int[]> tab; int n; int offset; vset(): tab(nullptr), n(0), offset(0) {} vset inc(int off) { vset ret = *this; ret.offset += off; return ret; } const int* begin() const { return tab.get(); } const int* end() const { return tab.get() + n; } vset add(int val) { val -= offset; vset ret = *this; const int * p = lower_bound(this->begin(), this->end(), val); if (p == this->end() || *p != val) { ret.tab.reset(new int[n+1]); ret.n++; int idx = 0; for(const int * i = this->begin(); i<p; i++) { ret.tab[idx++] = *i; } ret.tab[idx++] = val; for(const int * i = p; i<this->end(); i++) { ret.tab[idx++] = *i; } } else { ret.tab.reset(new int[n-1]); ret.n--; int idx = 0; for(const int * i = this->begin(); i<p; i++) { ret.tab[idx++] = *i; } for(const int * i = p+1; i<this->end(); i++) { ret.tab[idx++] = *i; } } return ret; } void push(vector<int> & V1) const { FOR(i, n) { // cerr << "P " << tab.get()[i] + offset << endl; V1.push_back(tab.get()[i] + offset); } } void print() const { FOR(i, n) { cerr << tab[i] + offset << " "; } cerr << endl; } }; vector<int> get_them(int a, int b) { vector<int> V; for(int i = a; i<b; i++) { if (A[i+1]%2 == 0) V.push_back(nieparzyste[i+2] + 1); } return V; } vset roots[MAXN]; vset bottoms[MAXN+1]; void get_them(vector<int> & V, int a, int b) { bottoms[a].push(V); bottoms[b].push(V); } bool sort_by_w(int a, int b) { return w1[a] < w1[b]; } int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; A[0] = 1; A[n] = 0; for (int i = 1; i<n; i++) cin >> A[i]; FOR(i, q) { cin >> a1[i] >> b1[i] >> w1[i]; a1[i]--; b1[i]--; w1[i]--; } FOR(i, q) order[i] = i; // sort(order, order+n, sort_by_w); ile[n] = 0; sumdyst[n] = 0; for (int i = n-1; i>=0; i--) { ile[i] = (ile[i+1] * A[i+1] + 1) % MOD; sumdyst[i] = ((sumdyst[i+1] + ile[i+1]) * A[i+1]) % MOD; } for (int i = 1; i<n; i++) { sumdyst[i] = (sumdyst[i-1] + (ile[0] - ile[i]) - ile[i] + 2*MOD) % MOD; } nieparzyste[n] = 0; for (int i = n-1; i>=0; i--) { if (A[i] % 2 == 1) nieparzyste[i] = nieparzyste[i+1] + 1; else nieparzyste[i] = 0; } for(int i = n-2; i >= 0; i--) { if (A[i+1]%2 == 0) { bottoms[i] = bottoms[i+1].add(nieparzyste[i+2] + 1); } else { bottoms[i] = bottoms[i+1]; } // cerr << i << " " << bottoms[i].n << endl; } // cerr << "DONE" << endl; // FOR(i, n) cerr << ile[i] << " " << sumdyst[i] << " " << nieparzyste[i] << endl; FOR(rep, q) { // cerr << "ORD " << rep << " " << order[rep] << endl; int a=a1[order[rep]], b = b1[order[rep]], w = w1[order[rep]]; // cerr << a << b << w << endl; V.clear(); if (w < a && w < b) { push(nieparzyste[a+1]); push(nieparzyste[b+1]); push(nieparzyste[w+1]); } else { // if(a != b && A[w+1]%2 == 0) { // push(nieparzyste[w+2] + 1); // } push(nieparzyste[max(a, b)+1]); } // cerr << "middle" << endl; vector<int> V1(get_them(min(a,b), max(a,b))); get_them(V, min(a,b), max(a,b)); // for(auto it: V1) V.push_back(it); // roots[w].push(V); // cerr << "top" << endl; for (int i = w-1; i>=0; i--) { if (A[i+1]%2 == 0) { push(nieparzyste[i+2] + 1 + (w - i) ); push(w - i); } } push(w); if (V.size() %2 == 1) push(0); long long p = 0; sort(V.begin(), V.end()); // FOR(i, V.size()) { // cerr << " " << V[i]; // } // cerr << endl; int rot = 1; while(!V.empty()) { int v1 = V.back(); int v2 = V[V.size()-2]; int zmiana = v1 - v2; if (zmiana%2 == 1) { p += MOD + rot * (v2+1 + (v1-v2)/2); rot = -rot; } else { p += MOD + rot * ((v1-v2)/2); } p %= MOD; FOR(rep2, 2) V.pop_back(); } // cerr << sumdyst[a] << " " << sumdyst[b] << " " << sumdyst[a] - sumdyst[b] << endl; p%=MOD; if (nieparzyste[0] % 2 == 1 /*&& (a-w + b-w) %2 != 1*/) { p += (sumdyst[a] - sumdyst[b] + (a-w + b-w) + MOD)*INV2 + MOD; // ilość wierzchołków jest nieparzysta // p += ((a-w) + (b-w))/2; } else { p += (sumdyst[a] - sumdyst[b] + MOD)*INV2 + MOD; } p %= MOD; odpowiedz[order[rep]] = p; // cout << p << endl; } FOR(i, q) { cout << odpowiedz[i] << endl; } 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | #include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <unordered_map> #include <memory> using namespace std; const int MAXN = 500000; const int MAXQ = 500000; const long long MOD = 1000000007; const long long INV2 = 500000004; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long A[MAXN + 1]; int a1[MAXN], b1[MAXN], w1[MAXN]; int order[MAXN]; long long ile[MAXN + 1]; long long sumdyst[MAXN + 1]; long long odpowiedz[MAXN + 1]; int nieparzyste[MAXN]; vector<int> V; void push(int p) { V.push_back(p); // cerr << "PUSH " << p << endl; } struct vset { shared_ptr<int[]> tab; int n; int offset; vset(): tab(nullptr), n(0), offset(0) {} vset inc(int off) { vset ret = *this; ret.offset += off; return ret; } const int* begin() const { return tab.get(); } const int* end() const { return tab.get() + n; } vset add(int val) { val -= offset; vset ret = *this; const int * p = lower_bound(this->begin(), this->end(), val); if (p == this->end() || *p != val) { ret.tab.reset(new int[n+1]); ret.n++; int idx = 0; for(const int * i = this->begin(); i<p; i++) { ret.tab[idx++] = *i; } ret.tab[idx++] = val; for(const int * i = p; i<this->end(); i++) { ret.tab[idx++] = *i; } } else { ret.tab.reset(new int[n-1]); ret.n--; int idx = 0; for(const int * i = this->begin(); i<p; i++) { ret.tab[idx++] = *i; } for(const int * i = p+1; i<this->end(); i++) { ret.tab[idx++] = *i; } } return ret; } void push(vector<int> & V1) const { FOR(i, n) { // cerr << "P " << tab.get()[i] + offset << endl; V1.push_back(tab.get()[i] + offset); } } void print() const { FOR(i, n) { cerr << tab[i] + offset << " "; } cerr << endl; } }; vector<int> get_them(int a, int b) { vector<int> V; for(int i = a; i<b; i++) { if (A[i+1]%2 == 0) V.push_back(nieparzyste[i+2] + 1); } return V; } vset roots[MAXN]; vset bottoms[MAXN+1]; void get_them(vector<int> & V, int a, int b) { bottoms[a].push(V); bottoms[b].push(V); } bool sort_by_w(int a, int b) { return w1[a] < w1[b]; } int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; A[0] = 1; A[n] = 0; for (int i = 1; i<n; i++) cin >> A[i]; FOR(i, q) { cin >> a1[i] >> b1[i] >> w1[i]; a1[i]--; b1[i]--; w1[i]--; } FOR(i, q) order[i] = i; // sort(order, order+n, sort_by_w); ile[n] = 0; sumdyst[n] = 0; for (int i = n-1; i>=0; i--) { ile[i] = (ile[i+1] * A[i+1] + 1) % MOD; sumdyst[i] = ((sumdyst[i+1] + ile[i+1]) * A[i+1]) % MOD; } for (int i = 1; i<n; i++) { sumdyst[i] = (sumdyst[i-1] + (ile[0] - ile[i]) - ile[i] + 2*MOD) % MOD; } nieparzyste[n] = 0; for (int i = n-1; i>=0; i--) { if (A[i] % 2 == 1) nieparzyste[i] = nieparzyste[i+1] + 1; else nieparzyste[i] = 0; } for(int i = n-2; i >= 0; i--) { if (A[i+1]%2 == 0) { bottoms[i] = bottoms[i+1].add(nieparzyste[i+2] + 1); } else { bottoms[i] = bottoms[i+1]; } // cerr << i << " " << bottoms[i].n << endl; } // cerr << "DONE" << endl; // FOR(i, n) cerr << ile[i] << " " << sumdyst[i] << " " << nieparzyste[i] << endl; FOR(rep, q) { // cerr << "ORD " << rep << " " << order[rep] << endl; int a=a1[order[rep]], b = b1[order[rep]], w = w1[order[rep]]; // cerr << a << b << w << endl; V.clear(); if (w < a && w < b) { push(nieparzyste[a+1]); push(nieparzyste[b+1]); push(nieparzyste[w+1]); } else { // if(a != b && A[w+1]%2 == 0) { // push(nieparzyste[w+2] + 1); // } push(nieparzyste[max(a, b)+1]); } // cerr << "middle" << endl; vector<int> V1(get_them(min(a,b), max(a,b))); get_them(V, min(a,b), max(a,b)); // for(auto it: V1) V.push_back(it); // roots[w].push(V); // cerr << "top" << endl; for (int i = w-1; i>=0; i--) { if (A[i+1]%2 == 0) { push(nieparzyste[i+2] + 1 + (w - i) ); push(w - i); } } push(w); if (V.size() %2 == 1) push(0); long long p = 0; sort(V.begin(), V.end()); // FOR(i, V.size()) { // cerr << " " << V[i]; // } // cerr << endl; int rot = 1; while(!V.empty()) { int v1 = V.back(); int v2 = V[V.size()-2]; int zmiana = v1 - v2; if (zmiana%2 == 1) { p += MOD + rot * (v2+1 + (v1-v2)/2); rot = -rot; } else { p += MOD + rot * ((v1-v2)/2); } p %= MOD; FOR(rep2, 2) V.pop_back(); } // cerr << sumdyst[a] << " " << sumdyst[b] << " " << sumdyst[a] - sumdyst[b] << endl; p%=MOD; if (nieparzyste[0] % 2 == 1 /*&& (a-w + b-w) %2 != 1*/) { p += (sumdyst[a] - sumdyst[b] + (a-w + b-w) + MOD)*INV2 + MOD; // ilość wierzchołków jest nieparzysta // p += ((a-w) + (b-w))/2; } else { p += (sumdyst[a] - sumdyst[b] + MOD)*INV2 + MOD; } p %= MOD; odpowiedz[order[rep]] = p; // cout << p << endl; } FOR(i, q) { cout << odpowiedz[i] << endl; } return 0; } |