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\%\)数据\(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\)的数量即可。

#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;
}

转载于:https://www.cnblogs.com/forth/p/9852697.html

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值