hdu 1166 敌兵布阵(树状数组 or 线段树)

2024-09-09 16:48

本文主要是介绍hdu 1166 敌兵布阵(树状数组 or 线段树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意是求一个线段的和,在线段上可以进行加减的修改。

树状数组的模板题。


代码:

#include <stdio.h>
#include <string.h>
const int maxn = 50000 + 1;int c[maxn];
int n;int lowbit(int x)
{return x & -x;
}void add(int x, int num)
{while (x <= n){c[x] += num;x += lowbit(x);}
}void sub(int x, int num)
{while (x <= n){c[x] -= num;x += lowbit(x);}
}int query(int x)
{int res = 0;while (x > 0){res += c[x];x -= lowbit(x);}return res;
}int main()
{
#ifdef LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALint ncase;scanf("%d", &ncase);for (int t = 1; t <= ncase; t++){memset(c, 0, sizeof(c));scanf("%d", &n);for (int i = 1; i <= n; i++){int tmp;scanf("%d", &tmp);add(i, tmp);}char ch[6];printf("Case %d:\n", t);while (1){scanf("%s", ch);getchar();if (strcmp(ch, "Query") == 0){int from, to;scanf("%d%d", &from, &to);printf("%d\n", query(to) - query(from - 1));continue;}if (strcmp(ch, "Add") == 0){int x, num;scanf("%d%d", &x, &num);add(x, num);continue;}if (strcmp(ch, "Sub") == 0){int x, num;scanf("%d%d", &x, &num);sub(x, num);continue;}if (strcmp(ch, "End") == 0){break;}}}return 0;
}


2015.4.20

线段树:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
const int maxn = 50000 + 10;LL sum[maxn << 2];void pushup(int rt)
{sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}void build(int lo, int hi, int rt)
{if (lo == hi){scanf("%d", &sum[rt]);return;}int mi = (lo + hi) >> 1;build(lson);build(rson);pushup(rt);
}void update(int pos, int add, int lo, int hi, int rt)
{if (lo == hi){sum[rt] += add;return;}int mi = (lo + hi) >> 1;if (pos <= mi)update(pos, add, lson);elseupdate(pos, add, rson);pushup(rt);
}LL query(int L, int H, int lo, int hi, int rt)
{if (L <= lo && hi <= H)return sum[rt];int mi = (lo + hi) >> 1;int res = 0;if (L <= mi)res += query(L, H, lson);if (mi < H)res += query(L, H, rson);return res;
}int main()
{
#ifdef LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALint ncase;scanf("%d", &ncase);for (int t = 1; t <= ncase; t++){int n;scanf("%d", &n);build(1, n, 1);char ch[6];printf("Case %d:\n", t);while (1){scanf("%s", ch);getchar();if (strcmp(ch, "Query") == 0){int from, to;scanf("%d%d", &from, &to);printf("%d\n", query(from, to, 1, n, 1));continue;}if (strcmp(ch, "Add") == 0){int x, num;scanf("%d%d", &x, &num);update(x, num, 1, n, 1);continue;}if (strcmp(ch, "Sub") == 0){int x, num;scanf("%d%d", &x, &num);update(x, -num, 1, n, 1);continue;}if (strcmp(ch, "End") == 0){break;}}}return 0;
}


这篇关于hdu 1166 敌兵布阵(树状数组 or 线段树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1151723

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c