• [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 is

    0 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(线段树单点更新区间查询)的更多相关文章

  1. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  2. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  3. hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询

    单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...

  4. HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. HDU1166(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. CDOJ 1073 线段树 单点更新+区间查询 水题

    H - 秋实大哥与线段树 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit S ...

  7. Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)

    预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...

  8. HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场

    题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...

  2. [SVN(ubuntu)] svn 文件状态标记含义

    A item 文件.目录或是符号链item预定加入到版本库. C item 文件item发生冲突,在从服务器更新时与本地版本发生交迭,在你提交到版本库前,必须手工的解决冲突. D item 文件.目录 ...

  3. 手把手教你cuda5.5与VS2010的编译环境搭建

    参考:http://www.cnblogs.com/xing901022/archive/2013/08/09/3248469.html 目前版本的cuda是很方便的,它的一个安装里面包括了Toolk ...

  4. redis的PHP扩展包安装方法

    试用Redis安装.php环境连接.测试  Redis介绍     Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解 ...

  5. XTU 1242 Yada Number 容斥

    Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...

  6. 【codevs1191】数轴染色 线段树 区间修改+固定区间查询

    [codevs1191]数轴染色 2014年2月15日4317 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[L ...

  7. 【poi】用POI新建一个xlsx文件【或者说将数据存入到xlsx中】/【将数据从xlsx中获取到项目中】

    第一部分:写入xlsx中 使用POI创建一个xlsx文件: 项目结构如下: 具体使用的POI中的 XSSFWorkbook   xlsx对象 Sheet 工作簿对象 Row 行对象 Cell  单元格 ...

  8. CodeForces 304C

    E - E Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. 二分查找法 java

    前几天去面试,让我写二分查找法,真是哔了狗! 提了离职申请,没事写写吧! 首先二分查找是在一堆有序的序列中找到指定的结果. public class Erfen { public static int ...

  10. node相关--代码共享

    代码共享问题: 是否值得在两个环境中运行同一份代码: //看项目 依赖的API是否在两个环境中都有或有替代: 浏览器提供的标准API:XMLHttpRequest.WebSocket.DOM.canv ...