Luogu P2574 XOR的艺术 (线段树)

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:
10 6
1011101001
0 2 4
1 1 5
0 3 7
1 1 10
0 1 4
1 2 6
输出样例#1:
3
6
1

说明

样例解释:

(1011101001)

(1100101001)

询问([1,5])输出(3)

(1111010001)

询问([1,10])输出(6)

(0000010001)

询问([2,6])输出(1)

数据范围:

(10\%)数据(2leq n),(mleq 10)

另有(30\%)数据(2leq n,mleq 2000)

(100\%)数据(2leq n,mleq 2 imes 10^5)

By:worcher

Solution

好像线段树写上瘾了

挺简单的一道线段树, 异或操作交换(0)(1)的数量即可。

#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, &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
", T.Sum(l, r));
        break;
      }
    }
  }
  return 0;
}

原文地址:https://www.cnblogs.com/forth/p/9852697.html