传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1230
【题解】
线段树裸题啊
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int M = 5e5 + 10; const int mod = 1e9+7; # define RG register # define ST static int n, m; struct SMT { int w[M], n; bool tag[M]; inline void set(int _n) { n = _n; memset(w, 0, sizeof w); memset(tag, 0, sizeof tag); } # define ls (x<<1) # define rs (x<<1|1) inline void up(int x) { if(!x) return ; w[x] = w[ls] + w[rs]; } inline void pushtag(int x, int l, int r) { if(!x) return ; w[x] = r-l+1-w[x]; tag[x] ^= 1; } inline void down(int x, int l, int r) { if(!x) return ; if(!tag[x]) return ; int mid = l+r>>1; pushtag(ls, l, mid); pushtag(rs, mid+1, r); tag[x] = 0; } inline void edt(int x, int l, int r, int L, int R) { if(L <= l && r <= R) { pushtag(x, l, r); return ; } down(x, l, r); int mid = l+r>>1; if(L <= mid) edt(ls, l, mid, L, R); if(R > mid) edt(rs, mid+1, r, L, R); up(x); } inline int query(int x, int l, int r, int L, int R) { if(L <= l && r <= R) return w[x]; down(x, l, r); int mid = l+r>>1, ret = 0; if(L <= mid) ret += query(ls, l, mid, L, R); if(R > mid) ret += query(rs, mid+1, r, L, R); return ret; } inline void edt(int L, int R) { edt(1, 1, n, L, R); } inline int sum(int L, int R) { return query(1, 1, n, L, R); } }T; int main() { cin >> n >> m; T.set(n); int opt, S, E; while(m--) { scanf("%d%d%d", &opt, &S, &E); if(opt == 0) T.edt(S, E); else printf("%d\n", T.sum(S, E)); } return 0; }