洛谷P3870- [TJOI2009]开关 - 分块
Posted Chivas_/Regal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷P3870- [TJOI2009]开关 - 分块相关的知识,希望对你有一定的参考价值。
题目:
思路:
分块的入门题
就是把块长记录下来,设为len
存一个块的lazy懒惰标记,存一个块的总和sum
查询:
对散的元素一个个计算他们进行完lazy后的状态,然后对块的sum进行累加
更新:
对散的元素一个个更新并同时更新所处块的sum(按该元素进行完lazy后的状态对sum更新),对块的sum更新是len - sum[i],同时lazy多记录1
代码:
/*
________ _ ________ _
/ ______| | | | __ | | |
/ / | | | |__| | | |
| | | |___ _ _ _ ___ _ _____ | ___| ______ _____ ___ _ | |
| | | __ \\ |_| | | | | | _\\| | | ____| | |\\ \\ | __ | | _ | | _\\| | | |
| | | | \\ | _ | | | | | | \\ | | \\___ | | \\ \\ | |_/ _| | |_| | | | \\ | | |
\\ \\______ | | | | | | \\ |_| / | |_/ | ___/ | | | \\ \\ | /_ \\__ | | |_/ | | |
Author : \\________| |_| |_| |_| \\___/ |___/|_| |_____| _________|__| \\__\\ |______| | | |___/|_| |_|
____| |
\\_____/
*/
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <utility>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define G 10.0
#define LNF 1e18
#define EPS 1e-6
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define ll long long
#define ull unsigned long long
#define LOWBIT(x) ((x) & (-x))
#define LOWBD(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
#define UPPBD(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
#define TEST(a) cout << "---------" << a << "---------" << '\\n'
#define CHIVAS_ int main()
#define _REGAL exit(0)
#define SP system("pause")
#define IOS ios::sync_with_stdio(false)
//#define map unordered_map
#define _int(a) int a; cin >> a
#define _ll(a) ll a; cin >> a
#define _char(a) char a; cin >> a
#define _string(a) string a; cin >> a
#define _vectorInt(a, n) vector<int>a(n); cin >> a
#define _vectorLL(a, b) vector<ll>a(n); cin >> a
#define PB(x) push_back(x)
#define ALL(a) a.begin(),a.end()
#define MEM(a, b) memset(a, b, sizeof(a))
#define EACH_CASE(cass) for (cin >> cass; cass; cass--)
#define LS l, mid, rt << 1
#define RS mid + 1, r, rt << 1 | 1
#define GETMID (l + r) >> 1
using namespace std;
template<typename T> inline void Read(T &x){T f = 1; x = 0;char s = getchar();while(s < '0' || s > '9'){if(s == '-') f = -1; s = getchar();}while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}x*=f;}
template<typename T> inline T MAX(T a, T b){return a > b? a : b;}
template<typename T> inline T MIN(T a, T b){return a > b? b : a;}
template<typename T> inline void SWAP(T &a, T &b){T tp = a; a = b; b = tp;}
template<typename T> inline T GCD(T a, T b){return b > 0? GCD(b, a % b) : a;}
template<typename T> inline void ADD_TO_VEC_int(T &n, vector<T> &vec){vec.clear(); cin >> n; for(int i = 0; i < n; i ++){T x; cin >> x, vec.PB(x);}}
template<typename T> inline pair<T, T> MaxInVector_ll(vector<T> vec){T MaxVal = -LNF, MaxId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MaxVal < vec[i]) MaxVal = vec[i], MaxId = i; return {MaxVal, MaxId};}
template<typename T> inline pair<T, T> MinInVector_ll(vector<T> vec){T MinVal = LNF, MinId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MinVal > vec[i]) MinVal = vec[i], MinId = i; return {MinVal, MinId};}
template<typename T> inline pair<T, T> MaxInVector_int(vector<T> vec){T MaxVal = -INF, MaxId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MaxVal < vec[i]) MaxVal = vec[i], MaxId = i; return {MaxVal, MaxId};}
template<typename T> inline pair<T, T> MinInVector_int(vector<T> vec){T MinVal = INF, MinId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MinVal > vec[i]) MinVal = vec[i], MinId = i; return {MinVal, MinId};}
template<typename T> inline pair<map<T, T>, vector<T> > DIV(T n){T nn = n;map<T, T> cnt;vector<T> div;for(ll i = 2; i * i <= nn; i ++){while(n % i == 0){if(!cnt[i]) div.push_back(i);cnt[i] ++;n /= i;}}if(n != 1){if(!cnt[n]) div.push_back(n);cnt[n] ++;n /= n;}return {cnt, div};}
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
const int maxn = 2e5 + 10;
int n, m, len;//总长、查询次数、块长
int c, a, b;
int sum[350];//块的总和
int lazy[350];//懒惰标记,记录这个块更新了多少次
int w[maxn];//灯的状态
inline int get(int i){//获取第i个元素所处块的编号
return i / len;
}
inline int Query(int l, int r){//查询区间和
int res = 0;
if(get(l) == get(r)){//所处同一个块,全是散数,直接暴力
for(int i = l; i <= r; i ++) res += (lazy[get(i)] & 1) ? w[i] ^ 1 : w[i];//如果变了奇数次,就说明有变值
}else{
int i = l, j = r;
while(get(i) == get(l)) res += (lazy[get(i)] & 1) ? w[i] ^ 1 : w[i], i ++;//将左侧散数给算了,方式和上面一样
while(get(j) == get(r)) res += (lazy[get(j)] & 1) ? w[j] ^ 1 : w[j], j --;//将右侧散数给算了,方式和上面一样
for(int k = get(i); k <= get(j); k ++) res += sum[k];//计算中间完整的块
}return res;
}
inline void Update(int l, int r){//更新区间
if(get(l) == get(r)){//所处同一个块,全是散数,直接暴力
for(int i = l; i <= r; i ++) sum[get(i)] += (Query(i, i) ^ 1) - Query(i, i), w[i] ^= 1;//该块的sum + (变换后的值 - 变换前的值),同时一个个更新
}else{
int i = l, j = r;
while(get(i) == get(l)) sum[get(i)] += (Query(i, i) ^ 1) - Query(i, i), w[i] ^= 1, i ++;//将左侧散数给算了,该块的sum + (变换后的值 - 变换前的值),同时一个个更新
while(get(j) == get(r)) sum[get(j)] += (Query(j, j) ^ 1) - Query(j, j), w[j] ^= 1, j --;//将右侧散数给算了,该块的sum + (变换后的值 - 变换前的值),同时一个个更新
for(int k = get(i); k <= get(j); k ++) sum[k] = len - sum[k], lazy[k] ++;//计算中间完整的块,并记录变了多少次
}
}
CHIVAS_{
cin >> n >> m; len = sqrt(n);
while(m --){
cin >> c >> a >> b;
if(c) cout << Query(a, b) << endl;
else Update(a, b);
}
_REGAL;
}
以上是关于洛谷P3870- [TJOI2009]开关 - 分块的主要内容,如果未能解决你的问题,请参考以下文章