【luogu P3368 树状数组2】 模板
2024-08-28 11:16:18
题目链接:https://www.luogu.org/problemnew/show/P3368
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int tree[],a[];
int lowbit(int x)
{
return x&-x;
}
void add(int k,int num)//给k位置的数值加num
{
while(k<=n)
{
tree[k]+=num;
k+=lowbit(k);
}
}
int query(int x)
{
int sum=;
for(int i=x;i>;i-=i&(-i)) sum+=tree[i];
return sum;
} int main()
{
cin>>n>>m;
for(int i=;i<=n;i++) cin>>a[i];
while(m--)
{
int k;
cin>>k;
if(k==)
{
int x,y,z;
cin>>x>>y>>z;
add(x,z); //维护查分数组
add(y+,-z);
}
else
{
int x;
cin>>x;
cout<<a[x]+query(x)<<endl; //query()求的是改变的值,再加上原来的值就可以了
}
}
return ;
}
最新文章
- 解决Can&#39;t connect to MySQL server on &#39;localhost&#39; (10048)
- JQ_浏览器窗口改变触发
- @property? @property?说说他身后的事情。
- 在filter中使用spring的service bean
- c# 垮线程调用控件
- MySQL配置
- C# 参考之方法参数关键字:params、ref及out
- 20145235李涛 《Java程序设计》第3周学习总结
- BZOJ 3199 escape
- 事件日志ID 2511:服务器服务无法重新创建 <;sharename>; 共享关系,因为 <;address>; 目录已不再存在
- Connection to https://dl-ssl.google.com refused 解决方案
- transfrom属性
- Qweb报表中的Reload from Attachment参数
- tinkphp5.0 traits 的引入
- ORACLE 本地数据库存储过程 调用远程数据库存储过程
- Sublime Text 3 快捷键总结(拿走)
- 不要在对抽象类接口abstract virtual什么的混淆不清了
- cookie、session、sessionid ,jsessionid 的区别
- JQ 使用模板
- BZOJ4010: [HNOI2015]菜肴制作(拓扑排序 贪心)