链接:

https://vjudge.net/problem/Gym-100741A

题意:

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). () ()

思路:

看半天没看懂题.

建10个树状数组即可, 对模m的每种情况分别统计.

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. //#include <memory.h>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <algorithm>
  10. #include <math.h>
  11. #include <stack>
  12. #include <string>
  13. #include <assert.h>
  14. #include <iomanip>
  15. #define MINF 0x3f3f3f3f
  16. using namespace std;
  17. typedef long long LL;
  18. const int INF = 1e9;
  19. const int MAXN = 1e4+10;
  20. LL A[MAXN], C[15][MAXN];
  21. int n, m, q;
  22. int Lowbit(int x)
  23. {
  24. return x&(-x);
  25. }
  26. void Add(int pos, int mod, LL val)
  27. {
  28. while (pos <= n)
  29. {
  30. C[mod][pos] += val;
  31. pos += Lowbit(pos);
  32. }
  33. }
  34. LL Query(int pos, int mod)
  35. {
  36. LL ans = 0;
  37. while (pos > 0)
  38. {
  39. ans += C[mod][pos];
  40. pos -= Lowbit(pos);
  41. }
  42. return ans;
  43. }
  44. int main()
  45. {
  46. scanf("%d%d", &n, &m);
  47. for (int i = 1;i <= n;i++)
  48. {
  49. scanf("%lld", &A[i]);
  50. Add(i, A[i]%m, A[i]);
  51. }
  52. scanf("%d", &q);
  53. char opt[5];
  54. int l, r, mod;
  55. while (q--)
  56. {
  57. scanf("%s", opt);
  58. if (opt[0] == 's')
  59. {
  60. scanf("%d%d%d", &l, &r, &mod);
  61. printf("%lld\n", Query(r, mod)-Query(l-1, mod));
  62. }
  63. else if (opt[0] == '+')
  64. {
  65. scanf("%d%d", &l, &r);
  66. Add(l, A[l]%m, -A[l]);
  67. A[l] += r;
  68. Add(l, A[l]%m, A[l]);
  69. printf("%lld\n", A[l]);
  70. }
  71. else
  72. {
  73. scanf("%d%d", &l, &r);
  74. Add(l, A[l]%m, -A[l]);
  75. if (r <= A[l])
  76. A[l] -= r;
  77. Add(l, A[l]%m, A[l]);
  78. printf("%lld\n", A[l]);
  79. }
  80. }
  81. return 0;
  82. }

Gym-10071A-Queries(树状数组)的更多相关文章

  1. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  2. Codeforces 369E Valera and Queries --树状数组+离线操作

    题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...

  3. Gym - 101755G Underpalindromity (树状数组)

    Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...

  4. CF Gym 100463A (树状数组求逆序数)

    题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...

  5. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  6. GYM 101889F(树状数组)

    bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...

  7. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  8. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  9. Gym 101908C - Pizza Cutter - [树状数组]

    题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...

  10. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

随机推荐

  1. Django视图之FBV与CBV

    一. CBV与FBV CBV:Class Based View FBV:Function Based View 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的,那就是C ...

  2. nginx reload的原理

    nginx启动时,会启动两个进程: 一个是Master进程和worker进程.改变配置后nginx做的事1)改变了nginx配置之后,HUP signal的信号需要发送给主进程.2)主进程首先会检测新 ...

  3. WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用)

    WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用) Win7部署基础知识(8):使用WDS捕获与应用映像  一.添加映像组 使用WDS捕获映像时,会将映像加载到WDS服务器的映像 ...

  4. C#DataGridView格式化显示单元格的内容

    今天又发现了一个很有用的东西,DataGridView的CellFormating事件 经常从数据库查到的原始数据需要经过转换之后显示在客户端,比如性别,“1”显示“男”,“0”显示“女”,为此经常将 ...

  5. linux 下文件上传的两种工具(XFTP5和Putty之pscp)方式

    一.使用XFTP(,需要先在LINUX上安装启用FTP服务) 然后,在WINDOWS上启动XFPT6客户端,将下载的文件上传至LINUX 指定目录: 二.使用PUTTY软件安装目录下的PSCP命令 1 ...

  6. C++ unsigned long 转化为 unsigned char*

    C++ Code 123456789101112131415161718   unsigned long lFileLen = 1000; unsigned char *ucFileLenFlag; ...

  7. MYSQL—第二部分(Linux版本的安装和数据表的操作)

    Linux版本的安装(过于简单了) 安装: ? 1 yum install mysql-server 服务端启动 ? 1 mysql.server start 客户端连接 ? 1 2 3 4 5 6 ...

  8. spark教程(12)-生态与原理

    spark 是目前非常流行的大数据计算框架. spark 生态 Spark core:包含 spark 的基本功能,定义了 RDD 的 API,其他 spark 库都基于 RDD 和 spark co ...

  9. python 写接口供外部调用

    .py: import requests import urllib2 import commands import subprocess def check(): status, msg = com ...

  10. 安装gitlab ce

    切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...