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 ...
随机推荐
- Maven运行时异常java.lang.UnsupportedClassVersionError的解决方案
我用的Maven版本为最新版:3.3.9,但是我执行一个简单的clean命令会报如下错误: Exception in thread "main" java.lang.Unsuppo ...
- UESTC 1215 (思维题 旋转)
Secrete Master Plan Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- NEFU 2016省赛演练一 F题 (高精度加法)
Function1 Problem:F Time Limit:1000ms Memory Limit:65535K Description You know that huicpc0838 has b ...
- ecshop绕过验证码暴力破解
若验证码不匹配,并没有销毁当前验证码 所以就可以一次请求验证码图片后,只要不再刷新验证码就可以一直使用 1.获取正确的验证码 2. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- opencv学习笔记(五)镜像对称
opencv学习笔记(五)镜像对称 设图像的宽度为width,长度为height.(x,y)为变换后的坐标,(x0,y0)为原图像的坐标. 水平镜像变换: 代码实现: #include <ios ...
- opencv学习笔记(四)投影
opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...
- .net学习笔记----二级域名站点共享Session状态
前面一篇文章提到了如何在使用了ASP.NET form authentication的二级站点之间共享登陆状态, http://www.cnblogs.com/jzywh/archive/2007/0 ...
- mysql中char,varchar与text类型的区别和选用
关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...
- 微信聊天测试脚本 wx_sample.php
<?php </FuncFlag> </xml>); curl_setopt($ch, CURLO ...
- c语言 struct 的初始化
转自:http://www.cnblogs.com/silentjesse/archive/2013/07/30/3225212.html struct数据有3中初始化方法:顺序,C风格及C++风格的 ...