HDU - 4027 Can you answer these queries? —— 区间更新 区间查询
2017-09-18 12:24
375 查看
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 19065 Accepted Submission(s): 4500
Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease
the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for
help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query
of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6
题意:给定区间 操作是把某个区间的数都开根号 问某个区间的和
思路:用lazy标签记录这个区间是不是都是1 是1就不用管 否则会超时
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#define max_ 100010
#define inf 0x3f3f3f3f
#define ll __int64
using namespace std;
struct node
{
int l,r,lazy;
ll sum;
};
struct node tree[max_*4];
int n,m,casenum=1;
void build(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
if(l==r)
{
scanf("%I64d",&tree[i].sum);
if(tree[i].sum==1)
tree[i].lazy=1;
else
tree[i].lazy=0;
return;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
if(tree[i<<1].lazy&&tree[i<<1|1].lazy)
tree[i].lazy=1;
else
tree[i].lazy=0;
}
void update(int i,int l,int r)
{
if(tree[i].lazy)
return;
if(tree[i].l==tree[i].r)
{
tree[i].sum=sqrt(tree[i].sum);
if(tree[i].sum==1)
tree[i].lazy=1;
return;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(l>mid)
update(i<<1|1,l,r);
else if(r<=mid)
update(i<<1,l,r);
else
{
update(i<<1,l,mid);
update(i<<1|1,mid+1,r);
}
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
if(tree[i<<1].lazy&&tree[i<<1|1].lazy)
tree[i].lazy=1;
}
ll query(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
return tree[i].sum;
int mid=(tree[i].l+tree[i].r)>>1;
if(l>mid)
return query(i<<1|1,l,r);
else if(r<=mid)
return query(i<<1,l,r);
else
return query(i<<1,l,mid)+query(i<<1|1,mid+1,r);
}
int main(int argc, char const *argv[])
{
while(scanf("%d",&n)!=EOF)
{
memset(tree,0,sizeof(tree));
build(1,1,n);
scanf("%d",&m);
printf("Case #%d:\n",casenum++);
while(m--)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
if(x>y)
swap(x,y);
if(t==0)
update(1,x,y);
else if(t==1)
printf("%I64d\n",query(1,x,y));
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU - 4027 Can you answer these queries? (线段树 区间更新区间查询)
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
- HDU - 4027 Can you answer these queries?(线段树区间更新,区间查询)
- Can you answer these queries? (HDU - 4027)(线段树区间更新-区间查询)
- HDU - 4027 Can you answer these queries? (线段树 区间更新 区间查询)
- HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)
- 【线段树区间单点更新加约束条件】HDU - 4027 Can you answer these queries?
- HDU 4027 Can you answer these queries?(线段树、查询区间和及修改区间数为数的平方根)
- hdu 4027 Can you answer these queries? 线段树区间修改区间查询
- HDU-4027 Can you answer these queries? (线段树 区间修改 区间查询)(The 36th ACM/ICPC Asia Regional Shanghai)
- HDU 4027 Can you answer these queries?【线段树+区间更新】
- HDU 4027 Can you answer these queries? 线段树 区间修改 区间查询
- HDU 4027 Can you answer these queries?(线段树区间更新)
- HDU 4027 Can you answer these queries? (线段树 区间更新)
- HDU 4027 Can you answer these queries? (线段树变着花样让你区间更新)
- HDU 4027 Can you answer these queries? 题解(思维+线段树区间更新)
- HDU 4027 Can you answer these queries?——其实是点更新的区间更新线段树
- HDU 4027 Can you answer these queries?(线段树 区间不等更新)
- HDU 4027 Can you answer these queries?(线段树/区间不等更新)
- Can you answer these queries? HDU - 4027 线段树 区间更新