Tunnel Warfare
Time limit:1 Sec Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=1540
Descriptionduring the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast areas of nor Th China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the ends, every village was directly connected with the 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 is severely isolated, restoration of connection must is done immediately! Inputthe first line of the input contains, 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 is 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 is directly or indirectly connected with includ ING itself.
R:the village destroyed was rebuilt.
Outputoutput the answer to all of the Army commanders ' request in order on a separate line.
Sample Input7 9 d 3 d 6 d 5 Q 4 q 5 r q 4 r q 4Sample Output1
0
2
4
HINT
Test instructions
Give you an interval, there are three operations to make a village destroy, to revive the last destroyed village, to inquire the longest interval of the village
Exercises
Ah, is a simple segment of the merging of the tree, single-point modification, record the maximum value from the left, the maximum value on the right, the maximum value of the interval and then each update is good ~
Code:
Qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long Long LL;
using namespace Std;
Freopen ("D.in", "R", stdin);
Freopen ("D.out", "w", stdout);
#define Sspeed ios_base::sync_with_stdio (0); Cin.tie (0)
#define MAXN 150001
#define MOD 10007
#define EPS 1e-9
const int INF=0X7FFFFFFF; Infinitely large
const int inf=0x3f3f3f3f;
/*
int buf[10];
inline void write (int i) {
int p = 0;if (i = = 0) p++;
else while (i) {buf[p++] = i% 10;i/= 10;}
for (int j = p-1; J >=0; j--) Putchar (' 0 ' + buf[j]);
printf ("\ n");
}
*/
//**************************************************************************************
inline ll read () {
int X=0,f=1;char Ch=getchar ();
while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
}
struct node{
int l,r;
int Len;
int lm,rm,mm;
};
Node A[maxn*4];
void build (int l,int r,int x) {
A[x].l=l,a[x].r=r;
a[x].lm=a[x].rm=a[x].len=a[x].mm=r-l+1;
if (l!=r) {
int mid= (L+R) >>1;
Build (l,mid,x<<1);
Build (mid+1,r,x<<1|1);
}
}
void Updata (int i,int t,int x)
{
if (a[i].l = = A[I].R) {a[i].lm = A[i].rm = A[I].MM = X;return;}
int mid = (A[I].L+A[I].R) >>1;
if (t<=mid) Updata (2*i,t,x);
else Updata (2*i+1,t,x);
A[I].LM = A[2*I].LM;
A[I].RM = a[2*i+1].rm;
A[I].MM = Max (max (a[2*i].mm,a[2*i+1].mm), A[2*I].RM+A[2*I+1].LM);
if (a[2*i].lm = = a[2*i].r-a[2*i].l+1) A[i].lm + = A[2*I+1].LM;
if (a[2*i+1].rm = = a[2*i+1].r-a[2*i+1].l+1) a[i].rm + = a[2*i].rm;
}
int query (int x,int p)
{
if (a[x].l==a[x].r| | a[x].mm==0| | A[x].mm==a[x].len)
return a[x].mm;
int mid= (A[X].L+A[X].R) >>1;
if (P<=mid) {
if (p>=a[x<<1].r-a[x<<1].rm+1)
return query (x<<1,p) +query (x<<1|1,mid+1);
Else
return query (X<<1,P);
}
else{
if (p<=a[x<<1|1].l+a[x<<1|1].lm-1)
return query (x<<1|1,p) +query (X<<1,mid);
Else
return query (X<<1|1,P);
}
}
int main ()
{
int n,m,y;
Char x[3];
while (scanf ("%d%d", &n,&m)!=eof)
{
Stack<int> K;
memset (A,0,sizeof (a));
Build (1,n,1);
for (int i=0;i<m;i++)
{
scanf ("%s", x);
if (x[0]== ' D ')
{
Y=read ();
Updata (1,y,0);
K.push (y);
}
else if (x[0]== ' Q ')
{
Y=read ();
printf ("%d\n", Query (1,y));
}
Else
{
Updata (1,k.top (), 1);
K.pop ();
}
}
}
}
HDU 1540 Tunnel Warfare segment Tree single point update, query interval length, interval merging