HDU -1166 线段树
#include <algorithm>
#include <iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<map>
#include<set> #define M 50000
#define inf 0x3f3f3f3f
typedef long long ll; using namespace std; struct Data
{
int left, right, sum, lazy;
}tree[M*];
int n;
int sum, lazy;
int x, y;
string str; void built(int l,int r,int k) {
tree[k].left = l, tree[k].right = r;
if (l == r) {
scanf("%d", &tree[k].sum);
return;
}
int mid = (l + r) / ;
built(l, mid,k*);
built(mid + , r, k * + );
tree[k].sum = tree[k * ].sum + tree[k * + ].sum;
} void down(int k) {
lazy = tree[k].lazy;
tree[k].lazy = ;
tree[k * ].lazy += lazy;
tree[k * + ].lazy += lazy;
tree[k * ].sum += (tree[k * ].right - tree[k * ].left + ) * lazy;
tree[k * + ].sum += (tree[k * + ].right - tree[k * + ].left + ) * lazy;
} void search(int k) {
if (tree[k].left >=x && tree[k].right <=y) {
sum += tree[k].sum;
return;
}
if (tree[k].lazy)down(k);
int mid = (tree[k].left + tree[k].right) / ;
if (x <= mid)search(k * );
if (y > mid)search(k * + );
} void change_inv(int k) {
if (tree[k].left == tree[k].right) {
//tree[k].lazy += add;
tree[k].sum += y;
return;
}
if (tree[k].lazy)down(k);
int mid = (tree[k].left + tree[k].right) / ;
if (x <= mid)change_inv(k * );
else change_inv(k * + );
tree[k].sum = tree[k * ].sum + tree[k * + ].sum;
} int main() {
int T;
cin >> T;
for (int Case = ; Case <= T; Case++) {
scanf("%d", &n);
built(, n, );
printf("Case %d:\n", Case);
while (true){
cin >> str;
if (str == "End")break;
scanf("%d%d", &x, &y);
if (str == "Add") {
change_inv();
}
else if (str == "Sub") {
y *= -;
change_inv();
}
else if (str == "Query") {
sum = ;
search();
printf("%d\n", sum);
}
}
}
return ;
}
HDU -1166 线段树的更多相关文章
- hdu 1166 线段树(sum+单点修改)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 敌兵布阵 HDU 1166 线段树
敌兵布阵 HDU 1166 线段树 题意 这个题是用中文来描写的,很简单,没什么弯. 解题思路 这个题肯定就是用线段树来做了,不过当时想了一下可不可用差分来做,因为不熟练就还是用了线段树来做,几乎就是 ...
- HDU(1166),线段树模板,单点更新,区间总和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后, ...
- hdu 1166 线段树单点更新
等线段树复习完再做个总结 1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Case 1:633 ...
- A - 敌兵布阵 HDU - 1166 线段树(多点修改当单点修改)
线段树板子题练手用 #include<cstdio> using namespace std; ; int a[maxn],n; struct Node{ int l,r; long lo ...
- hdu 1166 线段树(单点增减 区间求和)
Sample Input1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Sample Outp ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1166 线段树模板&树状数组模板
HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...
- hdu 1166 线段树 奇兵布阵
#include<iostream> using namespace std; ; )*];//n个叶子就有2*n-4*n个节点 ]; int n; void getup(int root ...
- hdu 1166 线段树 区间求和 +单点更新 CD模板
题目链接 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
随机推荐
- com.alibaba.druid.pool.DruidDataSource
https://www.cnblogs.com/wuyun-blog/p/5679073.html DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PR ...
- Profiling Top Kagglers: Bestfitting, Currently #1 in the World
We have a new #1 on our leaderboard – a competitor who surprisingly joined the platform just two yea ...
- 国内OLED产业与三星到底是差之千里还是近在咫尺?
此前,市面上几乎大部分智能手机搭载的刘海屏,都是来自三星的AMOLED屏幕.虽然三星总是被诟病为中国手机厂商提供的是"次品",不过没办法,OLED屏幕的核心技术.产能等都掌握在三星 ...
- Java TCP发送与接收
IP地址?端口号?主机名? 什么是Socket? 什么是UDP? 什么是TCP? UDP和TCP区别? 以上问题请自行百度,有标准解释,此处不再赘述,直接上干货! 实例: 发送端: public cl ...
- you-get使用
1.pip install you-get 2.如果出错 查看错误bug you-get http://www.iqiyi.com/v_19rrnqxz7k.html#vfrm=2-4-0-1 ...
- HTML-基础标记
HTML, 一种超文本标记语言,顾名思义,要比文本的样式多,而且是由标记组成,还是一门语言. 标记写法 <标记名> <a></a>双标记 超链接 <br /& ...
- SSM框架和微服务构架和的联系与区别
spring和springMvc: 1. spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc).业务层(Ioc) ...
- 【数据结构】Hash表简介及leetcode两数之和python实现
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...
- vue表单选项框
选项框选的内容在下面显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- Nginx无法监听虚拟VIP的问题报:99: Cannot assign requested address
99: Cannot assign requested address #本地网卡上没有10.0.0.3这个IPNginx就会报错: [root@lb01 conf]# /application/ng ...