Can you answer these queries? 区间更新和查询

来源:互联网 发布:珂润水乳怎么样知乎 编辑:程序博客网 时间:2024/05/29 17:42
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


额,,,想了一会儿没思路啊QAQ,这里是单个数开方取整不是区间加减,用lazy不行,挨个更新也肯定超时... 

看了网上的博客都说,再大的数经过差不过六七次的开方就成1了... 所以最终还是挨个更新,不过当区间内的数都是1的时候就没有再进行更新了,这里需要判断一下,因为1开根号还是1,这样就不会超时了,还有就是输入的左右端点,左端点有可能大于右端点...

#include <iostream>    #include <algorithm>    #include <cstdio>    #include <cmath>    using namespace std;    const int N = 1e5;    struct tree{    int l, r;    long long sum;}STree[N << 2];long long ans;void Build(int i, int l, int r){    STree[i].l = l;    STree[i].r = r;    if(l == r)    {        scanf("%lld", &STree[i].sum);        return;    }    int mid = (l + r) >> 1;    Build(i << 1, l, mid);    Build(i << 1 | 1, mid + 1, r);    STree[i].sum = STree[i << 1].sum + STree[i << 1 | 1].sum;}void Push_Down(int i){    if(STree[i].l == STree[i].r)  //区间内的数挨个更新    {        STree[i].sum = (int)sqrt(1.0 * STree[i].sum);        return;    }    Push_Down(i << 1);    Push_Down(i << 1 | 1);    STree[i].sum = STree[i << 1].sum + STree[i << 1 | 1].sum;}void Update(int i, int l, int r){    if(STree[i].l >= l && STree[i].r <= r)    {        if(STree[i].r - STree[i].l + 1 == STree[i].sum) return; //区间内都为1,无需更新        Push_Down(i);        return;    }    int mid = (STree[i].l + STree[i].r) >> 1;    if(l <= mid)        Update(i << 1, l, r);    if(r > mid)        Update(i << 1 | 1, l, r);    STree[i].sum = STree[i << 1].sum + STree[i << 1 | 1].sum;}void Query(int i, int l, int r){    if(STree[i].l >= l && STree[i].r <= r)    {        ans += STree[i].sum;        return;    }    int mid = (STree[i].l + STree[i].r) >> 1;    if(l <= mid)        Query(i << 1, l, r);    if(r > mid)        Query(i << 1 | 1, l, r);}int main()    {               int n, m, t, l, r, k = 1;    while(~scanf("%d", &n))    {        printf("Case #%d:\n", k++);        Build(1, 1, n);        scanf("%d", &m);        while(m--)        {            scanf("%d %d %d", &t, &l, &r);            if(l > r)  //注意...                swap(l, r);            if(t == 0)                Update(1, l, r);            else            {                ans = 0;                Query(1, l, r);                printf("%lld\n", ans);            }        }        printf("\n");    }    return 0;    }  



阅读全文
1 0
原创粉丝点击