1. 程式人生 > >Just a Hook HDU

Just a Hook HDU

Think:
1知識點:線段樹區間更新+lazy標記
2題意:在n個初始化點權為1的點的基礎上進行區間更新,詢問最終狀態n個點的點權之和
3反思:當前題目lazy標記賦值更新

以下為Wrong Answer程式碼——lazy標記更新錯誤

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 101400;

struct Node{
    int sum;
}node[N<<2];

int lazy[N<<2
]; void Build(int l, int r, int rt); void Updata(int rt); void down(int rt, int l, int r); void up_v(int L, int R, int v, int l, int r, int rt); int main(){ int T, k = 1, n, m, L, R, v; scanf("%d", &T); while(T--){ scanf("%d", &n); Build(1, n, 1); scanf("%d"
, &m); while(m--){ scanf("%d %d %d", &L, &R, &v); up_v(L, R, v, 1, n, 1); } printf("Case %d: The total value of the hook is %d.\n", k++, node[1].sum); } return 0; } void Build(int l, int r, int rt){ lazy[rt] = 0; if(l == r){ node[rt].sum = 1
; return; } int mid = (l+r)/2; Build(l, mid, rt<<1); Build(mid+1, r, rt<<1|1); Updata(rt); } void Updata(int rt){ node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum; } void up_v(int L, int R, int v, int l, int r, int rt){ if(L <= l && r <= R){ node[rt].sum = v*(r-l+1); lazy[rt] = v; return; } down(rt, l, r); int mid = (l+r)/2; if(L <= mid) up_v(L, R, v, l, mid, rt<<1); if(R > mid) up_v(L, R, v, mid+1, r, rt<<1|1); Updata(rt); } void down(int rt, int l, int r){ if(lazy[rt]){ lazy[rt<<1] += lazy[rt]; lazy[rt<<1|1] += lazy[rt]; int mid = (l+r)/2; node[rt<<1].sum = lazy[rt]*(mid-l+1); node[rt<<1|1].sum = lazy[rt]*(r-mid); lazy[rt] = 0; } }

以下為Accepted程式碼

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 101400;

struct Node{
    int sum;
}node[N<<2];

int lazy[N<<2];

void Build(int l, int r, int rt);
void Updata(int rt);
void down(int rt, int l, int r);
void up_v(int L, int R, int v, int l, int r, int rt);

int main(){
    int T, k = 1, n, m, L, R, v;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        Build(1, n, 1);
        scanf("%d", &m);
        while(m--){
            scanf("%d %d %d", &L, &R, &v);
            up_v(L, R, v, 1, n, 1);
        }
        printf("Case %d: The total value of the hook is %d.\n", k++, node[1].sum);
    }
    return 0;
}
void Build(int l, int r, int rt){
    lazy[rt] = 0;
    if(l == r){
        node[rt].sum = 1;
        return;
    }
    int mid = (l+r)/2;
    Build(l, mid, rt<<1);
    Build(mid+1, r, rt<<1|1);
    Updata(rt);
}
void Updata(int rt){
    node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
}
void up_v(int L, int R, int v, int l, int r, int rt){
    if(L <= l && r <= R){
        node[rt].sum = v*(r-l+1);
        lazy[rt] = v;
        return;
    }
    down(rt, l, r);
    int mid = (l+r)/2;
    if(L <= mid)
        up_v(L, R, v, l, mid, rt<<1);
    if(R > mid)
        up_v(L, R, v, mid+1, r, rt<<1|1);
    Updata(rt);
}
void down(int rt, int l, int r){
    if(lazy[rt]){
        lazy[rt<<1] = lazy[rt];/*lazy標記賦值更新*/
        lazy[rt<<1|1] = lazy[rt];/*lazy標記賦值更新*/
        int mid = (l+r)/2;
        node[rt<<1].sum = lazy[rt]*(mid-l+1);
        node[rt<<1|1].sum = lazy[rt]*(r-mid);
        lazy[rt] = 0;
    }
}

相關推薦

Just a Hook HDU

Think: 1知識點:線段樹區間更新+lazy標記 2題意:在n個初始化點權為1的點的基礎上進行區間更新,詢問最終狀態n個點的點權之和 3反思:當前題目lazy標記賦值更新 以下為Wrong Answer程式碼——lazy標記更新錯誤 #in

HDU 1698 Just a Hook(線段樹成段更新)

there clas class rec std sts present script metal 題目網址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 題目: Problem Description In the ga

HDU 1698 Just a Hook 線段樹 區間更新

不同 描述 感覺 ring 這也 deque deb %d total   題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1698   題目描述: 區間更新, 最後求出1 ~ n 之和     解題思路: 這裏涉及到區間

HDU-1698 Just a Hook (線段樹、段變換(覆蓋))

++ first rom c11 scan limit field sticks example Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/

hdu 1698 Just a Hook 線段樹成段更新

void log ++ ase scan efi nbsp code upd 線段樹功能:update:成段替換 成段更新去要用到延遲標記,具體調試代碼就容易懂些 #include <iostream> #include <string>

HDU 1698 Just a Hook (線段樹區間更新入門題)

follow and iostream http span meta ali pos num Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O

題解報告:hdu 1698 Just a Hook(線段樹lazy標記的運用)

http som height sum cup ive first 報告 original Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible th

hdu 1698 Just a Hook(區間修改)

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive me

hdu 1698 Just a Hook (成段更新線段樹)

題目連結:哆啦A夢傳送門 題意:首先給出n個掛鉤,每個掛鉤的值都為1,接著Q次詢問,每次 X,Y,Z,代表在區間 [X,Y]的掛鉤都改為Z,   模板參考連結:https://www.cnblogs.com/TenosDoIt/p/3453089.html 題解:很顯然

HDU 1698 Just a Hook(線段樹-區間更新)

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sti

HDU 1698 Just a Hook——區間更新的線段樹

題意:一開始有n個1組成的數列,執行q此操作,每次操作給出x y z,表示把區間【x,y】內的所有數變成z,執行完q此操作後求整個數列的和 思路:直接套用區間更新線段樹的模板,進行簡單的更新值與求和操作 #include <cstdio> #include &l

HDU 1698 Just a Hook 題解&程式碼

題目要求是對於一個初始值均為1的區間進行區間修改,修改目標為:[a,b]區間內的值被修改為c 對於多組資料的每組資料輸出q次修改後的全區間和 基本線段樹了…只有兩個元素需要維護:sum和add,su

HDU 1698 Just a Hook 線段樹區間修改

題意: 給定N個數,初始化全部為1. 一種操作,區間修改。把區間[L,R]內所有數字修改為另一個值X Q次操作之後,求區間[1,N]的和 分析: 簡單的線段樹區間修改操作應用題 具體實現見程式碼 #

HDU-1698 Just a Hook 線段樹區間更新

題意:一個長度為n的線段,q個操作,每個操作x,y,z是指把區間[x,y]內的值修改為z,最後求線段值的和。 思路:增加懶惰標誌,每次更新操作只更新到這個區間節點而不是這個區間的所有葉子結點。例如操作1,5,2。只更新到節點[1,5]而不是他的五個葉子結點

HDU 1698 Just a Hook(線段樹的區間更新)

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several c

HDU 1166 —— Just a Hook 【線段樹 區間修改】

http://acm.hdu.edu.cn/showproblem.php?pid=1698 要求:   1. 區間修改   2. 最後整個區間求和 分析:   因為沒有多次查詢,而只有最後的一次整個區

HDU 1698 Just a Hook (線段樹的區間更新)

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consec

HDU 1698 Just a Hook(線段樹區間更新)

題意: 屠夫是Dota中一個令所有英雄聞風喪膽的英雄。他有一個很長的鉤子,這個鉤子是用銅做的(剛剛開始都是1),現在他想要更改這些鉤子,把某個區間的鉤子改為金、銀或銅。 輸入 L, R,

hdu 1698 Just a Hook(線段樹基礎)

成段更新的線段樹,加入了延時標記............ 線段樹這種東西細節上的理解因人而異,還是要自己深入理解......慢慢來 #include <iostream> #include <algorithm> #include <cmat

HDU-1698 Just a Hook 線段樹成段更新

題目連結 #include "stdio.h" const int maxn = 100050; int n,x,y,z; struct Node { int val; }tree[maxn*4]; void pushup( int t ) { tree