HDU-4027 Can you answer these queries?
Can you answer these queries?
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.
InputThe 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 2 63.
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.
OutputFor 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
题意:n个数据,m组查询,每组查询由a l r 组成 a==0是求[l~r]内的值开根号 a==1求和[l~r]
思路:当一个值为1是用sqrt 还是1,这里我们就不用再往下面算了,我们搞个f数组记录是否出现过1了。其他就是模板了,这里不使用lazy也能过,但是不标记1,盲目开根号会T(因为我T了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define ll long long using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn = 100000+5; ll sum[maxn<<2],f[maxn<<2]; void pushup(ll rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; f[rt]=f[rt<<1]&f[rt<<1|1]; } void build(ll l,ll r,ll rt) { if(l==r) { scanf("%lld",&sum[rt]); return; } ll m=(l+r)>>1; build(lson); build(rson); pushup(rt); } void updata(ll L,ll R,ll l,ll r,ll rt) { if(l==r) { sum[rt]=sqrt(sum[rt]); if(sum[rt]<=1) f[rt]=1; return; } ll m=(l+r)>>1; if(L<=m&&!f[rt<<1]) updata(L,R,lson); if(R>m&&!f[rt<<1|1]) updata(L,R,rson); pushup(rt); } ll query(ll L,ll R,ll l,ll r,ll rt) { if(L<=l&&r<=R) { return sum[rt]; } ll ans=0; ll m=(l+r)>>1; if(L<=m) ans+=query(L,R,lson); if(R>m) ans+=query(L,R,rson); return ans; } int main() { ll n,m,p=1; while(~scanf("%lld",&n)) { memset(sum,0,sizeof(sum)); memset(f,0,sizeof(f)); build(1,n,1); ll a,b,c; int o[10]; scanf("%lld",&m); printf("Case #%lld:\n",p++); while(m--) { scanf("%lld %lld %lld",&a,&b,&c); ll bb=max(b,c); ll cc=min(b,c); if(a) printf("%lld\n",query(cc,bb,1,n,1)); else updata(cc,bb,1,n,1); } printf("\n"); } }
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~