hdu 1540 线段树
来源:互联网 发布:九月份非农数据 编辑:程序博客网 时间:2024/06/05 11:31
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540
题意:有n个点依次链接,先给出三种操作:
- 删除某个点
- 修复某个点
- 查询与某个点相连的点的个数
解法:主要是query函数的写法。利用回溯,分情况判断左/右是否联通。注意自定义的结构一定要赋初值。
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define PAUSE system("pause")
#define Pr printf
#define Sc scanf
#define pb push_back
#define Sort(v) sort(v.begin(), v.end())
#define Unique(v) (unique(v.begin(), v.end())-v.begin())
#define MM(x, y) memset(x, y,sizeof(x))
#define For(i, s, t) for(int i=s; i<(t); i++)
#define eps (1e-9)
#define pi 3.1415926
#define N 50005
struct Statue
{
bool l, r;
int len;
Statue(int len=0,bool l=false,bool r=false): len(len), l(l), r(r){}
};
struct SegmentTreeNode
{
bool ok;
}tree[N<<2];
int used[N];
void build(int l,int r,int p)
{
tree[p].ok = true;
if(l == r)
return;
int m = (l+r)>>1;
build(l, m, p<<1);
build(m+1, r, p<<1|1);
}
void updata(int l,int r,int p, int k,bool ok)
{
if(l == r)
{
tree[p].ok = ok;
return ;
}
int m = (l+r)>>1;
if(k <= m)
updata(l, m, p<<1, k, ok);
else
updata(m+1, r, p<<1|1, k, ok);
tree[p].ok = tree[p<<1].ok&& tree[p<<1|1].ok;
}
Statue query(int l, int r, int p,int k)
{
if(tree[p].ok == true)
{
return Statue(r-l+1,true,true);
}
if(l == r)
{
return Statue(0,false,false);
}
int m = (l+r)>>1;
Statue s1, s2;//忘记给s1, s2赋初值,蛋疼了好久。
if(k <= m)
{
s1 = query(l, m, p<<1, k);
if(s1.r == true)
s2 = query(m+1, r, p<<1|1, m+1);
return Statue(s1.len+s2.len, s1.l, s2.r);
}
else
{
s2 = query(m+1, r, p<<1|1, k);
if(s2.l == true)
s1 = query(l, m, p<<1, m);
return Statue(s1.len+s2.len, s1.l, s2.r);
}
}
int n, m;
int main()
{
while(Sc("%d%d",&n,&m) != EOF)
{
stack<int> vec;
MM(used, 0);
build(1, n, 1);
For(i, 0, m)
{
static char ch;
static int x;
Sc(" %c", &ch);
if(ch == 'R')
{
while( !used[vec.top()] )
vec.pop();
updata(1, n, 1, vec.top(), true);
used[vec.top()] = 0;
vec.pop();
}
else
{
Sc("%d", &x);
if(ch == 'D')
{
vec.push(x);
used[x]++;
updata(1, n, 1, vec.top(), false);
}
else
{
Statue s = query(1, n,1, x);
Pr("%d\n", s.len);
}
}
}
}
return 0;
}
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define PAUSE system("pause")
#define Pr printf
#define Sc scanf
#define pb push_back
#define Sort(v) sort(v.begin(), v.end())
#define Unique(v) (unique(v.begin(), v.end())-v.begin())
#define MM(x, y) memset(x, y,sizeof(x))
#define For(i, s, t) for(int i=s; i<(t); i++)
#define eps (1e-9)
#define pi 3.1415926
#define N 50005
struct Statue
{
bool l, r;
int len;
Statue(int len=0,bool l=false,bool r=false): len(len), l(l), r(r){}
};
struct SegmentTreeNode
{
bool ok;
}tree[N<<2];
int used[N];
void build(int l,int r,int p)
{
tree[p].ok = true;
if(l == r)
return;
int m = (l+r)>>1;
build(l, m, p<<1);
build(m+1, r, p<<1|1);
}
void updata(int l,int r,int p, int k,bool ok)
{
if(l == r)
{
tree[p].ok = ok;
return ;
}
int m = (l+r)>>1;
if(k <= m)
updata(l, m, p<<1, k, ok);
else
updata(m+1, r, p<<1|1, k, ok);
tree[p].ok = tree[p<<1].ok&& tree[p<<1|1].ok;
}
Statue query(int l, int r, int p,int k)
{
if(tree[p].ok == true)
{
return Statue(r-l+1,true,true);
}
if(l == r)
{
return Statue(0,false,false);
}
int m = (l+r)>>1;
Statue s1, s2;//忘记给s1, s2赋初值,蛋疼了好久。
if(k <= m)
{
s1 = query(l, m, p<<1, k);
if(s1.r == true)
s2 = query(m+1, r, p<<1|1, m+1);
return Statue(s1.len+s2.len, s1.l, s2.r);
}
else
{
s2 = query(m+1, r, p<<1|1, k);
if(s2.l == true)
s1 = query(l, m, p<<1, m);
return Statue(s1.len+s2.len, s1.l, s2.r);
}
}
int n, m;
int main()
{
while(Sc("%d%d",&n,&m) != EOF)
{
stack<int> vec;
MM(used, 0);
build(1, n, 1);
For(i, 0, m)
{
static char ch;
static int x;
Sc(" %c", &ch);
if(ch == 'R')
{
while( !used[vec.top()] )
vec.pop();
updata(1, n, 1, vec.top(), true);
used[vec.top()] = 0;
vec.pop();
}
else
{
Sc("%d", &x);
if(ch == 'D')
{
vec.push(x);
used[x]++;
updata(1, n, 1, vec.top(), false);
}
else
{
Statue s = query(1, n,1, x);
Pr("%d\n", s.len);
}
}
}
}
return 0;
}
- hdu 1540 线段树
- hdu 1540 线段树
- hdu 1540 Tunnel Warfare 线段树
- hdu-1540-Tunnel Warfare-线段树
- 【HDU】 1540 Tunnel Warfare 线段树
- hdu 1540 线段树区间合并
- hdu 1540 Tunnel Warfare【线段树】
- hdu 1540 神奇的线段树
- hdu 1540 Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare 线段树
- hdu 1540 线段树+区间合并
- hdu-1540(线段树+最大连续区间)
- HDU 1540 Tunnel Warfare [二分 + 线段树]
- hdu 1540 线段树区间合并
- HDU 1540<线段树,区间并>
- 线段树--区间合并--HDU 1540
- HDU 1540 线段树区间合并
- mysql 性能优化的几点建议
- 快速排序源代码
- Javascript高级程序设计1-2章回顾总结
- 理解SQL原理,写出高效的SQL语句
- Agile Web Application Development with Yii 1.1 and PHP5 读后总结(5)Task的所有...
- hdu 1540 线段树
- unix IPC 应知应会
- reason for dead lock, dead looping?
- Pro*C的一些基本操作
- 发光二极管之二——正负极如何判断
- 【二分】软件下载解题报告
- poj1273-----最大流基础(最简单的最大流!--模板题)
- 2013 google校园招聘笔试题
- 转载数据结构之kmp模式匹配