hdu1540 && POJ2892 Tunnel Warfare,

來源:互聯網
上載者:User

hdu1540 && POJ2892 Tunnel Warfare,
【比賽提醒】BestCoder 你報名了嗎?(點擊報名)
【科普】什麼是BestCoder?如何參加?
Tunnel WarfareTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4513    Accepted Submission(s): 1725


Problem DescriptionDuring the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
 
InputThe first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
 
OutputOutput the answer to each of the Army commanders’ request in order on a separate line.
 
Sample Input

7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 
Sample Output
1024
 
SourcePOJ Monthly 
RecommendLL   |   We have carefully selected several similar problems for you:  1542 1255 1698 1828 2871

這道題也是典型的線段樹區間合并的題, 主要是要記錄一段區間左邊連續的長度, 右邊連續長度, 單點更新,查詢時只要判斷要查的點是不是落在中間連續處,否則就繼續遞迴查詢

/*************************************************************************    > File Name: hdu1540.cpp    > Author: ALex    > Mail: 405045132@qq.com     > Created Time: 2015年01月11日 星期日 12時24分34秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 500010;stack <int> re;struct node{int l, r;int l_sum;int r_sum;}tree[N << 2];void pushup (int p){tree[p].l_sum = tree[p << 1].l_sum;tree[p].r_sum = tree[p << 1 | 1].r_sum;if (tree[p << 1].l_sum == tree[p << 1].r - tree[p << 1].l + 1){tree[p].l_sum += tree[p << 1 | 1].l_sum;}if (tree[p << 1 | 1].r_sum == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1){tree[p].r_sum += tree[p << 1].r_sum;}}void build (int p, int l, int r){tree[p].l = l;tree[p].r = r;tree[p].l_sum = tree[p].r_sum = (r - l + 1);if (l == r){return;}int mid = (l + r) >> 1;build (p << 1, l, mid);build (p << 1 | 1, mid + 1, r);}void update (int p, int pos, int sta){if (tree[p].l == tree[p].r){tree[p].l_sum = tree[p].r_sum = sta;return;}int mid = (tree[p].l + tree[p].r) >> 1;if (pos <= mid){update (p << 1, pos, sta);}else{update (p << 1 | 1, pos, sta);}pushup (p);}int query (int p, int pos){if (tree[p].l == tree[p].r){if (tree[p].l_sum == 1){return 1;}return 0;}int mid = (tree[p].l + tree[p].r) >> 1;if (pos <= mid){if (pos >= mid - tree[p << 1].r_sum + 1){return tree[p << 1].r_sum + tree[p << 1 | 1].l_sum;}return query (p << 1, pos);}else{if (pos <= mid + tree[p << 1 | 1].l_sum){return tree[p << 1 | 1].l_sum + tree[p << 1].r_sum;}return query (p << 1 | 1, pos);}}int main(){int n, m;int x;char op[10];while (~scanf("%d%d", &n, &m)){build (1, 1, n);while (!re.empty()){re.pop();}while (m--){scanf("%s", op);if(op[0] == 'R'){if (re.empty()){continue;}update (1, re.top(), 1);re.pop();}else{scanf("%d", &x);if (op[0] == 'D'){update (1, x, 0);re.push(x);}else{printf("%d\n", query (1, x));}}}}return 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.