题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031
Problem 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.
 
题目大意:http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=18709&messageid=1&deep=0
思路:首先网上的大部分解法复杂度都是没有保证的,虽然能过,但是感觉依然可以卡掉。
————————————————————————————————————————————————————————————
官方题解:线段树,标程是离线算法,按时间建立线段树,然后按区间的先后顺序插进去。
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
 
代码(1453MS):
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = ;
const int MAXT = MAXN << ;
const int MAXP = ; 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 * ];
int ans[MAXN];
int T, n, q, t, ncnt, atime; void init() {
for(int i = ; i <= n; ++i) qtime[i].clear(), qid[i].clear();
memset(ans, -, q * sizeof(int));
ncnt = atime = ;
} #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 = ; 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][] = empty[x][] = cnt[l] = ;
for(int i = ; i < t; ++i)
atk[x][i] = , empty[x][i] = i - ;
} else {
build(ll, l, mid);
build(rr, mid + , r);
update(x);
}
} void modify(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
atk[x][] = ; empty[x][] = cnt[a] ? t - : ;
for(int i = ; i < t; ++i)
atk[x][i] = cnt[a], empty[x][i] = i - ;
} else {
if(a <= mid) modify(ll, l, mid, a, b);
if(mid < b) modify(rr, mid + , r, a, b);
update(x);
}
} void modify(int pos) {
modify(, , 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 + , r, a, b, empty[ll][e]);
return res;
}
} int query(int pos) {
if(pos == ) return ;
return query(, , atime, , pos, );
} char s[]; int main() {
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase) {
scanf("%d%d%d", &n, &q, &t);
init();
for(int i = , a, b; i < q; ++i) {
scanf("%s", s);
if(strcmp(s, "Attack") == ) {
scanf("%d%d", &a, &b);
atime++;
attack[ncnt++] = Node(a, , atime);
attack[ncnt++] = Node(b + , -, atime);
} else {
scanf("%d", &a);
qtime[a].push_back(atime);
qid[a].push_back(i);
}
}
sort(attack, attack + ncnt); build(, , atime);
int p = ;
for(int i = ; 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 = ; k < qtime[i].size(); ++k)
ans[qid[i][k]] = query(qtime[i][k]);
} printf("Case %d:\n", kase);
for(int i = ; i < q; ++i)
if(ans[i] != -) printf("%d\n", ans[i]);
}
}

HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)的更多相关文章

  1. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  2. HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...

  3. HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...

  4. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  6. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  8. HDU 4031 Attack (线段树)

    成功袭击次数=所有袭击次数-成功防守次数 需要一个辅助pre来记录上一次袭击成功什么时候,对于每个查询,从上一次袭击成功开始,每隔t更新一次. 感觉这样做最坏时间复杂度是O(n^2),这里 说是O(q ...

  9. HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...

随机推荐

  1. 关于YARN的HA

    一:准备 1.规划 namenode               namenode ZKFC ZKFC journalnode        journalnode               jou ...

  2. J2SE 1.6 特性:java.lang.instrument

    1. import java.lang.instrument.Instrumentation; public class ObjectSizeFetcher { private static Inst ...

  3. 过滤android应用列表(区分系统应用、第三方应用、sd卡中的应用)

    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // 系统程序 }else if ((app.flags & Applica ...

  4. httpclient提交json参数

    private void httpReqUrl(List<HongGuTan> list, String url) throws ClientProtocolException, IOEx ...

  5. 创建ArcGIS API for JavaScript的第一个示例程序

    原文:创建ArcGIS API for JavaScript的第一个示例程序 在上一篇博客中已经介绍了如何搭建ArcGIS API for JavaScript开发环境,如果您还没有搭建好开发环境的话 ...

  6. Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在

    非常简单的源码,敬请各位小主参阅.若有不足之处,敬请大神指正,不胜感激! /** * Verify the element exist or not * * @author Aaron.ffp * @ ...

  7. JS-003-innerText 与 innerHTML 区别

    此文主要讲述在使用 innerText 和 innerHTML 获取元素中间值时的差别,我个人将二者的区别简单的理解为: webelement.innerText : 获取的是页面元素显示的文本 we ...

  8. C++经典编程题#3:数字求和

    总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小于a的整数的和是多少? 输入 输入一行,只包括6个小于100的正整数, ...

  9. imx6 kernel clock

    前段时间查看了uboot的时钟,kernel的也稍微了解了下,记录于此,以后再来补充完善. board-mx6q_sabresd.c MACHINE_START(MX6Q_SABRESD, " ...

  10. A股中为什么有涨幅超过10%的

    在A股规定涨跌幅10%的正常交易日中,某些股票的涨跌幅却超出或没达到10%的,原因是因为A股的交易中最小的价格“申报单位为0.01元”,就是说某些股票不管什么价格都不会刚刚好是10%,它只能“取最近于 ...