NBUT 1602 Mod Three(线段树单点更新区间查询)
[1602] Mod Three
- 时间限制: 5000 ms 内存限制: 65535 K
- 问题描述
- Please help me to solve this problem, if so, LiangChen must have zhongxie!
There is a sequence contains n integers a0, a1, ...., an, firstly all of them equal 0,
and there are q opertions, each of them either is0 x; add ax by 1
or
1 l r; calculator the total number of ai that ai mod 3 == 0 (l <= i <= r) - 输入
- Input starts with an integer T denoting the number of test cases.
For each test case, first line contains n and q(1 <= n, q <= 100,000), next line contain n integers. - 输出
- For each test case, first line contains "Case X:", then print your answer, one line contains one integer.
- 样例输入
1
10 8
0 4
0 3
0 6
1 2 3
0 7
1 1 10
0 8
1 1 8- 样例输出
Case 1:
1
6
3
题目链接:NBUT 1602
看了一下居然跟是1603重复的,不过这题没人写,另外题目描述有点问题,序列不是a0~an而是a1~an。
用val统计叶子节点此时的值,cnt统计区间(节点)内模3不为0的个数,一开始都是0因此cnt的初始值为1
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct seg
{
int l,mid,r;
int val,cnt;
};
seg T[N<<2];
void pushup(int k)
{
T[k].val=T[LC(k)].val+T[RC(k)].val;
T[k].cnt=T[LC(k)].cnt+T[RC(k)].cnt;
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
if(l==r)
{
T[k].val=0;
T[k].cnt=1;
return ;
}
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(int k,int x)
{
if(T[k].l==T[k].r&&T[k].l==x)
T[k].cnt=((++T[k].val)%3==0);
else
{
if(x<=T[k].mid)
update(LC(k),x);
else
update(RC(k),x);
pushup(k);
}
}
int query(int k,int l,int r)
{
if(l<=T[k].l&&T[k].r<=r)
return T[k].cnt;
else
{
if(r<=T[k].mid)
return query(LC(k),l,r);
else if(l>T[k].mid)
return query(RC(k),l,r);
else
return query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r);
}
}
int main(void)
{
int tcase,i,n,m,ops,l,r,x;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
scanf("%d%d",&n,&m);
build(1,1,n);
printf("Case %d:\n",q);
for (i=0; i<m; ++i)
{
scanf("%d",&ops);
if(ops==0)
{
scanf("%d",&x);
update(1,x);
}
else if(ops==1)
{
scanf("%d%d",&l,&r);
printf("%d\n",query(1,l,r));
}
}
}
return 0;
}
NBUT 1602 Mod Three(线段树单点更新区间查询)的更多相关文章
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 -> Link1 <- -> Link2 <- 以上两题题意是一样 ...
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...
- HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU1166(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- CDOJ 1073 线段树 单点更新+区间查询 水题
H - 秋实大哥与线段树 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit S ...
- Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)
预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...
- HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- CSS鼠标样式整理
鼠标样式的标签: cursor:*; //该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状: 鼠标样式: 值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一 ...
- rdesktop remember
整个地球都知道rdesktop,有了它,我们可以从Solaris或者Linux使用Windows,当然Windows要开启Windows Terminal Service.虽然也有基于GTK+的tsc ...
- 【读书笔记】读《JavaScript设计模式》之适配器模式
一.定义 适配器模式可用来在现有接口和不兼容的类之间进行匹配.使用这种模式的对象又叫包装器(wrapper),因为它们是在用一个新的接口包装另一个对象.在设计类的时候旺旺会遇到有些接口不能与现有API ...
- error: Refusing to undefine while domain managed save image exists
[root@ok libvirt]# virsh undefine win7 error: Refusing to undefine while domain managed save image e ...
- js prototype
1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展.我们称B的原型为A. 2 javasc ...
- mac上安装gradle
首先,先download最新版本的gradle,网址如下:http://www.gradle.org/get-started然后将下载下来的zip包放在你要安装的路径上,我安装在/usr/local/ ...
- google svn 服务器使用(免费SVN服务器)
转自:http://hi.baidu.com/%C0%AF%B1%CA%B9%A4%D7%F7%CA%D2/blog/item/d6f6c6d7707d81d0a044df5f.html 1. 进入h ...
- POJ 3461 Oulipo KMP
题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...
- 抓包工具Fiddler的使用
Fiddler 教程 Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发 ...
- oracle创建临时表没有权限
执行下面: grant create any table to 用户名称