hdu 4031 attack 线段树区间更新
Attack
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2496 Accepted Submission(s): 788
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.
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
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /* hdu 4031 attack 线段树区间更新 problem: 每个位置有一个防御塔,每次敌人会对一个区间进行攻击。防御塔有一个冷却时间t。 问某个时间时,某个位置被成功攻击的次数(没有被防御) solve: 主要是怎么处理这个冷却值的问题,区间攻击都可以使用 线段树区间更新来解决。 最开始考虑的是通过 更新来维护每个点,那每次就要更新[1,n],感觉应该会超时吧 然后想到是 通过每次-1来体现时间的变化,但是无法知道每个点攻击成功的情况 所以直接暴力搞了- - 通过线段树记录被攻击了多少次,然后通过记录可以知道防御成功了多少次 于是就能得出答案。 hhh-2016-08-05 21:16:55 */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #include <map> #include <queue> #include <vector> #include <set> #pragma comment(linker, "/STACK:1024000000,1024000000") #define lson (i<<1) #define rson ((i<<1)|1) using namespace std; typedef long long ll; const int maxn=100000 + 10; const int INF=0x3f3f3f3f; const int mod = 1e9+7; int n; struct node { int l,r; int val; int len ; int mid() { return (l+r)>> 1; } } tree[maxn<<2]; void push_up( int i) { } void build( int i, int l, int r) { tree[i].l = l; tree[i].r = r; tree[i].val = tree[i].len = 0; if (l == r) { return ; } int mid = tree[i].mid(); build(lson,l,mid); build(rson,mid+1,r); push_up(i); } void push_down( int i) { if (tree[i].val) { tree[lson].val += tree[i].val; tree[rson].val += tree[i].val; tree[i].val = 0; } } void Insert( int i, int l, int r, int val) { if (tree[i].l >= l && tree[i].r <= r) { tree[i].val += val; push_up(i); return ; } push_down(i); int mid = tree[i].mid(); if (l <= mid) Insert(lson,l,r,val); if (r > mid) Insert(rson,l,r,val); push_up(i); } int query( int i, int k) { if (tree[i].l == tree[i].r && tree[i].l == k) { return tree[i].val; } push_down(i); int mid = tree[i].mid(); if (k <= mid) return query(lson,k); else return query(rson,k); } struct Point { int l,r; Point() { } Point( int a, int b) { l = a,r = b; } }; Point pt[maxn]; char op[5]; int main() { int T,n,m,k; int cas = 1; int a,b; //freopen("in.txt","r",stdin); scanf ( "%d" ,&T); while (T--) { scanf ( "%d%d%d" ,&n,&m,&k); int cnt = 0; build(1,1,n); printf ( "Case %d:\n" ,cas++); for ( int i = 1;i <= m;i++) { scanf ( "%s" ,op); if (op[0] == 'Q' ) { int num = 0; scanf ( "%d" ,&a); for ( int i = 0;i < cnt;) { if ( a>= pt[i].l && a <= pt[i].r) { num++; i += k; } else { i++; } } printf ( "%d\n" ,query(1,a) - num); } else { scanf ( "%d%d" ,&a,&b); Insert(1,a,b,1); pt[cnt++] = Point(a,b); } } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
· drools 规则引擎和 solon-flow 哪个好?solon-flow 简明教程