hdu 4027 Can you answer these queries?

来源:互联网 发布:中文域名注册管理办法 编辑:程序博客网 时间:2024/05/17 05:53

http://acm.hdu.edu.cn/showproblem.php?pid=4027

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 10129    Accepted Submission(s): 2328


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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 

Sample Output
Case #1:1976
 
有一列数,(都是2^63范围内的并且都大于0的整数),现在呢有一些操作, 操作 0 可以把区间LR内的所有数都变成它的平方根数(是取整后的),操作 1 可以就是求区间LR内的和了。
分析:因为这个操作是把一个数变成平方根,所以显得略棘手,不过如果仔细演算的话会发现一个2^64数的平方根开8次也就变成了 1,所以也更新不了多少次,所以可以每次更新到底。、
注意:给的X Y大小未知,会出现X > Y的情况
<span style="color:#006600;">#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 1000005#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8#define Lson rt<<1, l, tree[rt].mid ()#define Rson rt<<1|1, tree[rt].mid () + 1, rstruct node{    int l, r;    long long sum;    int len () {return (r - l + 1);}    int mid () {return (l + r) / 2;}}tree[N*4];long long val[N];void build (int rt, int l, int r);void Insert (int rt, int l, int r);long long query (int rt, int l, int r);int main (){    int n, m, flag = 1;    while (~scanf ("%d", &n))    {        for (int i=1; i<=n; i++)            scanf ("%I64d", &val[i]);        build (1, 1, n);        scanf ("%d", &m);        printf ("Case #%d:\n", flag++);        int a, b, c;        while (m--)        {            scanf ("%d%d%d", &a, &b, &c);            if (b > c) swap (b, c);            if (a == 0) Insert (1, b, c);            else printf ("%I64d\n", query (1, b, c));        }        puts ("");    }    return 0;}void build (int rt, int l, int r){    tree[rt].l = l, tree[rt].r = r;    if (l == r)    {        tree[rt].sum = val[l];        return;    }    build (Lson), build (Rson);    tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;}void Insert (int rt, int l, int r){    </span><span style="color:#ff0000;">if (tree[rt].len () == tree[rt].sum) return;</span><span style="color:#006600;">    if (tree[rt].l == tree[rt].r)    {        tree[rt].sum = (long long) sqrt (tree[rt].sum * 1.0);        return;    }    if (r <= tree[rt].mid ()) Insert (rt<<1, l, r);    else if (l > tree[rt].mid ()) Insert (rt<<1|1, l, r);    else Insert (Lson), Insert (Rson);    tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;}long long query (int rt, int l, int r){    if (tree[rt].l == l && tree[rt].r == r)        return tree[rt].sum;    if (r <= tree[rt].mid ()) return query (rt<<1, l, r);    else if (l > tree[rt].mid ()) return query (rt<<1|1, l, r);    else return (query (Lson) + query (Rson));}</span>



0 0
原创粉丝点击