• [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. hadoop的关键进程

    hadoop集群中主要进程有master:   NameNode, ResourceManager,slaves:   DataNode, NodeManager,  RunJar, MRAppMas ...

  2. dbca no protocol support

    http://blog.itpub.net/26937943/viewspace-1325094/

  3. wpa_supplicant.conf

    转自:http://w1.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/wpa_supplicant.conf ### ...

  4. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Xamarin.Android开发实践(七)

    Xamarin.Android广播接收器与绑定服务 一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架 ...

  6. Scala入门

    搭建环境请参考: http://www.cnblogs.com/super-d2/p/4534208.html 1.交互式编程: adeMacBook-Pro:ssdb-master apple$ s ...

  7. PHP 判断数组里的值是否有存在

     一维数组检测<?php  $a = array('as','ddf','ddf','as','qwe','wer','ert'); $res = array_unique($a); var_d ...

  8. TypeC一个微软开发的超简单.NET依赖注入/IoC容器

    控制反转(IoC,Inversion of Control)是由Martin Fowler总结出来的一种设计模式,用来减少代码间的耦合.一般而言,控制反转分为依赖注入(Dependency Injec ...

  9. 关于html5不支持frameset的解决方法

    转自:http://blog.sina.com.cn/s/blog_b2813a790101ejvf.html html5已经不支持frameset了,很郁闷,看了大家的解决方法,无非就是两种1. 使 ...

  10. keytool生成证书与Tomcat SSL配置

    转自:http://tomhat.iteye.com/blog/2087673 一.Keytool介绍 Keytool是一个Java数据证书的管理工具.Keytool将密钥(key)和证书(certi ...