HDU 3954 Level up(线段树)
HDU 3954 Level up
题意:k个等级,n个英雄,每一个等级升级有一定经验,每次两种操作,一个区间加上val,这样区间内英雄都获得当前等级*val的经验,还有一个操作询问区间经验最大值
思路:由于等级少,所以每一个结点用Max[10]记录下每一个等级的最大值,假设有一个升级就一直找究竟,由于一个英雄升级最多10次,所以这个操作最多就10W次能够接受,剩下就是普通的区间改动区间查询的延迟操作了
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 10005;
const int M = 11; int t, n, k, qw, need[M]; #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) struct Node {
int l, r, Max[M], add;
bool cover;
} node[N * 4]; void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r;
memset(node[x].Max, -1, sizeof(node[x].Max));
node[x].Max[1] = 0;
node[x].add = 0;
node[x].cover = false;
if (l == r) return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} bool judge(int x, int v) {
for (int i = 1; i < k; i++) {
if (node[x].Max[i] == -1) continue;
if (node[x].Max[i] + i * v >= need[i + 1]) return true;
}
return false;
} void pushup(int x) {
node[x].cover = (node[lson(x)].cover && node[rson(x)].cover);
for (int i = 1; i <= k; i++)
node[x].Max[i] = max(node[lson(x)].Max[i], node[rson(x)].Max[i]);
} void pushdown(int x) {
if (node[x].add) {
node[lson(x)].add += node[x].add;
node[rson(x)].add += node[x].add;
for (int i = 1; i <= k; i++) {
if (node[lson(x)].Max[i] != -1)
node[lson(x)].Max[i] += node[x].add * i;
if (node[rson(x)].Max[i] != -1)
node[rson(x)].Max[i] += node[x].add * i;
}
node[x].add = 0;
}
} void add(int l, int r, int v, int x = 0) {
if (node[x].l >= l && node[x].r <= r && (node[x].cover || !judge(x, v))) {
node[x].add += v;
for (int i = 1; i <= k; i++) {
if (node[x].Max[i] == -1) continue;
node[x].Max[i] += i * v;
}
return;
}
if (node[x].l == node[x].r) {
int have;
for (int i = 1; i < k; i++) {
if (node[x].Max[i] != -1) {
have = node[x].Max[i] + i * v;
break;
}
}
memset(node[x].Max, -1, sizeof(node[x].Max));
for (int i = 2; i <= k; i++) {
if (have < need[i]) {
node[x].Max[i - 1] = have;
return;
}
}
node[x].Max[k] = have;
node[x].cover = true;
return;
}
int mid = (node[x].l + node[x].r) / 2;
pushdown(x);
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
} int query(int l, int r, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
for (int i = k; i >= 1; i--) {
if (node[x].Max[i] == -1) continue;
return node[x].Max[i];
}
}
int mid = (node[x].l + node[x].r) / 2;
int ans = 0;
pushdown(x);
if (l <= mid) ans = max(ans, query(l, r, lson(x)));
if (r > mid) ans = max(ans, query(l, r, rson(x)));
pushup(x);
return ans;
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
printf("Case %d:\n", ++cas);
scanf("%d%d%d", &n, &k, &qw);
for (int i = 2; i <= k; i++)
scanf("%d", &need[i]);
build(1, n);
char op[15];
int a, b, c;
while (qw--) {
scanf("%s%d%d", op, &a, &b);
if (op[0] == 'W') {
scanf("%d", &c);
add(a, b, c);
} else printf("%d\n", query(a, b));
}
printf("\n");
}
return 0;
}
HDU 3954 Level up(线段树)的更多相关文章
- hdu 3954 Level up(线段树)
题目链接:hdu 3954 Level up 题目大意:N个英雄,M个等级,初始等级为1,给定每一个等级须要的经验值,Q次操作,操作分两种,W l r x:表示l~r之间的英雄每一个人杀了x个怪物:Q ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 3954 Level up(多颗线段树+lazy操作)
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...
- HDU 4107 Gangster(线段树 特殊懒惰标记)
两种做法. 第一种:标记区间最大值和最小值,若区间最小值>=P,则本区间+2c,若区间最大值<P,则本区间+c.非常简单的区间更新. 最后发一点牢骚:最后query查一遍就行,我这个2B竟 ...
- HDU 5091---Beam Cannon(线段树+扫描线)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
随机推荐
- vs2008中使用gdi+的设置
vs2008中使用gdi+ 1.新建一个mfc工程 2.在stdafx.h文件中加入以下几行语句: #include <gdiplus.h> //#pragm ...
- Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...
- Shell 传递参数
Shell 传递参数 向脚本传递参数,格式为:$n. 向脚本传递三个参数,并分别输出: echo "Shell 传递参数实例!"; echo "第一个参数为:$1&quo ...
- c++ __declspec关键字详细用法
c++ __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的 ...
- 手把手教你修改pcduino系统默认的音频输出
最近要搞个小玩意儿,要用到pcduino的音频输出,但是系统默认的是输出到hdmi的音频,我的显示器上没有喇叭,只能搞个USB声卡.但是系统默认又不是输出到USB,这里我手把手叫你怎么设置系统默认声卡 ...
- setStyleSheet来设定窗口部件的样式
使用setStyleSheet来设置图形界面的外观:QT Style Sheets是一个很有利的工具,允许定制窗口的外观,此外还可以用子类QStyle来完成,他的语法很大比重来源于html的CSS,但 ...
- RR模式下的事务隔离
<pre name="code" class="html">mysql> select * from t100; Session 2: +-- ...
- 产生n不同随机数的算法
昨天无聊,就模仿仙剑5外传中的卡牌游戏做了一个小游戏,结果在开发这个小游戏的时候,碰到了产生多个不同随机数的问题.我们知道,仙剑中的卡牌游戏是随机产生16张图片,并且这16张图片是两个一组的,因为只有 ...
- java web从零单排第十六期《struts2》控制标签(2)
1.s:subset标签概述: s:subset标签功能是从一个集合中取出部分元素合并成一个新的集合,新生成的这个集合是原来集合的子集.属性和意义如下: 属性名 是否必需 默认值 类型 说明介绍 co ...
- C++不确定行为
一个简单的程序引发了一块让人纠结的领域,也许强调编程规范的重要性也在这把.规范了就easy避免一些问题. 程序是这种 int Change(int& a) { a = 4; return a; ...