<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 10<span style="box-sizing: border-box; position: relative; font-size: 12px; line-height: 0; vertical-align: baseline; top: -0.5em;">18</span>. On this sequence you have to apply M (M <= 100,000) operations:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.</p><h3 style="box-sizing: border-box; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 20px; margin-bottom: 10px; font-size: 20px;">Input</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">Multiple test cases, please proceed them one by one. Input terminates by EOF.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">For each test case:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">The first line contains an integer N. The following line contains N integers, representing the sequence A<span style="box-sizing: border-box; position: relative; font-size: 12px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">1</span>..A<span style="box-sizing: border-box; position: relative; font-size: 12px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">N</span>. <br style="box-sizing: border-box;" />The third line contains an integer M. The next M lines contain the operations in the form "i x y".i=0 denotes the modify operation, i=1 denotes the query operation.</p><h3 style="box-sizing: border-box; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 20px; margin-bottom: 10px; font-size: 20px;">Output</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">For each test case:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">Print an blank line after each test case.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(85, 85, 85); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">See the sample output for more details.</p><span style="font-family: Arial; font-size: 18px; line-height: 26px;">重點在於如果一個區間全是1了,那就沒必要執行開方操作了;</span>
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
struct node{
long long l,r,sum;
}tree[4*MAXN];
long long a[MAXN];
void build(long long id,long long l,long long r)
{
tree[id].l=l;
tree[id].r=r;
if(l==r)
{
tree[id].sum=a[l];
return;
}
else
{
long long mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
}
}
void update(long long id,long long l,long long r)
{
if(tree[id].r-tree[id].l+1==tree[id].sum)
return;
if(tree[id].l==tree[id].r)
{
tree[id].sum=(long long)sqrt(tree[id].sum);
return;
}
long long mid=(tree[id].l+tree[id].r)/2;
if(r<=mid)
update(id*2,l,r);
else if(l>mid)
update(id*2+1,l,r);
else
{
update(id*2,l,mid);
update(id*2+1,mid+1,r);
}
tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
}
long long query(long long id,long long l,long long r)
{
if(l==tree[id].l&&r==tree[id].r)
return tree[id].sum;
long long mid=(tree[id].l+tree[id].r)/2;
if(r<=mid)
return query(id*2,l,r);
else if(l>mid)
return query(id*2+1,l,r);
else
return query(id*2,l,mid)+query(id*2+1,mid+1,r);
}
int main()
{
long long n,m,op,s,e,cas=1;
while(scanf("%lld",&n)!=EOF)
{
printf("Case #%d:\n",cas++);
for(long long i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
build(1,1,n);
scanf("%lld",&m);
for(long long i=0;i<m;i++)
{
scanf("%lld%lld%lld",&op,&s,&e);
if(s>e)
swap(s,e);
if(op==0)
update(1,s,e);
else
printf("%lld\n",query(1,s,e));
}
}
return 0;
}