社区讨论
简单分块题 求调
灌水区参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @m45c746q
- 此快照首次捕获于
- 2024/12/01 16:25 去年
- 此快照最后确认于
- 2025/11/04 13:30 4 个月前
CPP
#include "bits/stdc++.h"
#define For(i, l, r) for(int i = (l) ; i <= (r) ; ++i)
using namespace std ;
const int N = 200005 ;
int P[N], L[N], R[N], res[N], tag[N], len, tot ;
int A[N], n, m ;
void Init() ;
void Update(int l, int r) ;
int Get(int l, int r) ;
int main()
{
ios::sync_with_stdio(false) ;
cin.tie(0) ; cout.tie(0) ;
cin >> n >> m ; Init() ;
For(i, 1, m)
{
int op, l, r ;
cin >> op >> l >> r ;
if(op == 0) Update(l, r) ;
else cout << Get(l, r) << endl ;
}
return 0 ;
}
void Init()
{
len = sqrt(n), tot = (n / len) + (n % len ? 1 : 0) ;
For(i, 1, n) P[i] = (i - 1) / len + 1 ;
For(i, 1, tot) L[i] = (i - 1) * len + 1, R[i] = i * len ;
}
void Update(int l, int r)
{
For(i, l, min(R[P[l]], r))
{
if(A[i]) res[P[i]]--, A[i] = 0 ;
else res[P[i]]++, A[i] = 1 ;
}
if(P[l] != P[r]) For(i, L[P[r]], r)
{
if(A[i]) res[P[i]]--, A[i] = 0 ;
else res[P[i]]++, A[i] = 1 ;
}
For(i, P[l] + 1, P[r] - 1)
{
if(tag[i]) res[i] = len - res[i], tag[i] = 0 ;
else res[i] = len - res[i], tag[i] = 1 ;
}
}
int Get(int l, int r)
{
int cnt = 0 ;
For(i, l, min(R[P[l]], r)) cnt += A[i] ^ tag[i] ;
if(P[l] != P[r]) For(i, L[P[r]], r) cnt += A[i] ^ tag[i] ;
For(i, P[l] + 1, P[r] - 1)
{
if(tag[i]) cnt += len - res[i] ;
else cnt += res[i] ;
}
return cnt ;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...