Suppose each query is not the entire length, but [x, y] this time interval.
。
It's right to feel free to write. This will become the merge scope.
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <queue > #include <algorithm> #define MEM (f) memset (F,0,sizeof (f)) #define M 100005#define mod 1000000007#define Lson o& Lt;<1, L, M#define Rson o<<1|1, m+1, rusing namespace std;typedef long long ll;const int MAX = 0x3f3f3f3f;const I NT MAXN = 200005;int mx_three (int A, int b, int c) {return Max (A, max (b, C));} int n, q, C, X, Y, b[maxn];struct c {int mx, LX, RX;} a[maxn<<2];void build (int o, int l, int r) {A[O].LX = a[o].mx = A[o].rx = 1; if (L = = r) return; int m = (l+r) >> 1; Build (Lson); Build (Rson);} C query (int o, int l, int r) {if (x <= l && r <= y) return a[o]; int m = (l+r) >> 1, len = r-l+1; if (y <= m) return query (Lson); if (M < x) return query (Rson); C s, s1, S2; S1 = query (Lson); S2 = query (Rson); S.LX = S1.LX; S.rx = S2.rx; if (b[m]! = b[m+1]) {if (S.LX = = len-(len>>1)) S.lx + = S2.LX; if (S.rx = = len>>1) S.rx + = S1.rx; s.mx = Mx_three (s1.mx, s2.mx, S1.RX+S2.LX); } else s.mx = Max (s1.mx, s2.mx); return s;} void update (int o, int l, int r) {if (L = = r) {B[c] ^= 1; Return } int m = (l+r) >> 1; if (c <= m) update (Lson); else update (Rson); int len = r-l+1, L = o<<1, r = o<<1|1; A[O].LX = A[L].LX; A[o].rx = A[r].rx; if (b[m]! = b[m+1]) {a[o].mx = Mx_three (a[l].mx, a[r].mx, A[L].RX+A[R].LX); if (A[O].LX = = len-(len>>1)) A[o].lx + = A[R].LX; if (A[o].rx = = len>>1) A[o].rx + = A[l].rx; } else a[o].mx = Max (a[l].mx, a[r].mx);} int main () {scanf ("%d%d", &n, &q); Build (1, 1, N); while (q--) {int RR; scanf ("%d", &RR); if (rr = = 2) {scanf ("%d", &c); Update (1, 1, N); } else {scanf ("%d%d", &x, &y); printf ("%d\n", Query (1, 1, N). mx); }} return 0;}
Vijos P1881 twinkling stars (strengthen yourself a little more.) )