hdu 4031 Attack(樹狀數組)

來源:互聯網
上載者:User

題目連結:hdu 4031 Attack


題目大意:有一個長為n的長城,進行q次操作,d為防護罩的冷卻時間,Attack表示區間a-b的牆將在1秒後受到攻擊,

詢問表示計算第a塊牆受到攻擊的次數,被防護罩抵消的不算。


解題思路:樹狀數組,更新區間查詢點,每次攻擊區間a-b時,只要進行add(a,1); add(b+1, -1);然後第i堵牆受到的總攻擊次數即為sum(i)。實際受到攻擊的次數=總攻擊次數-防護次數。防護次數的計算是將每次攻擊的區間記錄下來,然後枚舉被抵擋的次數。


#include <stdio.h>#include <string.h>const int N = 20005;int n, q, d, ti, v[N], l[N], r[N], cnt[N], rec[N];void init() {scanf("%d%d%d", &n, &q, &d);ti = 0;memset(cnt, 0, sizeof(cnt));memset(rec, 0, sizeof(rec));memset(v, 0, sizeof(v));memset(l, 0, sizeof(l));memset(r, 0, sizeof(r));}void add(int x, int val) {while (x <= n) {v[x] += val;x += (x & (-x));}}int sum(int x) {int ans = 0;while (x > 0) {ans += v[x];x -= (x &(-x));}return ans;}void solve() {int a, b;char o[10];for (int i = 0; i < q; i++) {scanf("%s", o);if (o[0] == 'A') {scanf("%d%d", &a, &b);add(a, 1); add(b+1, -1);l[ti] = a, r[ti] = b;ti++;} else {scanf("%d", &a);for (int j = rec[a]; j < ti; j++) if (l[j] <= a && r[j] >= a) {rec[a] = j + d;cnt[a]++;j += d - 1;}printf("%d\n", sum(a) - cnt[a]);}}}int main() {int cas;scanf("%d", &cas);for (int i = 1; i <= cas; i++) {printf("Case %d:\n", i);init();solve();}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.