hdu 1540 Tunnel Warfare (区间线段树(模板))
2013-08-02 10:21
459 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1540
[align=left]Problem Description[/align]
During 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!
[align=left]Input[/align]
The 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.
[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.
[align=left]Sample Input[/align]
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
[align=left]Sample Output[/align]
1
0
2
4
[align=left]Source[/align]
POJ Monthly
【题解】:
区间线段树树:
节点:
struct Nod
{
int l,r; //左右
int ll,ml,rl; //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
}node[N<<2];
建树和更新应该是没问题的
下面我说下查询:
int query(int id,int p) //传入参数
{
if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1)
{
return node[p].ml;
}
int mid = (node[p].l+node[p].r)>>1;
if(id<=mid)
{
if(id>=node[lson].r-node[lson].rl+1) return query(id,lson)+query(mid+1,rson); //1
else query(id,lson);
}
else
{
if(id<=node[rson].l+node[rson].ll-1) return query(mid,lson)+query(id,rson); //2
else query(id,rson);
}
}
附上图形解释:
【code】:
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3168 Accepted Submission(s): 1216[align=left]Problem Description[/align]
During 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!
[align=left]Input[/align]
The 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.
[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.
[align=left]Sample Input[/align]
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
[align=left]Sample Output[/align]
1
0
2
4
[align=left]Source[/align]
POJ Monthly
【题解】:
区间线段树树:
节点:
struct Nod
{
int l,r; //左右
int ll,ml,rl; //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
}node[N<<2];
建树和更新应该是没问题的
下面我说下查询:
int query(int id,int p) //传入参数
{
if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1)
{
return node[p].ml;
}
int mid = (node[p].l+node[p].r)>>1;
if(id<=mid)
{
if(id>=node[lson].r-node[lson].rl+1) return query(id,lson)+query(mid+1,rson); //1
else query(id,lson);
}
else
{
if(id<=node[rson].l+node[rson].ll-1) return query(mid,lson)+query(id,rson); //2
else query(id,rson);
}
}
附上图形解释:
【code】:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<stack> #define N 50005 #define lson p<<1 #define rson p<<1|1 using namespace std; struct Nod { int l,r; //左右 int ll,ml,rl; //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说) }node[N<<2]; void building(int l,int r,int p) //建树 { node[p].l = l; node[p].r = r; node[p].ll=node[p].ml=node[p].rl=r-l+1; if(l==r) return; int mid = (l+r)>>1; building(l,mid,lson); building(mid+1,r,rson); } void update(int id,int p,int ok) { if(node[p].l==node[p].r) { if(ok==0) node[p].ll=node[p].ml=node[p].rl=0; else node[p].ll=node[p].ml=node[p].rl=1; return ; } int mid = (node[p].l+node[p].r)>>1; if(id<=mid) update(id,lson,ok); else update(id,rson,ok); node[p].ml = max(node[lson].ml,node[rson].ml); node[p].ml = max(node[p].ml,node[lson].rl+node[rson].ll); //最大中间值是 左右孩子的中间值 与 左边右最大+右边左最大 三者的最大值 node[p].ll = node[lson].ll; //左孩子的左最大 node[p].rl = node[rson].rl; //右孩子的右最大 if(node[lson].ll==node[lson].r-node[lson].l+1) node[p].ll+=node[rson].ll; if(node[rson].rl==node[rson].r-node[rson].l+1) node[p].rl+=node[lson].rl; } int query(int id,int p) { if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1) { return node[p].ml; } int mid = (node[p].l+node[p].r)>>1; if(id<=mid) { if(id>=node[lson].r-node[lson].rl+1) return query(id,lson)+query(mid+1,rson); //1 else query(id,lson); } else { if(id<=node[rson].l+node[rson].ll-1) return query(mid,lson)+query(id,rson); //2 else query(id,rson); } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { char op[5]; int id; stack<int> stk; building(1,n,1); while(m--) { scanf("%s",op); if(op[0]=='D') { scanf("%d",&id); stk.push(id); update(id,1,0); } else if(op[0]=='Q') { scanf("%d",&id); printf("%d\n",query(id,1)); } else if(op[0]=='R') { id = stk.top(); stk.pop(); update(id,1,1); } } while(!stk.empty()){stk.pop();} } return 0; }
相关文章推荐
- HDU-1540 Tunnel Warfare 线段树 区间合并
- hdu--1540 Tunnel Warfare(线段树+区间合并)
- HDU 1540 Tunnel Warfare(线段树,去最大连续区间)
- HDU - 1540 Tunnel Warfare(线段树 区间合并)
- HDU - 1540 Tunnel Warfare(线段树最大连续区间)
- 【Hdu】1540 Tunnel Warfare(线段树|区间合并)
- HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)
- HDU 1540 && POJ 2892 Tunnel Warfare (线段树,区间合并).
- hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
- hdu 1540 Tunnel Warfare(线段树区间合并)
- hdu 1540 Tunnel Warfare 线段树--区间合并
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare (线段树,区间合并)
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare (线段树区间合并)
- hdu 1540 Tunnel Warfare (线段树区间合并)
- HDU 1540 Tunnel Warfare(线段树 区间合并 +单点更新)
- 【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
- HDU-1540 Tunnel Warfare (线段树区间合并)