2015
04-15

# Can you answer these queries?

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.

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.

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.

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

Case #1:
19
7
6

#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define LL __int64
using namespace std;
const int N=100010;
struct Node
{
int L,R;
LL len;
LL sum;
} t[N*4];
LL data[N];
int n,q;
void up(int fa){
t[fa].sum=t[fa<<1].sum+t[fa<<1|1].sum;
}
void down(int l,int r,int fa)
{
if(t[fa].L==t[fa].R)
{
t[fa].sum=(LL)(sqrt(t[fa].sum));
return;
}
int ls=fa<<1;
int rs=fa<<1|1;
int mid=(t[fa].L+t[fa].R)/2;
down(l,mid,ls);
down(mid+1,r,rs);
up(fa);
}

void built(int l,int r,int fa)
{
t[fa].L=l;
t[fa].R=r;
t[fa].len=r-l+1;
t[fa].sum=0LL;
if(l==r)
{
t[fa].sum=data[l];
return;
}
int mid=(l+r)/2;
built(l,mid,fa<<1);
built(mid+1,r,fa<<1|1);
up(fa);
}

void update(int l,int r,int fa)
{
int ls=fa<<1;
int rs=fa<<1|1;
int mid=(t[fa].L+t[fa].R)/2;

if(t[fa].L==l&&t[fa].R==r)
{
if(t[fa].sum==t[fa].len)
return ;
down(l,r,fa);
return ;
}

if(r<=mid)
update(l,r,ls);
else if(l>mid)
update(l,r,rs);
else
{
update(l,mid,ls);
update(mid+1,r,rs);
}
up(fa);
}

LL query(int l,int r,int fa)
{
int ls=fa<<1;
int rs=fa<<1|1;
int mid=(t[fa].L+t[fa].R)/2;

if(t[fa].L>=l&&t[fa].R<=r)
return t[fa].sum;
if(r<=mid)
return query(l,r,ls);
else if(l>mid)
return query(l,r,rs);
else
return query(l,mid,ls)+query(mid+1,r,rs);
}

int main()
{
int t=1;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
scanf("%I64d",&data[i]);
built(1,n,1);
int a,b,c;
printf("Case #%d:\n",t++);
cin>>q;
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
if(b>c)swap(b,c);
if(a)
cout<<query(b,c,1)<<endl;
else
update(b,c,1);
}
cout<<endl;
}
return 0;
}


1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。