hdu 4027 Can you answer these queries? 線段樹 懶惰標記 單點更新妙用

來源:互聯網
上載者:User

題意:把一組艦隊看成是線段上的端點,有一種秘密武器,每次可以攻擊一個區間上的船,然後他們的防禦力x減低為sqrt(x).

做法:最多隻能有7次攻擊有效。所以用帶點更新吧,一遇到區間防禦力總和為len(區間中含有艦船的個數),就停止更新,因為這個區間所有的船的防禦力已經只剩1了,這麼一來,最後的時間複雜度不會太高

#include <cstdio>#include <cstring>#include <cmath>#define max(a, b) ((a) > (b) ? (a) : (b))#define left l, m, x << 1#define right m + 1, r, x << 1 | 1//有效攻最多七下。。。typedef __int64 LL;const int LMT = 100003;LL sum[LMT << 2];struct __node{    int l, r, len;}node[LMT << 2];inline LL get(void){    LL res = 0;    char ch = getchar();    while(ch < '0' || ch > '9') ch = getchar();    while(ch >= '0' && ch <= '9')    {        res = res * 10 + ch - '0';        ch = getchar();    }    return res;}void build(int l, int r, int x){    node[x].l = l;    node[x].r = r;    node[x].len = r - l + 1;    if(l == r)    {        sum[x] = get();        return ;    }    int m = (l + r) >> 1;    build(left);    build(right);    sum[x] = sum[x << 1] + sum[x << 1 | 1];}LL query(int L, int R, int x){    if (L <= node[x].l && node[x].r <= R) return sum[x];    int m = (node[x].l + node[x].r) >> 1;    LL res = 0;    if (L <= m) res += query(L, R, x << 1);    if (R > m) res += query(L, R, x << 1 | 1);    return res;}void update(int L, int R, int x){    if(sum[x] == node[x].len) return;    if(node[x].l == node[x].r)    {        sum[x] = (LL)sqrt(1.0 * sum[x]);        return;    }    int m = (node[x].l + node[x].r) >> 1;    if (L <= m) update(L, R, x << 1);    if (R > m) update(L, R, x << 1 | 1);        sum[x] = sum[x << 1] + sum[x << 1 | 1];}int main(void){    int n, q, ord, l, r, I = 1;    while(~scanf("%d", &n))    {        build(1, n, 1);        scanf("%d", &q);        printf("Case #%d:\n", I++);        while(q--)        {            scanf("%d%d%d", &ord, &l, &r);            if(l > r)            {             l = l ^ r;             r = l ^ r;             l = l ^ r;            }            if(ord) printf("%I64d\n", query(l, r, 1));            else update(l, r, 1);        }        printf("\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.