博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[洛谷P5057][CQOI2006]简单题
阅读量:4581 次
发布时间:2019-06-09

本文共 1588 字,大约阅读时间需要 5 分钟。

题目大意:有一个长度为$n$的$01$串,两个操作:

  1. $1\;l\;r:$把区间$[l,r]$翻转($0->1,1->0$)
  2. $2\;p:$求第$p$位是什么

题解:维护前缀异或和,树状数组即可

卡点:

 

C++ Code:

#include 
#include
namespace std { struct istream {#define M (1 << 24 | 3) char buf[M], *ch = buf - 1; inline istream() {#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin);#endif fread(buf, 1, M, stdin); } inline istream& operator >> (int &x) { while (isspace(*++ch)); for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15); return *this; }#undef M } cin; struct ostream {#define M (1 << 24 | 3) char buf[M], *ch = buf - 1; int w; inline ostream& operator << (int x) { if (!x) { *++ch = '0'; return *this; } for (w = 1; w <= x; w *= 10); for (w /= 10; w; w /= 10) *++ch = (x / w) ^ 48, x %= w; return *this; } inline ostream& operator << (const char x) {*++ch = x; return *this;} inline ostream& operator << (const char *x) { while (*x) *this << *x++; return *this; } inline ~ostream() {#ifndef ONLINE_JUDGE freopen("output.txt", "w", stdout);#endif fwrite(buf, 1, ch - buf + 1, stdout); }#undef M } cout;}#define maxn 100010int n, m;namespace BIT { int Tr[maxn], res; inline void add(int p) {for (; p <= n; p += p & -p) Tr[p] ^= 1;} inline int ask(int p) {for (res = 0; p; p &= p - 1) res ^= Tr[p]; return res;}}int main() { std::cin >> n >> m; while (m --> 0) { int op, l, r; std::cin >> op >> l; if (op == 1) { std::cin >> r; BIT::add(l), BIT::add(r + 1); } else std::cout << BIT::ask(l) << '\n'; } return 0;}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/10127483.html

你可能感兴趣的文章
LNMP环境下安装freeradius+radius manager3.9
查看>>
cocos2d-x性能优化的那些事
查看>>
LightOJ 1007 - Mathematically Hard
查看>>
前端和算法实现:给网站上加上自己的水印(简单+复杂)
查看>>
react-native学习(RN)--之Window环境下搭建环境配置,以及初始化建立react-native项目,(真机和模拟器运行的相关错误解决办法,android打包报错)...
查看>>
WPF路由事件学习(一)
查看>>
特殊字符导致jquery-mobile 挂起(firefox控制台报错 malformed URI sequence)
查看>>
Java3-1
查看>>
系统分析与设计 作业一
查看>>
大数据入门---------------------Java部分开始
查看>>
Java中的逆变与协变
查看>>
ASP.NET站点
查看>>
mvc
查看>>
[leetcode]Map-560. Subarray Sum Equals K
查看>>
LeetCode No.6 ZigZag Conversion
查看>>
CSS中position为relative时的特性
查看>>
javascript类式继承最优版
查看>>
opencv
查看>>
将相关数据拼成所需JSON数据
查看>>
第一章
查看>>