[USACO08NOV]lites

时间:2023-03-09 03:18:23
[USACO08NOV]lites

嘟嘟嘟

竟然还能发现这么水的题。就是线段树维护区间亦或嘛~~~~

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define space putchar(' ')
#define enter puts("")
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << ) + (ans << ) + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m; int l[maxn << ], r[maxn << ], sum[maxn << ], lazy[maxn << ];
void build(int L, int R, int now)
{
l[now] = L; r[now] = R;
if(L == R) return;
int mid = (L + R) >> ;
build(L, mid, now << );
build(mid + , R, now << | );
}
void pushdown(int now)
{
if(lazy[now])
{
sum[now << ] = r[now << ] - l[now << ] + - sum[now << ];
lazy[now << ] ^= ;
sum[now << | ] = r[now << | ] - l[now << | ] + - sum[now << | ];
lazy[now << | ] ^= ;
lazy[now] = ;
}
}
void update(int L, int R, int now)
{
if(l[now] == L && r[now] == R)
{
sum[now] = (R - L + ) - sum[now];
lazy[now] ^= ; return;
}
pushdown(now);
int mid = (l[now] + r[now]) >> ;
if(R <= mid) update(L, R, now << );
else if(L > mid) update(L, R, now << | );
else update(L, mid, now << ), update(mid + , R, now << | );
sum[now] = sum[now << ] + sum[now << | ];
}
int query(int L, int R, int now)
{
if(l[now] == L && r[now] == R) return sum[now];
pushdown(now);
int mid = (l[now] + r[now]) >> ;
if(R <= mid) return query(L, R, now << );
else if(L > mid) return query(L, R, now << | );
else return query(L, mid, now << ) + query(mid + , R, now << | );
} int main()
{
n = read(); m = read();
build(, n, );
for(int i = ; i <= m; ++i)
{
int d = read(), L = read(), R = read();
if(!d) update(L, R, );
else write(query(L, R, )), enter;
}
return ;
}