Problem
题目描述
AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏。在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下
1. 拥有一个伤害串为长度为\(n\)的\(01\)串。 2. 给定一个范围\([l,r]\),伤害为伤害串的这个范围内中\(1\)的个数 3.
会被随机修改伤害串中的数值,修改的方法是把\([l,r]\)中的所有数\(\oplus\)上\(1\)
AKN想知道一些时刻的伤害,请你帮助他求出这个伤害 ## 输入输出格式 ###
输入格式:
第一行两个数\(n\),\(m\),表示长度为\(n\)的\(01\)串,有\(m\)个时刻
第二行一个长度为\(n\)的\(01\)串,为初始伤害串
第三行开始\(m\)行,每行三个数\(p\),\(l\),\(r\)
若\(p\)为\(0\),则表示当前时刻改变\([l,r]\)的伤害串,改变规则如上
若\(p\)为\(1\),则表示当前时刻AKN想知道\([l,r]\)的伤害
输出格式:
对于每次询问伤害,输出一个数值伤害,每次询问输出一行
输入输出样例
输入样例#1:
1 2 3 4 5 6 7 8
| 10 6 1011101001 0 2 4 1 1 5 0 3 7 1 1 10 0 1 4 1 2 6
|
输出样例#1:
说明
样例解释:
\(1011101001\)
\(1100101001\)
询问\([1,5]\)输出\(3\)
\(1111010001\)
询问\([1,10]\)输出\(6\)
\(0000010001\)
询问\([2,6]\)输出\(1\)
数据范围:
\(10\%\)数据\(2\leq n\),\(m\leq
10\)
另有\(30\%\)数据\(2\leq n,m\leq 2000\)
\(100\%\)数据\(2\leq n,m\leq 2\times 10^5\)
By:worcher # Solution 好像线段树写上瘾了
挺简单的一道线段树, 异或操作交换\(0\)和\(1\)的数量即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include <algorithm> #include <cstdio> class SegmentTree { private: struct Node { int zero, one, delta; Node *left, *right; int L, R; inline void Update() { if (this->left) { this->zero = this->left->zero + this->right->zero; this->one = this->left->one + this->right->one; } } inline void PushDown() { if (this->delta && this->left) { std::swap(this->left->zero, this->left->one); std::swap(this->right->zero, this->right->one); this->left->delta ^= 1; this->right->delta ^= 1; this->delta = 0; } } Node(const int &l, const int &r) { this->L = l, this->R = r; this->zero = this->one = this->delta = 0; this->left = this->right = nullptr; if (l == r) { if (getchar() - '0') this->one = 1; else this->zero = 1; } else { register int mid((l + r) >> 1); this->left = new Node(l, mid); this->right = new Node(mid + 1, r); this->Update(); } } ~Node() { if (this->left) { delete this->left; delete this->right; } } inline int Sum(const int &l, const int &r) { if (l <= this->L && this->R <= r) { return this->one; } else { this->PushDown(); register int mid((this->L + this->R) >> 1), ret(0); if (l <= mid) ret += this->left->Sum(l, r); if (mid < r) ret += this->right->Sum(l, r); return ret; } } inline void ExclusiveOr(const int &l, const int &r) { if (l <= this->L && this->R <= r) { std::swap(this->zero, this->one); this->delta ^= 1; } else { this->PushDown(); register int mid((this->L + this->R) >> 1); if (l <= mid) this->left->ExclusiveOr(l, r); if (mid < r) this->right->ExclusiveOr(l, r); this->Update(); } } } * root;
public: SegmentTree() { this->root = nullptr; } inline void Clear() { if (this->root) { delete this->root; this->root = nullptr; } } inline void Build(const int &l, const int &r) { this->root = new Node(l, r); } inline int Sum(const int &l, const int &r) { return this->root->Sum(l, r); } inline void ExclusiveOr(const int &l, const int &r) { this->root->ExclusiveOr(l, r); } } T; int n, m; int p, l, r; int main(int argc, char const *argv[]) { scanf("%d %d\n", &n, &m); T.Build(1, n); while (m--) { scanf("%d %d %d", &p, &l, &r); switch (p) { case 0: { T.ExclusiveOr(l, r); break; } case 1: { printf("%d\n", T.Sum(l, r)); break; } } } return 0; }
|