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.
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
题意: 有一个长度不超过 100000 个数组, 数组的数大小不超过 2^63, 且一直是整型, 执行两个操作, 0 操作表示将指定区间内的数都开平方, 1 操作
表示查询指定区间的总数和. 注意操作的区间 x,y,x 可能大于 y, 输出格式按样例, 输入以 EOF 结束.
思路: 首先一个数的被开方次数不会超过 7 次, 因为超过 7 次后这个数会变成 1, 因此单曲奖和变成 r-l+1 时可以直接 return , 方便节省时间,
比较注意的是区间开方问题, 由于是开方, 依此标记就没用了, 改用定区间, 单点修改值的方法, 具体实现见代码, 最后再加一个区间求和就行.
注意数要开 longlong 型.
代码:
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #include <sstream>
- #include <iostream>
- #define mod 998244353
- #define eps 1e-6
- #define ll long long
- #define INF 0x3f3f3f3f
- using namespace std;
- struct node
- {
- //l 表示左边, r 表示右边
- int l,r;
- ll sum;
- };
- node no[500000];
- ll num[100000];
- int q,a,b,c;
- // 初始化
- //k 表示当前节点的编号, l 表示当前区间的左边界, r 表示当前区间的右边界
- void build(int k,int l,int r)
- {
- no[k].l=l;
- no[k].r=r;
- // 如果递归到最低点
- if(l==r)
- {
- no[k].sum=num[l];
- return ;
- }
- // 对半分
- int mid=(l+r)/2;
- // 递归到左线段
- build(k*2,l,mid);
- // 递归到右线段
- build(k*2+1,mid+1,r);
- // 更新当前节点的值
- no[k].sum=no[k*2].sum+no[k*2+1].sum;
- }
- // 区间单点修改
- // 从区间 1 到 n 中搜索在定区间 b,c 中的元素,
- void change(int k,int l,int r)
- {
- if(no[k].sum==(no[k].r-no[k].l+1))
- {
- return ;
- }
- if(no[k].l==no[k].r)
- {
- no[k].sum=(ll)sqrt(no[k].sum);
- return ;
- }
- int mid = (no[k].l+no[k].r)/2;
- if(b<=mid)
- {
- change(k*2,l,mid);
- }
- if(c>mid)
- {
- change(k*2+1,mid+1,r);
- }
- // 更新当前节点的值
- no[k].sum=no[k*2].sum+no[k*2+1].sum;
- }
- // 求区间
- ll query(int k,int l,int r)
- {
- // 到对应层时返回值
- if(no[k].l==l&&no[k].r==r)
- {
- return no[k].sum;
- }
- // 取中值
- int mid=(no[k].l+no[k].r)/2;
- if(r<=mid)
- {
- return query(k*2,l,r);
- }
- else if(l>mid)
- {
- return query(k*2+1,l,r);
- }
- else
- {
- return query(k*2,l,mid)+query(k*2+1,mid+1,r);
- }
- }
- int main()
- {
- int n;
- int ans=1;
- while(scanf("%d",&n)!=EOF)
- {
- memset(num,0,sizeof(num));
- memset(no,0,sizeof(no));
- printf("Case #%d:\n",ans++);
- for(int i=1;i<=n;i++)
- {
- scanf("%lld",&num[i]);
- }
- build(1,1,n);
- scanf("%d",&q);
- while(q--)
- {
- scanf("%d %d %d",&a,&b,&c);
- if(b>c)
- {
- swap(b,c);
- }
- if(a==0)
- {
- change(1,1,n);
- }
- else if(a==1)
- {
- printf("%lld\n",query(1,b,c));
- }
- }
- printf("\n");
- }
- }
来源: http://www.bubuko.com/infodetail-3275621.html