遠古技術htaccess筆記

來源:互聯網
上載者:User

關於M的計算方法

int M = 1;for (; M < n;)M <<= 1;if (M > 1) M--;

今天讀學生代碼,小朋友的代碼都很規範

int M = 1;for (; M < n;) {    M <<= 1;}if (M > 1) {    M--;}

可是,好長啊,多年前看見主任的幾個log感覺自己寫不出來(當時自己確實寫不出來)

現在試試吧M = (1 << (int)(ceil)(log(i)/log(2))) - 1,諾

void emmm(){    for (int i = 1; i <= 1000000;i++){        int M = 1;        for (; M < i;) {            M <<= 1;        }        if (M > 1) {            M--;        }        int k = (1 << (int)(ceil)(log(i)/log(2))) - 1;        if (k != M){            printf("%d, %d, %d\n", i, M, k);        }    }}

發現n=1的時候不一樣,當時加那個M--的if的時候似乎是在某次cf上,現在腦子一想,好像n=1的時候M就該是0

打擾了,告辭

一句話

M = (1 << (int)(ceil)(log(i)/log(2))) - 1;

現在板子長這樣

struct ZKWSegTree{    #define lc (t<<1)    #define rc (t<<1^1)    int sum[N], M;    inline void build(int n){        M = (1 << (int)(ceil)(log(i)/log(2))) - 1;        memset(sum, sizeof(sum), 0);        for (int i = 1+M; i <= n+M; i++){            scanf("%d", &sum[i]);        }        for (int t = M; t >= 1; t--){            sum[t] = sum[lc] + sum[rc];        }    }    void add(int t, int x){        for (sum[t+=M]+=x, t>>=1; t; t>>=1){            sum[t] = sum[lc] + sum[rc];        }    }    int query(int l, int r){        int ans = 0;        for (l+=M-1,r+=M+1; l^r^1; l>>=1,r>>=1){            if (~l&1) ans += sum[l^1];            if ( r&1) ans += sum[r^1];        }        return ans;    }} T;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.