void HLD::update(int x, int y) { while(ntop[x] != ntop[y]) { if( dept[ ntop[x] ] > dept[ ntop[y] ] ) swap(x,y); bit.update( dfsn[ ntop[y] ], dfsn[ y ], 1); y = fath[ ntop[y] ]; } if( dept[x] > dept[y] ) swap(x,y); x = bson[x]; bit.update( dfsn[x], dfsn[y], 1); }
int main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("d.in", "r", stdin); cin >> T; while (T--){ cin >> N; for (int i = 1 ; i <= N ; i++){ cin >> A[i]; B[i] = A[i]; } sort(B + 1, B + N + 1); for (int i = 1 ; i <= N ; i++) ID[i] = lower_bound(B + 1, B + N + 1, A[i]) - B; S[N + 1] = 0; for (int i = N ; i >= 1 ; i--){ S[i] = 1; if (ID[i] < ID[i + 1]) S[i] += S[i + 1]; } int sol = 0; BIT bit; for (int i = 1 ; i <= N ; i++){ D[i] = 1; if (ID[i] > ID[i - 1]) D[i] += D[i - 1]; bit.update(ID[i], D[i]); int s = S[i] + bit.query(ID[i] - 1); if (sol < s) sol = s; } cout << sol << '\n'; } return 0; }
int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int s; scanf("%d", &s); sect[s].push_back(i); } for (int i = 1; i <= n; ++i) scanf("%d", &need[i]); scanf("%d", &k); for (int q = 0; q < k; ++q) { scanf("%d%d%d", &l[q], &r[q], &a[q]); } for (int i = 1; i <= n; ++i) { lo[i] = 0; hi[i] = k; } BIT bit; while (!terminated()){ // clear tree bit.reset(); // generate new check for (int i = 1; i <= n; ++i) { int mid = (lo[i]+hi[i])/2; check[mid].push_back(i); } for (int q = 0; q < k; ++q) { // apply query if (l[q] <= r[q]) { bit.update(l[q], a[q]); bit.update(r[q]+1, -a[q]); } else { bit.update(l[q], a[q]); bit.update(1, a[q]); bit.update(r[q]+1, -a[q]); } // check for (auto &v: check[q]) { long long sum = 0; for (auto &s: sect[v]) { sum += bit.query(s); if (sum >= need[v]) break; } if (sum >= need[v]) { hi[v] = q; } else { lo[v] = q+1; } } check[q].clear(); } } for (int i = 1; i <= n; ++i) { if (lo[i] < k) printf("%d\n", lo[i]+1); else puts("NIE"); } return 0; }