[Usaco2015DEC] Breed Counting
2024-08-26 20:46:28
[题目链接]
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 ; }
最新文章
- C#编程:SqlCommand.Parameters.Add()方法的参数问题。
- 由java的八个基本数据类型说开去
- HANA Studio打开系统显示Secure storage is locked
- PHP程序员如何突破成长瓶颈
- linq分页扩展(转)
- HW4.3
- zabbix之2安装编译/基本功能实现
- ubantu root 默认密码
- git merge,rebase和*(no branch)
- 设计模式二 适配器模式 adapter
- appach-maven-3.5.0配置与下载
- jenkins学习之多项目构建
- C语言作业--数据类型
- java--基本数据类型的转换(强制转换)
- win10关不了机解决办法以及win10怎么禁止开机启动项
- 向dnsrecord.txt 中添加 配置
- c 时间转移函数
- 微信小程序模拟点击出现问题解决方法
- Cordova安装、设置代理和引入插件
- mint-ui之Swipe使用
热门文章
- java常见问题集锦
- [转]ORA-38500: USING CURRENT LOGFILE option not available without stand
- [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)
- PHP复制和移动目录
- C++内存分配方式(——选自:C++内存管理技术内幕)
- msp430项目编程16
- msp430入门编程21
- POJ 2348 Euclid&#39;s Game【博弈】
- Go --- 设计模式(工厂模式)
- 【Nginx】如何使用http配置