题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027

题目:

题意:n个数,每次区间更新将其数值变成它的根号倍(向下取整),区间查询数值和。

思路:易知这题的lazy标记是不好打的,但是我们发现当这个数取根号直到变成1时就不需要更新了,像这种无法打标记并且经过多次操作后就不需要操作的线段树叫做势能线段树。在需要更新时我们一直暴力到它的叶子节点,当某个区间无法更新时我们将其的flag设为0,对于某个节点的flag就等于它的左右子节点的flag取或。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e8;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; inline int read() {//读入挂
int ret = , c, f = ;
for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
if(c == '-') f = -, c = getchar();
for(; isdigit(c); c = getchar()) ret = ret * + c - '';
if(f < ) ret = -ret;
return ret;
} int n, q, op, x, y; struct node {
int l, r, flag;
ll sum;
}segtree[maxn*]; void push_up(int i) {
segtree[i].sum = segtree[i*].sum + segtree[i*+].sum;
segtree[i].flag = segtree[i*].flag | segtree[i*+].flag;
} void build(int i, int l, int r) {
segtree[i].l = l, segtree[i].r = r, segtree[i].flag = ;
if(l == r) {
scanf("%lld", &segtree[i].sum);
if(segtree[i].sum <= ) segtree[i].flag = ;
return;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
push_up(i);
} void update(int i, int l, int r) {
if(!segtree[i].flag) return;
if(segtree[i].l == segtree[i].r) {
if(segtree[i].flag) {
segtree[i].sum = sqrt(segtree[i].sum);
if(segtree[i].sum <= ) segtree[i].flag = ;
}
return;
}
int mid = (segtree[i].l + segtree[i].r) >> ;
if(r <= mid) {
if(segtree[i*].flag)
update(i*, l, r);
}
else if(l > mid) {
if(segtree[i*+].flag)
update(i*+, l, r);
}
else {
if(segtree[i*].flag) update(lson);
if(segtree[i*+].flag) update(rson);
}
push_up(i);
} ll query(int i, int l, int r) {
if(segtree[i].l == l && segtree[i].r == r) {
return segtree[i].sum;
}
int mid = (segtree[i].l + segtree[i].r) >> ;
if(l > mid) return query(i * + , l, r);
else if(r <= mid) return query(i * , l, r);
else return query(lson) + query(rson);
} int main() {
//FIN;
int icase = ;
while(~scanf("%d", &n)) {
build(, , n);
printf("Case #%d:\n", ++icase);
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &op, &x, &y);
int a = max(x, y), b = min(x, y);
if(op == ) {
update(, b, a);
} else {
printf("%lld\n", query(, b, a));
}
}
printf("\n");
}
return ;
}

Can you answer these queries?(HDU4027+势能线段树)的更多相关文章

  1. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  2. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  3. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  4. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  5. Can you answer these queries III(线段树)

    Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...

  6. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

  7. hdu4027Can you answer these queries?【线段树】

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  8. 2018.10.16 spoj Can you answer these queries V(线段树)

    传送门 线段树经典题. 就是让你求左端点在[l1,r1][l1,r1][l1,r1]之间,右端点在[l2,r2][l2,r2][l2,r2]之间且满足l1≤l2,r1≤r2l1\le l2,r1 \l ...

  9. GSS1 - Can you answer these queries I(线段树)

    前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...

随机推荐

  1. TCP系列06—连接管理—5、TCP fastopen(TFO)

    一.TFO背景 当前web和web-like应用中一般都是在三次握手后开始数据传输,相比于UDP,多了一个RTT的时延,即使当前很多应用使用长连接来处理这种情况,但是仍然由一定比例的短连接,这额外多出 ...

  2. HUAS 1476 不等数列(DP)

    考虑DP. 如果把转移看出当前位填什么数的话,这样是有后效性的. 如果考虑当前的序列是将1至n依次插入序列中的话. 考虑将i插入1到i-1的序列中,如果插入到<号中或者首部,那么最后就会多出一个 ...

  3. 【bzoj2259】[Oibh]新型计算机 堆优化Dijkstra

    题目描述 Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计算机的输入问题.新型计算机的输入也很独特,假设输入序列中有 ...

  4. 【bzoj3524】[Poi2014]Couriers 主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  5. Codeforces Round#516 Div.1 翻车记

    A:开场懵逼.然后发现有人1min过,于是就sort了一下,于是就过了.正经证明的话,考虑回文串两端点一定是相同的,所以最多有Σcnti*(cnti+1)/2个,cnti为第i种字母出现次数.而sor ...

  6. BZOJ3675 [Apio2014]序列分割 【斜率优化dp】

    3675: [Apio2014]序列分割 Time Limit: 40 Sec  Memory Limit: 128 MB Submit: 3366  Solved: 1355 [Submit][St ...

  7. Ark组件[转]

    Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...

  8. Redux的应该注意的问题

    1. Store中的State修改不能直接修改原有的State,若直接修改State,则redux中的所有操作都将指向 内存中的同一个state,将无法获取每一次操作前后的state,就无法追溯sta ...

  9. [zhuan]VMware中bridge方式网络不能上网的解决办法

    http://jingpin.jikexueyuan.com/article/31601.html 安装好VMware 7后,打开原来的虚拟机文件,发现不能上网,原来的Ethernet是设置的Brid ...

  10. ubuntu下如何控制风扇速度?

    1.安装lm-sensors  (https://apps.ubuntu.com/cat/applications/lm-sensors/)和fancontrol(https://apps.ubunt ...