》》点击进入原题测试《《

思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记。入门题

cin,cout会超时,记得加std::ios::sync_with_stdio(false);

#include<string>
#include<iostream> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int maxn = 5e4 + ; int add[maxn << ];
int tree[maxn << ]; void PushDown(int rt)
{
if (add[rt]){
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
tree[rt << ] += add[rt];
tree[rt << | ] += add[rt];
add[rt] = ;
}
}
void build(int l, int r, int rt)
{
if (l == r){
cin >> tree[rt];
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
}
void update(int x, int ans, int l, int r, int rt)
{
if (l == r){
tree[rt] += ans;
return;
}
int m = (l + r) >> ;
if (x <= m)update(x, ans, lson);
else update(x, ans, rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R){
return tree[rt];
}
int m = (l + r) >> , ans = ;
if (L <= m)ans += query(L, R, lson);
if (R > m)ans += query(L, R, rson);
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
int t, ca = ;
cin >> t;
while (t--){ int n; cin >> n;
build(, n, );
string flag; int i, j;
cout << "Case " << ca++ << ":" << endl;
while (cin >> flag, flag != "End"){
cin >> i >> j;
if (flag == "Add"){
update(i, j, , n, );
}
else if (flag == "Sub"){
update(i, - * j, , n, );
}
else if (flag == "Query"){
cout << query(i, j, , n, ) << endl;
} } }
return ;
}

最新文章

  1. Spring boot开发过程遇到的一些小问题
  2. 5分钟教你Windows 10中将“运行”固定到开始菜单
  3. socket_server源码剖析、python作用域、IO多路复用
  4. 用python自定义实现db2的连接池
  5. 修改Chrome默认搜索引擎为Google.com
  6. Fedora20 编译安装qemu-system
  7. 进入GRUB改root用户密码
  8. 特现C语言编程特点的小代码,itoa,数值转换成字符串
  9. 【转】APUE习题4.6---测试lseek作用
  10. delphi xe5 android tts(Text To Speech)
  11. C#调用IOS推送
  12. .net+easyui系列--验证框
  13. 使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework
  14. 【jQuery】使用JQ来编写面板的淡入淡出效果
  15. theOS环境搭建
  16. Hashtable、synchronizedMap、ConcurrentHashMap 比较
  17. vue小demo易错点总结
  18. 设计模式之模板方法(Template Method)
  19. PL/SQL Developer从11.0.6版本开始32/64为之区分
  20. DateTables的服务器分页

热门文章

  1. bzoj2440 [中山市选2011]完全平方数——莫比乌斯+容斥
  2. easyui 生成tas方式
  3. js获取下拉框当前选中的值并弹出
  4. bzoj 1593: [Usaco2008 Feb]Hotel 旅馆【线段树】
  5. vue-cli 升级至 webpack 4 指北
  6. Spark之RDD的定义及五大特性
  7. spring data elasticsearch的 @Documnet 和 @Field 注解
  8. 状态压缩+枚举 UVA 11464 Even Parity
  9. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
  10. dos 下小tip