zoukankan      html  css  js  c++  java
  • [洛谷P5057][CQOI2006]简单题

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

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

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

    卡点:

    C++ Code:

    #include <cstdio>
    #include <cctype>
    
    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 100010
    
    int 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) << '
    ';
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    03-Spring默认标签解析
    想要写出好味道的代码,你需要养成这些好习惯!
    IDEA 缺少Springboot启动图标 如何添加
    echarts的canvas大小
    JS控制div上下滚动内容
    2020新的一年开始了
    2019年第一个工作日!
    关于.net项目前后端分离框架(一)
    MongoDB学习一:安装及简单使用
    spring默认标签与自定义标签学习
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/10127483.html
Copyright © 2011-2022 走看看