0
点赞
收藏
分享

微信扫一扫

HDU4027:Can you answer these queries?(开方操作,查询和)

fbd4ffd0717b 2023-02-07 阅读 33


Can you answer these queries?
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 

0 1 10 
1 1 10 
1 1 5 
0 5 8 
1 4 8

Sample Output 
Case #1: 
19 

6

Source 
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
--------------------- 

 

题意:两个操作,0.对区间数做平方根  1.区间和

分析:

本题我还以为需要懒惰标记的特殊的应用,但是根本不用,对区间更新可以对区间内每一个点进行单点更新,但这样的话话会超时。但是数据范围2^63,在7次开方之后归1,且1开平方也是1。所以我们可以用一个标记来对树的节点进行标记。如果该节点范围内所有树都为1,就不需要进行区间更新。

但还有很多坑点,代码标注

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

ll b[N];
ll ans_sum,ans_max,ans_min;
struct node
{
int l,r;
ll sum;
int flag;
} a[N<<2];

void pushup(int i)
{
a[i].sum=a[lson].sum+a[rson].sum;
a[i].flag=a[lson].flag&a[rson].flag;
}
//建立线段树
void build(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
a[i].flag=0;
if(l == r)
{
a[i].sum = b[l];
if(b[l]==1)
a[i].flag=1;
return;
}
int mid = (l+r)>>1;
build(LS);
build(RS);
pushup(i);
}
//a[l,r]都变为val
void update(int l,int r,int i)
{
//cout<<l<<" "<<r<<" "<<i<<endl;
if(a[i].l>=l&&a[i].r<=r)
{
if(a[i].flag==1)
return;
}
if(a[i].l==a[i].r)
{
a[i].sum=sqrt(a[i].sum);
if(a[i].sum==1)
a[i].flag=1;
return ;
}
int mid = (a[i].l+a[i].r)>>1;
if(l<=mid) update(l,r,lson);
if(r>mid) update(l,r,rson);
pushup(i);
}
void query(int l,int r,int i)
{
//cout<<l<<" "<<r<<" "<<i<<endl;
if(l <= a[i].l && a[i].r <= r)
{
ans_sum += a[i].sum;
return ;
}
int mid = (a[i].l+a[i].r)>>1;
if(l<=mid) query(l,r,lson);
if(r>mid) query(l,r,rson);
pushup(i);
}

int main()
{
int n,m;
int cas=1;
while(scanf("%d",&n)!=-1)
{
for (int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
}
scanf("%d",&m);
build(1,n,1);
printf("Case #%d:\n",cas++);
while(m--){
int l, r;
int op;
scanf("%d",&op);
scanf("%d%d", &l,&r);
if(l>r) swap(l,r);///坑死了
//cout<<op<<endl;
if(op==0)
{
update(l,r,1);
}
else
{
//cout<<l<<" "<<r<<endl;
ans_sum=0;
query(l,r,1);
printf("%lld\n",ans_sum);
}
}
cout<<endl;
}


return 0;
}

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

ll b[N];
ll ans_sum,ans_max,ans_min;
struct node
{
int l,r;
ll sum;
int flag;
} a[N<<2];

void pushdown(int i)//标记下传
{


}

void pushup(int i)
{
a[i].sum=a[lson].sum+a[rson].sum;
a[i].flag=a[lson].flag&a[rson].flag;
}
//建立线段树
void build(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
if(l == r)
{
a[i].sum = b[l];
a[i].flag=0;
return;
}
int mid = (l+r)>>1;
build(LS);
build(RS);
pushup(i);
}
//a[l,r]都变为val
void set_data(int l,int r,int i,ll val)
{

if(a[i].l>=l&&a[i].r<=r)
{
if(a[i].flag==1)
return;
if(a[i].l==a[i].r)
{
a[i].sum=sqrt(a[i].sum);
if(a[i].sum==1)
{
a[i].flag=1;
}

return ;
}

}
//cout<<a[i].l<<" "<<a[i].r<<endl;

pushdown(i); //标记下传
int mid = (a[i].l+a[i].r)>>1;
if(l<=mid) set_data(l,r,lson,val);
if(r>mid) set_data(l,r,rson,val);
pushup(i);
}


void query(int l,int r,int i)
{
//cout<<l<<" "<<r<<" "<<i<<endl;
if(l <= a[i].l && a[i].r <= r)
{
ans_sum += a[i].sum;

return ;
}
pushdown(i);
int mid = (a[i].l+a[i].r)/2;
if(l<=mid) query(l,r,lson);
if(r>mid) query(l,r,rson);
pushup(i);
}

int main()
{
int n,m;
int cas=1;
while(scanf("%d",&n)!=-1)
{
for (int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
}

build(1,n,1);
scanf("%d",&m);
printf("Case #%d:\n",cas++);
while(m--){
int l, r,k;
scanf("%d",&k);

if(k==0)
{
scanf("%d %d",&l,&r);
if(l>r) swap(l,r);
set_data(l,r,1,0);
}
else
{
scanf("%d %d", &l,&r);
if(l>r) swap(l,r);
ans_sum=0;
query(l,r,1);
printf("%lld\n",ans_sum);
}
}
cout<<endl;
}


return 0;
}

 

举报

相关推荐

0 条评论