随笔分类 -  线段树+树状数组

poj-3321 Apple Tree ****
摘要:/* 看了网上的代码。。再自己写了一个。。。( 网上很多代码都是默认输入的数据u是v的父节点。。 也能AC。。 但不严谨。。)【转】思路:树状数组。这道题重点怎么建立树到树状数组的映射关系:利用dfs遍历树,对每个节点进行两次编号,第一次搜到第i个节点时的深度dep,为这个节点管辖区间的上限low[i](也为这个节点的下标),然后搜这个节点的子节点,最后搜回来后的深度dep,为这个节点管辖区间的下限high[i]。接下来就是树状数组部分了。*//* * 3321 * 邻接表建树+树状数组 * */#include <cstdio>using namespace std;const 阅读全文

posted @ 2011-07-11 21:43 龙豆 阅读(334) 评论(0) 推荐(0) 编辑

poj-1195 Mobile phones *
摘要:/* 难得的1A题~~ 标准的树状数组 * 1195.cpp * 注意坐标从0开始, 应处理成从1开始 * Created on: 2011-7-11 * Author: */#include <cstdio>using namespace std;const int MAXS = 1024 + 5;int map[MAXS][MAXS] = {};int c[MAXS][MAXS] = {};int s, x, y , a, l, r, b, t, cmd; //题目中的参数int lowbit(int x){ return x & (-x);}void update(i 阅读全文

posted @ 2011-07-11 15:37 龙豆 阅读(268) 评论(0) 推荐(0) 编辑

poj-2777 Count Color *
摘要://蛮水的线段树//2777//用scanf() 用位运算#include <cstdio>#include <cstring>using namespace std;const int MAXL = 100000 + 5;const int MAXT = 30 + 5;int L, T, O, a, b, c, ans;bool vis[MAXT * 3];char cmd;struct node{ int l, r; int c;};node tree[MAXL * 3];void build(int i, int l, int r){ tree[i].l = l; 阅读全文

posted @ 2011-07-11 15:06 龙豆 阅读(252) 评论(0) 推荐(0) 编辑

poj-2828 Buy Tickets ***
摘要:线段树//2828View Code /* * [这段话转的。。] * 如果我们从头开始一次做的话,无论是用链表还是数组,肯定会超时(链表寻址时间长,数组移位时间长。)。 * 所以要用一个快速的方法直接找到位置。但是换个角 度,如果我们反过来排,那样的话,我们就知道他所在的 * 精确位置!把存储的结构想象成是链表,插队的人插入后,把他所在的位置从链表中屏蔽掉,然后在新的链 表种 * 继续这样存,这样的话我们就得到了我们想要的顺序! * * 按照这种思想,可以用线段树实现。。。 * */#include <cstdio>using namespace std;const int MA 阅读全文

posted @ 2011-07-10 16:41 龙豆 阅读(463) 评论(0) 推荐(0) 编辑

poj-2528 Mayor's posters
摘要:离散化 + 线段树坐标按从小到大排序,再构造映射。。View Code 1 //color=0表示未着色或为杂色 2 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int MAXN = 40000 + 5; 9 const int MAX_R = 10000000 + 5; 10 11 int n, ans; 12 bool vis[MAXN] = {}; 13 struct SData{ //记录数据 14 i 阅读全文

posted @ 2011-07-10 14:06 龙豆 阅读(248) 评论(0) 推荐(0) 编辑

poj-1656 Counting Black
摘要:可用暴力法~下面用二维树状数组:/** 1656.cpp** Created on: 2011-7-6* Author: **/#include <iostream>#include <string>using namespace std;const int MAXN = 100 + 5;const int W = 0, B = 1;int map[MAXN][MAXN] = {};int c[MAXN][MAXN] = {};int t, x, y, l;string cmd;int lowbit(int _x){ return _x & (-_x);}voi 阅读全文

posted @ 2011-07-06 23:09 龙豆 阅读(241) 评论(0) 推荐(0) 编辑

导航

点击右上角即可分享
微信分享提示