• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            oyjpArt ACM/ICPC算法程序設(shè)計(jì)空間

            // I am new in programming, welcome to my blog
            I am oyjpart(alpc12, 四城)
            posts - 224, comments - 694, trackbacks - 0, articles - 6

            HOJ 11107

            Posted on 2008-01-09 14:11 oyjpart 閱讀(7761) 評(píng)論(8)  編輯 收藏 引用 所屬分類(lèi): ACM/ICPC或其他比賽
            Number of stones
            Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:32768KB
            Total submit users: 13, Accepted users: 1
            Problem 11107 : No special judgement
            Problem description
            There are N baskets rounded in a circle, and numbered as 1、2、3、…….、N-1、N, in clockwise. At the beginning, all of the baskets are empty. Some workers go to the moutain to collect stones. When they are back,they put their stones to some baskets. The workers have a habit, Once a worker come back, he choose a baskets, and choose a direction(clockwise or anticlockwise), he put one stone to this basket and move to the next basket according to the direction he has chosen, he continues doing this until all of the stones they have collected have been put down.
            Sometimes the workers ask you how many stone it is in the basket, as there are too many baskets, You are to wirte a program to calculate this.


            Input
            The input test file will contain multiple cases. Each test case:
            In the first line of the input contain two integers N,M(3 <= N <= 100000, 0 <= M <= 300000). Following M lines,each line represents an event. There are only three kinds of events: Q, C, U. And the format is:
            “Q x”, query the number of stones in the basket x.
            “C x num”, a worker comes back and the number of the stones he has picked up is num, he puts down stones from the basket x in clockwise.
            “U x num”, a worker comes back and the number of the stone he has picked up is num, he puts down stones from the basket x in anticlockwise.
            (x, num are both integers, 1 <= x <= N, 1 <= num <= 10000)


            Output
            For each query “Q x”, print the current number of stones in basket x.

            Sample Input
            5 8
                        C 5 8
                        Q 5
                        Q 4
                        U 5 3
                        C 2 6
                        Q 2
                        Q 1
                        U 2 8
                        
            Sample Output
            2
                        1
                        4
                        3
                        
            Problem Source
            birdman


            上次比賽沒(méi)有做..補(bǔ)做一個(gè)..挺好的題..重寫(xiě)了點(diǎn)樹(shù)模板
             1/*
             2 * 主程序要作的事情
             3 * 1.確定N :必須是2^n,可以取實(shí)際n的上界
             4 * 2.build(left, right);
             5 *
             6 */

             7
             8#include <cstdio>
             9#include <cstring>
            10
            11const int N = 131072;                //必須是2^n,可以取實(shí)際n的上界
            12
            13int upperbound;
            14
            15struct Node {
            16    int i, j, c, m;                    //left, right
            17}
             T[N*2];
            18
            19void bd(int d, int left, int right) {
            20    T[d].i = left, T[d].j = right, T[d].c = 0;
            21    if(right > left) {
            22        bd(d*2+1, left, T[d].m = (left+right)>>1);
            23        bd(d*2+2, T[d].m+1, right);
            24    }

            25}

            26
            27void build(int left, int right) {
            28    upperbound = 1;
            29    while(upperbound < right-left+1) upperbound <<= 1;
            30    bd(0, left, left + upperbound-1);
            31}

            32
            33void add(int d, int left, int right, int c) {
            34    if(left <= T[d].i && right >= T[d].j) {
            35        T[d].c += c;
            36    }

            37    else {
            38        if(left <= T[d].m) add(d*2+1, left, right, c);
            39        if(right > T[d].m) add(d*2+2, left, right, c);
            40    }

            41}

            42
            43int get(int x) // 獲得點(diǎn)的覆蓋次數(shù)
            44    int idx = upperbound+x-1, sum = 0;
            45    do {
            46        sum += T[idx].c;
            47        idx = (idx-1)>>1;
            48    }
             while(idx != 0);
            49    return sum;
            50}

            51
            52int n, m;
            53
            54void Add(int x, int num) {
            55    int laps = (num-(n-x))/n;
            56    if(laps > 0{
            57        add(00, n-1, laps);
            58    }

            59    num -= laps*n;
            60    if(n->= num) {
            61        add(0, x, x+num-11);
            62    }

            63    else {
            64        add(0, x, n-11);
            65        add(00, (x+num-1)%n, 1);
            66    }

            67}

            68
            69int main() {
            70    while(scanf("%d %d"&n, &m) != EOF) {
            71        build(0, n-1);
            72        while(m--{
            73            char cmd;
            74            int x, num;
            75            scanf(" %c"&cmd);
            76            if(cmd == 'Q'{
            77                scanf("%d"&x); 
            78                --x;
            79                printf("%d\n", get(x));
            80            }

            81            else if(cmd == 'C'{
            82                scanf("%d %d"&x, &num);
            83                --x;
            84                Add(x, num);
            85            }

            86            else if(cmd == 'U'{
            87                scanf("%d %d"&x, &num);
            88                --x;
            89                int y = (x-num+1% n;
            90                if(y < 0) y += n;
            91                Add(y, num);
            92            }

            93        }

            94    }

            95
            96    return 0;
            97}

            Feedback

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-05-24 21:25 by terence_zhao
            good pro
            but cant follow you

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-05-25 20:31 by sicheng[I am oyjpArt]
            如果我們把這個(gè)環(huán)放成直線(準(zhǔn)確的說(shuō)是一個(gè)區(qū)間)來(lái)看的話,放入某一個(gè)籃子并且按照順時(shí)針旋轉(zhuǎn)一直放num,相當(dāng)于在這個(gè)區(qū)間插入很多條線段。而進(jìn)一步說(shuō),我們可以考慮只有3中線段,比如
            區(qū)間是[0,4] 從3開(kāi)始插入長(zhǎng)度為11的線段 則可以分成
            [3,4]
            [0,4] * 2
            [0,0]
            而逆時(shí)針的情況很好處理,如果你現(xiàn)算出最后停在哪個(gè)點(diǎn)上,換一下起始點(diǎn)和終點(diǎn)就是順時(shí)針了.

            最后是線段樹(shù)了,我們把所有的線段都分別插入.最后統(tǒng)計(jì)詢問(wèn)中的點(diǎn)上有多少線段覆蓋就可以了.

            要進(jìn)行點(diǎn)的線段覆蓋查詢,有很多種做法,我覺(jué)得比較好的就是從葉節(jié)點(diǎn)向上到根節(jié)點(diǎn),去疊加覆蓋數(shù)就可以了.

            呵呵~~

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-06-03 14:01 by w
            建樹(shù)可以非遞歸話吧

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-10-13 10:57 by re: HOJ 11107
            謝謝大牛了,我搞了半天終于弄懂了什么原理哈

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-10-13 14:14 by re: HOJ 11107
            int get(int x) { // 獲得點(diǎn)的覆蓋次數(shù)
            44 int idx = upperbound+x-1, sum = 0;
            45 do {
            46 sum += T[idx].c;
            47 idx = (idx-1)>>1;
            48 } while(idx != 0);
            49 return sum;
            50}

            貌似這里有個(gè)錯(cuò)誤,你的代碼對(duì)這組數(shù)據(jù)通不過(guò):
            5 3
            C 1 5
            Q 1
            Q 5

            應(yīng)改為:

            int get(int x) { // 獲得點(diǎn)的覆蓋次數(shù)
            44 int idx = upperbound+x-1, sum = 0;
            45 do {
            46 sum += T[idx].c;
            47 idx -= 1;
            if(idx != -1) idx >>= 1;
            48 } while(idx >= 0);
            49 return sum;
            50}

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2008-10-16 02:30 by oyjpart
            Thx!~

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2009-03-23 20:41 by 成大才子
            絕對(duì)大牛

            # re: HOJ 11107   回復(fù)  更多評(píng)論   

            2009-03-26 00:27 by alpc12
            久久精品成人欧美大片| 一本综合久久国产二区| 久久亚洲精品国产亚洲老地址| 久久黄色视频| 久久99久久99精品免视看动漫| 91久久精品91久久性色| 久久久久九国产精品| 久久午夜免费视频| 久久91综合国产91久久精品| 一本久久a久久精品亚洲| 国产免费久久精品丫丫| 亚洲精品无码久久千人斩| 人妻无码久久精品| 久久国产一区二区| 久久这里只精品99re66| 99久久免费只有精品国产| 国产成人综合久久综合| 亚洲午夜精品久久久久久app| 久久精品国产69国产精品亚洲 | 深夜久久AAAAA级毛片免费看 | 国内精品免费久久影院| 久久国产精品成人影院| 国产成人久久精品一区二区三区| 99久久精品九九亚洲精品| 人妻精品久久无码专区精东影业| 精品多毛少妇人妻AV免费久久| 99精品久久久久久久婷婷| 久久精品国产亚洲av麻豆小说 | 久久无码AV中文出轨人妻| 久久人人爽人人澡人人高潮AV | 久久久久精品国产亚洲AV无码 | 久久国产精品偷99| 国产A级毛片久久久精品毛片| 国产精品免费福利久久| 狠色狠色狠狠色综合久久| 久久久久99精品成人片试看| 亚洲欧美日韩久久精品第一区| 综合网日日天干夜夜久久| 99蜜桃臀久久久欧美精品网站| 一本久道久久综合狠狠爱| 久久发布国产伦子伦精品|