Codeforce GYM 100741 A. Queries
A. Queries
time limit per test
0.25 s
memory limit per test
64 MB
input
standard input
output
standard output
Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my friend, a mathematician, thinks that it is very fun to play with a sequence of integer numbers. He writes the sequence in a row. If he wants he increases one number of the sequence, sometimes it is more interesting to decrease it (do you know why?..) And he likes to add the numbers in the interval [l;r]. But showing that he is really cool he adds only numbers which are equal some mod (modulo m).
Guess what he asked me, when he knew that I am a programmer? Yep, indeed, he asked me to write a program which could process these queries (n is the length of the sequence):
- + p r It increases the number with index p by r. (
,
) You have to output the number after the increase.
- - p r It decreases the number with index p by r. (
,
) You must not decrease the number if it would become negative. You have to output the number after the decrease.
- s l r mod You have to output the sum of numbers in the interval
which are equal mod (modulo m). (
) (
)
Input
The first line of each test case contains the number of elements of the sequence n and the number m. (1 ≤ n ≤ 10000) (1 ≤ m ≤ 10)
The second line contains n initial numbers of the sequence. (0 ≤ number ≤ 1000000000)
The third line of each test case contains the number of queries q (1 ≤ q ≤ 10000).
The following q lines contains the queries (one query per line).
Output
Output q lines - the answers to the queries.
Examples
input
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
output
2
3
1
看一下数据范围,
m<=20
果断开20个树状数组
注意:要开long long
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<cstring>
6 #include<algorithm>
7 #define lli long long int
8 using namespace std;
9 const lli MAXN=10001;
10 void read(lli &n)
11 {
12 char c='+';lli x=0;bool flag=0;
13 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
14 while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();
15 n=flag==1?-x:x;
16 }
17 lli n,m;
18 struct node
19 {
20 lli a[MAXN];
21 lli lowbit(lli x){return x&(-x);}
22 lli change(lli pos,lli val)
23 {
24 for(;pos<=n;pos+=lowbit(pos))
25 a[pos]+=val;
26 }
27 lli query(lli l,lli r)
28 {
29 return ask(r)-ask(l-1);
30 }
31 lli ask(lli pos)
32 {
33 lli ans=0;
34 for(;pos;pos-=lowbit(pos))
35 ans+=a[pos];
36 return ans;
37 }
38 node(){memset(a,0,sizeof(a));}
39 }bit[21];
40 lli date[MAXN];
41 int main()
42 {
43 read(n);read(m);
44 for(lli i=1;i<=n;i++)
45 {
46 read(date[i]);
47 bit[date[i]%m].change(i,date[i]);
48 }
49 lli T;read(T);
50 while(T--)
51 {
52 char how;cin>>how;
53 if(how=='+')
54 {
55 lli p,num;read(p);read(num);
56 bit[date[p]%m].change(p,-date[p]);
57 date[p]+=num;
58 bit[date[p]%m].change(p,date[p]);
59 printf("%lldn",date[p]);
60 }
61 else if(how=='-')
62 {
63 lli p,num;read(p);read(num);
64 if(date[p]<num)
65 printf("%lldn",date[p]);
66 else
67 {
68 bit[date[p]%m].change(p,-date[p]);
69 date[p]-=num;
70 bit[date[p]%m].change(p,date[p]);
71 printf("%lldn",date[p]);
72 }
73
74 }
75 else if(how=='s')
76 {
77 lli l,r,mod;read(l);read(r);read(mod);
78 printf("%lldn",bit[mod].query(l,r));
79 }
80 }
81 return 0;
82 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 支付业务优化else if 代码
- 在UBUNTU虚拟机上安装R软件包
- R语言逻辑回归和泊松回归模型对发生交通事故概率建模
- 万恶的NPE差点让我半个月工资没了
- R语言非参数模型厘定保险费率:局部回归、广义相加模型GAM、样条回归
- 大白话java多线程,高手勿入
- R语言小数定律的保险业应用:泊松分布模拟索赔次数
- R语言中自编基尼系数的CART回归决策树的实现
- ArrayList的删除姿势你都掌握了吗
- sas神经网络:构建人工神经网络模型来识别垃圾邮件
- R语言多分类logistic逻辑回归模型在混合分布模拟单个风险损失值评估的应用
- 10分钟带你入门git到github
- 微服务[学成在线] day18:基于oauth2实现RBAC认证授权、微服务间认证实现
- 【TBase开源版测评】分布式事务全局一致性
- R语言进阶之主成分分析