SPOJ MULTQ3 7299 Multiples of 3 (区间更新)
题目连接:http://www.spoj.com/problems/MULTQ3/
#include <iostream>
#include <stdio.h>
#include <string.h>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R
/*
题意:给出n个数,初试为0,给出两个操作;
0 A B :将[A,B]区间中的每个数+1
1 A B :询问[A,B]区间中有多少个能被3整除的数。
思路:每个节点存储三个值:num0:整除3的个数,num1:除以3余1的个数,num2:除以3余2的个数
更新的时候,只要将这三个值互换即可
*/
using namespace std;
const int maxn=;
int n,m; struct Node{
//num0:整除3的个数,num1:除以3余1的个数,num2:除以3余2的个数
int num0,num1,num2;
int lazy; //标记该区间+1的次数,如果三次+1相当于不变
}tree[maxn<<]; void build(int rt,int L,int R){
tree[rt].num0=(R-L+);
tree[rt].num1=tree[rt].num2=;
tree[rt].lazy=;
if(L==R)
return;
int mid=(R+L)>>;
build(lson);
build(rson);
}
void pushUp(int rt){
tree[rt].num0=tree[rt<<].num0+tree[rt<<|].num0;
tree[rt].num1=tree[rt<<].num1+tree[rt<<|].num1;
tree[rt].num2=tree[rt<<].num2+tree[rt<<|].num2;
}
void pushDown(Node &rt,Node &ls,Node &rs){
if(rt.lazy==){
/*
+1一次:
num0->num1
num1->num2
num2->num0
*/
int tmp;
tmp=ls.num0;
ls.num0=ls.num2;
ls.num2=ls.num1;
ls.num1=tmp;
ls.lazy=(ls.lazy+rt.lazy)%; tmp=rs.num0;
rs.num0=rs.num2;
rs.num2=rs.num1;
rs.num1=tmp;
rs.lazy=(rs.lazy+rt.lazy)%; rt.lazy=;
}
else if(rt.lazy==){
/*
+1二次:
num0->num2
num2->num1
num1->num0
*/
int tmp;
tmp=ls.num0;
ls.num0=ls.num1;
ls.num1=ls.num2;
ls.num2=tmp;
ls.lazy=(ls.lazy+rt.lazy)%; tmp=rs.num0;
rs.num0=rs.num1;
rs.num1=rs.num2;
rs.num2=tmp;
rs.lazy=(rs.lazy+rt.lazy)%;
rt.lazy=;
}
}
void update(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
int tmp;
tmp=tree[rt].num0;
tree[rt].num0=tree[rt].num2;
tree[rt].num2=tree[rt].num1;
tree[rt].num1=tmp;
tree[rt].lazy=(tree[rt].lazy+)%;
return;
}
pushDown(tree[rt],tree[rt<<],tree[rt<<|]);
int mid=(L+R)>>;
if(l<=mid)
update(lson,l,r);
if(r>mid)
update(rson,l,r);
pushUp(rt);
}
int query(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
return tree[rt].num0;
}
pushDown(tree[rt],tree[rt<<],tree[rt<<|]);
int ret=;
int mid=(L+R)>>;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
}
int main()
{
int t,a,b;
scanf("%d%d",&n,&m);
build(,,n);
for(int i=;i<=m;i++){
scanf("%d%d%d",&t,&a,&b);
a++;b++;
if(t==){
update(,,n,a,b);
}
else{
int ans=query(,,n,a,b);
printf("%d\n",ans);
}
}
return ;
}
SPOJ MULTQ3 7299 Multiples of 3 (区间更新)的更多相关文章
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- hdu 3397 Sequence operation(线段树:区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的 ...
- 【HDU 4614】Vases and Flowers(线段树区间更新懒惰标记)
题目0到n-1的花瓶,操作1在下标a开始插b朵花,输出始末下标.操作2清空[a,b]的花瓶,求清除的花的数量.线段树懒惰标记来更新区间.操作1,先查询0到a-1有num个空瓶子,然后用线段树的性质,或 ...
- HDU 1698 Just a Hook(线段树/区间更新)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description In the g ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- NOIP 2015复赛提高组Day2 T1==Codevs 4768 跳石头
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中 ...
- Android 悬浮窗 WindowManager WindowManager.LayoutParamas
前方高清大图~~~~ 绘制的图片资源: // draw bitmap BitmapDrawable bmpDraw = (BitmapDrawable) getResources().getDrawa ...
- Matlab2012a第一次安装打不开 查找程序安装类时出错
打开bin文件夹下的matlab!!!!!!进行激活~
- JQ中的html()、text()、val()的用法
<input type="text" val=""> 用val(); <sapn>你好</sapn> 用text() &l ...
- easyUI中treegrid组件构造树形表格(简单数据类型)+ssm后台
这几天做的项目要求用树形表格的形式展示一部分数据,于是就想到了使用easyUI的treegrid组件,但几经翻查各种资料,发现数据类型大多采取标准数据类型,即包含children元素的数据类型,小编查 ...
- js设计模式(4)---组合模式
0.前言 今天是建党节,新疆那边又开始了闹腾.作为立志成为码农的我,现在已经从一个大愤青淡化为一个小愤青,对这些国家民生大事不在血气喷发,转而把经历发泄在技术问题上面,因而在扯一篇随笔吧,把无处发泄的 ...
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- 前端工程搭建NodeJs+gulp+bower
需要node.npm的事先安装!! 1.nodejs安装程序会在环境变量中添加两个变量: 系统环境变量中:path 增加C:\Program Files\nodejs\ 因为在该目下存在node.ex ...
- Linux之父Linus Torvalds:讨厌C++
"Linux内核的创始人Linus Torvalds最近在一封邮件中说明了内核开发需要使用C语言而非C++的理由.在庞大的项目中,人们对不是自己开发的模块并不了解,能快速理解其他模块中函数的 ...
- easyui菜单栏的使用
<div id="tabs" class="easyui-tabs" data-options="plain:true,fit:true,bor ...