HDU 4031 Attack(离线+线段树)

A - A
Time Limit:3000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

Description

Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack American again. However, American is protected by a high wall this time, which can be treating as a segment with length N. Al Qaeda has a super weapon, every second it can attack a continuous range of the wall. American deployed N energy shield. Each one defends one unit length of the wall. However, after the shield defends one attack, it needs t seconds to cool down. If the shield defends an attack at kth second, it can’t defend any attack between (k+1)th second and (k+t-1)th second, inclusive. The shield will defend automatically when it is under attack if it is ready. 

During the war, it is very important to understand the situation of both self and the enemy. So the commanders of American want to know how much time some part of the wall is successfully attacked. Successfully attacked means that the attack is not defended by the shield. 
 

Input

The beginning of the data is an integer T (T ≤ 20), the number of test case. 
The first line of each test case is three integers, N, Q, t, the length of the wall, the number of attacks and queries, and the time each shield needs to cool down. 
The next Q lines each describe one attack or one query. It may be one of the following formats 
1. Attack si ti 
  Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N 
2. Query p 
  How many times the pth unit have been successfully attacked. 1 ≤ p ≤ N 
The kth attack happened at the kth second. Queries don’t take time. 
1 ≤ N, Q ≤ 20000 
1 ≤ t ≤ 50 
 

Output

For the ith case, output one line “Case i: ” at first. Then for each query, output one line containing one integer, the number of time the pth unit was successfully attacked when asked.
 

Sample Input

       
       
2 3 7 2 Attack 1 2 Query 2 Attack 2 3 Query 2 Attack 1 3 Query 1 Query 3 9 7 3 Attack 5 5 Attack 4 6 Attack 3 7 Attack 2 8 Attack 1 9 Query 5 Query 3
 

Sample Output

       
       
Case 1: 0 1 0 1 Case 2: 3 2
 

官方题解:线段树,标程是离线算法,按时间建立线段树,然后按区间的先后顺序插进去。
1001就是从1到n依次搞定每个点在那些时间被攻击过,由于每个攻击区间都是连续的,所以那些时间可以靠一个线段树来维护起 
————————————————————————————————————————————————————————————
其中一个正解是:离线处理,从1~n的墙按顺序处理答案。然后,可以注意到t最多只有50。
开一棵时间线段树,在我的代码中:
atk[x][i]代表在线段树结点x代表的时间区间中,防御壁一开始有 i 个单位时间的无法防御期,此时走过 x 中的时间,会受到的攻击次数。
empty[x][i]代表在线段树结点x代表的时间区间中,防御壁一开始有 i 个单位时间的无法防御期,此时走过 x 中的时间后,会有的无法防御时间。
 
PS:还有一种分块的在线做法:http://blog.renren.com/share/240115026/8571592218
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int MAXN = 20010;
const int MAXT = MAXN << 2;
const int MAXP = 55;

struct Node {
    int pos, op, time;
    Node() {}
    Node(int pos, int op, int time): pos(pos), op(op), time(time) {}
    bool operator < (const Node &rhs) const {
        return pos < rhs.pos;
    }
};

vector<int> qtime[MAXN], qid[MAXN];
Node attack[MAXN * 2];
int ans[MAXN];
int T, n, q, t, ncnt, atime;

void init() {
    for(int i = 0; i <= n; ++i) qtime[i].clear(), qid[i].clear();
    memset(ans, -1, q * sizeof(int));
    ncnt = atime = 0;
}

#define ll (x << 1)
#define rr (ll | 1)
#define mid ((l + r) >> 1)
int atk[MAXT][MAXP], empty[MAXT][MAXP];
int cnt[MAXN];

void update(int x) {
    for(int i = 0; i < t; ++i) {
        int t = empty[ll][i];
        atk[x][i] = atk[ll][i] + atk[rr][t];
        empty[x][i] = empty[rr][t];
    }
}

void build(int x, int l, int r) {
    if(l == r) {
        atk[x][0] = empty[x][0] = cnt[l] = 0;
        for(int i = 1; i < t; ++i)
            atk[x][i] = 0, empty[x][i] = i - 1;
    } else {
        build(ll, l, mid);
        build(rr, mid + 1, r);
        update(x);
    }
}

void modify(int x, int l, int r, int a, int b) {
    if(a <= l && r <= b) {
        atk[x][0] = 0; empty[x][0] = cnt[a] ? t - 1 : 0;
        for(int i = 1; i < t; ++i)
            atk[x][i] = cnt[a], empty[x][i] = i - 1;
    } else {
        if(a <= mid) modify(ll, l, mid, a, b);
        if(mid < b) modify(rr, mid + 1, r, a, b);
        update(x);
    }
}

void modify(int pos) {
    modify(1, 1, atime, pos, pos);
}

int query(int x, int l, int r, int a, int b, int e) {
    if(a <= l && r <= b) {
        return atk[x][e];
    } else {
        int res = query(ll, l, mid, a, b, e);
        if(mid < b) res += query(rr, mid + 1, r, a, b, empty[ll][e]);
        return res;
    }
}

int query(int pos) {
    if(pos == 0) return 0;
    return query(1, 1, atime, 1, pos, 0);
}

char s[10];

int main() {
    scanf("%d", &T);
    for(int kase = 1; kase <= T; ++kase) {
        scanf("%d%d%d", &n, &q, &t);
        init();
        for(int i = 0, a, b; i < q; ++i) {
            scanf("%s", s);
            if(strcmp(s, "Attack") == 0) {
                scanf("%d%d", &a, &b);
                atime++;
                attack[ncnt++] = Node(a, 1, atime);
                attack[ncnt++] = Node(b + 1, -1, atime);
            } else {
                scanf("%d", &a);
                qtime[a].push_back(atime);
                qid[a].push_back(i);
            }
        }
        sort(attack, attack + ncnt);

        build(1, 1, atime);
        int p = 0;
        for(int i = 1; i <= n; ++i) {
            while(p < ncnt && attack[p].pos == i) {
                cnt[attack[p].time] += attack[p].op;
                modify(attack[p++].time);
            }
            for(size_t k = 0; k < qtime[i].size(); ++k)
                ans[qid[i][k]] = query(qtime[i][k]);
        }

        printf("Case %d:\n", kase);
        for(int i = 0; i < q; ++i)
            if(ans[i] != -1) printf("%d\n", ans[i]);
    }
}




Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值