P3870 [TJOI2009]开关
思路
代码
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ls rt<<1
#define rs rt<<1|1
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn = 2e5 + 7;
int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int n, m;
struct node{
int l,r,size,sum,lazy;
}e[maxn<<2];
void pushup(int rt) {
e[rt].sum=e[ls].sum+e[rs].sum;
}
void pushdown(int rt) {
if(e[rt].lazy%2) {
e[ls].lazy++;
e[rs].lazy++;
e[ls].sum=e[ls].size-e[ls].sum;
e[rs].sum=e[rs].size-e[rs].sum;
e[rt].lazy=0;
}
}
void build(int l,int r,int rt) {
e[rt].l=l,e[rt].r=r,e[rt].size=r-l+1;
if(l==r) return;
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
pushup(rt);
}
void modfity(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
e[rt].sum=e[rt].size-e[rt].sum;
e[rt].lazy++;
return;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modfity(L,R,ls);
if(R>mid) modfity(L,R,rs);
pushup(rt);
}
int query(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
return e[rt].sum;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1,ans=0;
if(L<=mid) ans+=query(L,R,ls);
if(R>mid) ans+=query(L,R,rs);
return ans;
}
int main() {
n=read(),m=read();
build(1,n,1);
FOR(i,1,m) {
int p=read(),x=read(),y=read();
if(p) {
printf("%d\n",query(x,y,1));
} else {
modfity(x,y,1);
}
}
return 0;
}
P3870 [TJOI2009]开关的更多相关文章
- 洛谷P3870 [TJOI2009]开关
题目描述 现有\(N(2 ≤ N ≤ 100000)\)盏灯排成一排,从左到右依次编号为:\(1,2,......,N\).然后依次执行\(M(1 ≤ M ≤ 100000)\)项操作,操作分为两种: ...
- 洛谷 P3870 [TJOI2009]开关
题意简述 有n盏灯,默认为关,有两个操作: 1.改变l~r的灯的状态(把开着的灯关上,关着的灯打开) 2.查询l~r开着的灯的数量 题解思路 维护一个线段树,支持区间修改,区间查询 懒标记每次^1 代 ...
- 洛谷 P3870 [TJOI2009]开关 题解
原题链接 前置知识: 线段树的单点.区间的修改与查询. 一看,我们需要维护两个操作: 区间取反: 区间求和. (因为区间 \(1\) 的个数,就是区间的和) 典型的 线段树 . 如果你只会线段树的 区 ...
- 洛谷P3870 [TJOI2009] 开关 (线段树)
简单的省选题...... 打异或标记即可. 1 #include<bits/stdc++.h> 2 const int N=2e5+10; 3 using namespace std; 4 ...
- 题解 P3870 【[TJOI2009]开关】/基础分块学习小结
直接进入正题: 分块: 分块分块,就是把一个长串东西,分为许多块,这样,我们就可以在操作一个区间的时候,对于在区间里面完整的块,直接操作块,不完整的直接操作即可,因为不完整,再加上一个块本身就不大,复 ...
- 题解 P3870 【[TJOI2009]开关】
这个题我愣是交了好几遍没有过...... 后来@_皎月半洒花dalao告诉我说要^儿子节点的tag,然后就明白了...... 行吧,先上题面: 题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成 ...
- [TJOI2009]开关 (线段树)
题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成一排,从左到右依次编号为:1,2,......,N.然后依次执行M(1 ≤ M ≤ 100000)项操作,操作分为两种:第一种操作指定一个区间[ ...
- 洛谷 3870 [TJOI2009]开关
[题解] 线段树基础题.对于每个修改操作把相应区间的sum改为区间长度-sum即可. #include<cstdio> #include<algorithm> #include ...
- Luogu3870 [TJOI2009]开关 (分块)
线段树做法很简单,但分块好啊 #include <iostream> #include <cstdio> #include <cstring> #include & ...
随机推荐
- King's Quest---poj1904(连通图缩点)
题目链接:http://poj.org/problem?id=1904 题意:国王有n个儿子,每个儿子喜欢ki个女孩,国王想让王子与他喜欢的人结婚,就让巫师做一个列表出来,但是国王想知道王子能和哪些女 ...
- Find The Multiple--POJ1426
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- Ucenter社区服务搭建
1.搭建lamp环境yum –y install httpd php php-mysql mysql mysql-server 2启动服务 3.设置服务开机自动启动 4.上传UCEN ...
- webview与js交互(转)
原文:http://www.cnblogs.com/vanezkw/archive/2012/07/02/2572799.html 对于android初学者应该都了解webView这个组件.之前我也是 ...
- spring security积累
使用数据库管理用户权限: Spring Security默认情况下需要两张表,用户表和权限表 create table users( username varchar_ignorecase(50) n ...
- R中基本函数学习[转载]
转自:https://www.douban.com/note/511740050/ 1.数据管理 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- 支持向量机:Numerical Optimization,SMO算法
http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988419.html 另外一篇:http://www.cnblogs.com/vivouni ...
- Bootstrap 网格系统(Grid System)的工作原理 - 媒体查询
媒体查询 媒体查询是非常别致的"有条件的 CSS 规则".它只适用于一些基于某些规定条件的 CSS.如果满足那些条件,则应用相应的样式. Bootstrap 中的媒体查询允许您基于 ...
- 使用POI读取/创建Execl(.xlsx)文件
最近项目中用到了解析Execl表格的功能,在网上百度了一下自己写了一个小Demo.由于项目中使用的是Execl2007,就是后缀为.xlsx的,所以只研究了解析和创建Execl2007的文件,解析Ex ...