[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4397

[算法]

树状数组

时间复杂度 : O(QlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 int n , q; struct Binary_Indexed_Tree
{
int c[MAXN];
inline int lowbit(int x)
{
return x & (-x);
}
inline void modify(int x , int val)
{
for (int i = x; i <= n; i += lowbit(i)) c[i] += val;
}
inline int query(int x)
{
int ret = ;
for (int i = x; i; i -= lowbit(i)) ret += c[i];
return ret;
}
inline int query(int l , int r)
{
return query(r) - query(l - );
}
} bit1 , bit2 , bit3; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
int main()
{ read(n); read(q);
for (int i = ; i <= n; i++)
{
int x;
read(x);
if (x == ) bit1.modify(i , );
else if (x == ) bit2.modify(i , );
else bit3.modify(i , );
}
while (q--)
{
int l , r;
read(l); read(r);
printf("%d %d %d\n",bit1.query(l , r) , bit2.query(l , r) , bit3.query(l , r));
} return ; }

最新文章

  1. C#编程:SqlCommand.Parameters.Add()方法的参数问题。
  2. 由java的八个基本数据类型说开去
  3. HANA Studio打开系统显示Secure storage is locked
  4. PHP程序员如何突破成长瓶颈
  5. linq分页扩展(转)
  6. HW4.3
  7. zabbix之2安装编译/基本功能实现
  8. ubantu root 默认密码
  9. git merge,rebase和*(no branch)
  10. 设计模式二 适配器模式 adapter
  11. appach-maven-3.5.0配置与下载
  12. jenkins学习之多项目构建
  13. C语言作业--数据类型
  14. java--基本数据类型的转换(强制转换)
  15. win10关不了机解决办法以及win10怎么禁止开机启动项
  16. 向dnsrecord.txt 中添加 配置
  17. c 时间转移函数
  18. 微信小程序模拟点击出现问题解决方法
  19. Cordova安装、设置代理和引入插件
  20. mint-ui之Swipe使用

热门文章

  1. java常见问题集锦
  2. [转]ORA-38500: USING CURRENT LOGFILE option not available without stand
  3. [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)
  4. PHP复制和移动目录
  5. C++内存分配方式(——选自:C++内存管理技术内幕)
  6. msp430项目编程16
  7. msp430入门编程21
  8. POJ 2348 Euclid&#39;s Game【博弈】
  9. Go --- 设计模式(工厂模式)
  10. 【Nginx】如何使用http配置