hdu 4267 A Simple Problem with Integers(樹形結構-線段樹),hduintegers

來源:互聯網
上載者:User

hdu 4267 A Simple Problem with Integers(樹形結構-線段樹),hduintegers
A Simple Problem with IntegersTime Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3708    Accepted Submission(s): 1139


Problem DescriptionLet A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element. 
InputThere are a lot of test cases. 
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
 
OutputFor each test case, output several lines to answer all query operations. 
Sample Input

4 1 1 1 1142 12 22 32 41 2 3 1 22 1 2 22 32 41 1 4 2 12 12 22 32 4
 
Sample Output
111113312341
 首先,我們來設計一下頂層模型。看:
假設我們先插入了3段:L1 R1 k1 c1L2 R2 k2 c2L3 R3 k3 c3然後查詢圖中的i點,那麼,我只要遍曆所有插入的線段裡包含i點的線段,然後判斷(i-L)%k是否==0,若等於0,就將i點的初始值加上c。實現:暴力實現肯定會逾時,逾時就在於找包含i點的線段上。我們可以用線段樹來解決尋找包含i點的線段的問題。見:
像圖中找點2時,其實線段樹查詢到2走過的每個線段都包含了2這個點。那麼,現在問題就轉換為:只要在當前這個線段判斷(i-l)%k==0,然後再+c就行了。又因為1<=k<=10,範圍很小,所以每個線段都有一個數組sum[11]來記錄插入該段的資訊,比如當前這個線段內某點i滿足(i-l)%k==0,就可以+c,那麼sum[k] = c。例如,我們插入:a b k c1 2 2 1那麼,[1,2]線段裡的sum[2] = 1,假設我們現在查詢1這個點,走到[1,2]這段時,因為(1-1)%2==0,所以最終答案就是初始值+1.上面的例子是插入的線段往左兒子方向的情況,但是當要往右兒子方向的情況的時候會有一個問題,看下面的例子。假設我們插入:a b k c2 4 2 1
我們發現一個問題,就是當[2,4]往右子樹走時,區間變成了[3,4],但是[3,4]這個區間的左邊界L=3,(L-a)%k = (3-2)%2 != 0,這樣一來,我們查詢點3的時候就會錯,因為查詢點3時,經過[3,4],(3-3)%2 == 0那麼答案會+1,但是(3-2)%2 !=0。因此,我們插入的時候必須保證左端點(L-a)%k 一定等於0。所以,當往右邊走的時候,要對區間稍作處理,一般我們會這樣寫:update(mid+1 , r , 2*k+1);現在是:int tl = mid+1+(k-(mid+1)%k)%k;update(tl , r , 2*k+1);當然tl<=r,否則就不必往下插入了。
#include <iostream>#include <cstdio>using namespace std;const int maxn = 50010;struct tree{    int l , r, k[12];}a[4*maxn];int num[maxn] , N , Q;void build(int l , int r , int k){    a[k].l = l;    a[k].r = r;    for(int i = 0; i < 12; i++) a[k].k[i] = 0;    if(l != r){        int mid = (l+r)/2;        build(l , mid , 2*k);        build(mid+1 , r , 2*k+1);    }}void update(int l , int r , int k , int lk , int lc){    if(l <= a[k].l && a[k].r <= r) a[k].k[lk] += lc;    else{        int mid = (a[k].l+a[k].r)/2;        if(mid >= r) update(l , r, 2*k , lk , lc);        else if(mid < l) update(l , r , 2*k+1 , lk , lc);        else{            update(l , mid , 2*k , lk , lc);            int tl = mid+1+(lk-(mid+1-l)%lk)%lk;            if(tl <= r) update(tl , r , 2*k+1 , lk , lc);        }    }}int getAns(int k , int i){    int ans = 0;    for(int j = 1; j <= 10; j++){        if((i-a[k].l)%j == 0) ans += a[k].k[j];    }    return ans;}int query(int l , int r , int k){    if(l <= a[k].l && a[k].r <= r) return getAns(k , l);    else{        int mid = (a[k].l+a[k].r)/2;        int ans = getAns(k , l);        if(mid >= r) return ans+query(l , r , 2*k);        else return ans+query(l , r , 2*k+1);    }}void computing(){    for(int i = 1; i <= N; i++) scanf("%d" , &num[i]);    build(1 , N , 1);    scanf("%d" , &Q);    int op , la, lb , lk , lc;    while(Q--){        scanf("%d" , &op);        if(op == 1){            scanf("%d%d%d%d" , &la , &lb , &lk , &lc);            update(la , lb , 1 , lk , lc);        }else{            scanf("%d" , &la);            printf("%d\n" , num[la]+query(la , la , 1));        }    }}int main(){    while(~scanf("%d" , &N)){        computing();    }    return 0;}



poj 3468 A Simple Problem with Integers我的程式(線段樹做的)我不知道錯什地方了,幫看一下

:36</span>
</p>
<p class="ft p1">
 
誰有資料結構的演算法跟POJ題目的對應關係

打星號的表示個人認為比較經典,或是演算法比較好的題目

1195 Mobile phones 樹狀數組
1455
1521 Entropy huffman
1703 Find them, Catch them 並查集
1785 Binary Search Heap Construction
1794 Castle Walls 逆序對
1961 Period KMP重複因子
1984* Navigation Nightmare 並查集+座標平移
1986* Distance Queries LCA
1988* Cube Stacking 並查集應用
1990* MooFest 線段樹
2010* Moo University - Financial Aid 最大堆-最小堆
2182 Lost Cows 線段樹
2183 Bovine Math Geniuses hash
2188 Cow Laundry 逆序對
2227 The Wedding Juicer 堆+floodfill
2236 Wireless Network 並查集
2266* Quadtree 遞迴構造四叉樹
2269* Friends 運算式
2270 Quadtree II or: Florida Jones strikes back 將2266反之
2299 Ultra-QuickSort 歸併排序
2352 Stars 樹狀數組
2395 Out of Hay 並查集
2482 Stars in Your Window 靜態2叉樹
2513 Colored Sticks 並查集
2524 Ubiquitous Religions 並查集
2528 Mayor's posters 線段樹
2567 Code the Tree
2750* Potted Flower 線段樹
2777 Count Color 線段樹
2796 Feel Good RMQ
2823 Sliding Window 堆或雙端隊列
2828 Buy Tickets 線段樹
2886* Who Gets the Most Candies? 線段樹
2892* Tunnel Warfare 樹狀數組
3214* Heap 後序遍曆,每個節點減去相應sub保證屬性,然後對遍曆結果求最長不下降序列
3253 Fence Repair huffman
3263 Tallest Cow 線段樹
3274* Gold Balanced Lineup hash
3277 City Horizon 線段樹
3320 Jessica's Reading Problem 隊列操作或最小堆
3321* Apple Tree 樹狀數組
3332 Parsing Real Numbers DFA
3344 Chessboard Dance 隊列類比
3349 Snowflake Snow Snowflakes hash(or 暴力)
3437 Tree Grafting dfs樹構造
3461 Oulipo KMP
3468 A Simple Problem with Integers 線段樹區間更新,懶操作
3631 Cuckoo Hashing 並查集
3667 Hotel 線段樹
3690 Constellations trie匹配
3695 Rectangles 矩陣切割...餘下全文>>
 

聯繫我們

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