POJ 1195 Mobile phones (二維樹狀數組),poj1195

來源:互聯網
上載者:User

POJ 1195 Mobile phones (二維樹狀數組),poj1195

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3

Sample Output

34


二維樹狀數組求和與修改,從一維可以擴充到二維。


#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <set>#include <stack>#include <cctype>#include <algorithm>#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef long long LL;const int mod = 99999997;const int MAX = 1000000000;const int maxn = 100005;int ca, s, op;int c[1100][1100];void add(int i, int j, int v) {    while(i <= s) {        int y = j;        while(y <= s) {            c[i][y] += v;            y += y&-y;        }        i += i&-i;    }}int query(int i ,int j) {    int ans = 0;    while(i > 0) {        int y = j;        while(y > 0) {            ans += c[i][y];            y -= y&-y;        }        i -= i&-i;    }    return ans;}int main(){    cin >> ca >> s;    while(1) {        scanf("%d", &op);        if(op == 1) {            int a, b, v;            scanf("%d%d%d", &a, &b, &v);            add(a+1, b+1, v);        } else if(op == 2) {            int x1, x2, y1, y2;            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);            int ans = query(x2+1, y2+1) + query(x1, y1) - query(x2+1, y1) - query(x1, y2+1);            printf("%d\n", ans);        } else break;    }    return 0;}



誰有資料結構的演算法跟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 矩陣切割...餘下全文>>
 
POJ 2481 cows 怎做?樹狀數組+快排 逾時可以附帶Pascal程式?

/**
sort排序+樹狀數組, 不會逾時
按e降序排序, e相等則s小的優先,然後順序掃描排序後的數組,對每個cow只需求出
s小於等於它的cow個數即可, 但是要注意兩個cow,e和s都相等的情況。
**/
程式語言無所謂, 如果你pascal功底好,下面的
代碼,應該很好理解,附:
#include <stdio.h>
#include <string.h>
#include <algorithm>
const int N = 100010;
//樹狀數組
int tree[N];
int getsum(int idx) {
int sum = 0;
for (int i = idx; i > 0; i -= (i & -i))
sum += tree[i];
return sum;
}

int maxv;
void update(int idx, int val) {
for (int i = idx; i <= maxv; i += (i & -i))
tree[i] += val;
}

struct range {
int ord; //儲存排序前每個cow的序號
int s, e;
} cow[N];
//排序謂詞定義
int cmp(const range& x, const range& y) {
if (x.e == y.e)
return x.s < y.s;
return x.e > y.e;
}

int main()
{
int n, res[N];
while (scanf("%d", &n) != EOF && n) {
memset(tree, 0, sizeof(int)*(n+2));
maxv = 0;
for (int i = 0; i < n; ++i) {
scanf("%d %d", &cow[i].s, &cow[i].e);
++cow[i].s, ++cow[i].e;
if (cow[i].s > maxv) maxv = cow[i].s;
cow[i].ord = i;
}
std::sort(cow, cow + n, cmp);
for (int i = 0; i < n; ++i) {
if (cow[i].s == cow[i-1].s && cow[i].e == cow[i-1].e) //注意這裡,兩cow相同
res[cow[i].ord] = res[cow[i-1].ord];
else
res[cow[i].ord] = getsum(cow[i].s-1);
update(cow[i].s, 1);
}
printf("%d", res[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.