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

NEUOJ 1117
摘要:View Code 1 //方法一:用数组模拟队列 2 #include <iostream> 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<algorithm> 7 using namespace std; 8 int value[1000010]; 9 int Qu[6000010];//单调队列 10 int head, tail;//队列的头和尾 11 void insertBig(int index)//单调减队列,维护最大值 阅读全文

posted @ 2011-12-05 12:46 lonelycatcher 阅读(515) 评论(0) 推荐(0)

UESTC 1598 兰斯的后宫计划
摘要:这道题可以用线段树来求解,当然,如果给的数据没有负值,同样可以用二分的思想去做,效率和线段树差不多View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define max(a,b) a>b?a:b 5 struct node 6 { 7 int left,right,maxSum; 8 }; 9 node Tree[400100];10 int sum[100010],value[100010];11 void build_tree(int k,int l 阅读全文

posted @ 2011-11-28 09:22 lonelycatcher 阅读(409) 评论(0) 推荐(0)

POJ 1195 mobile phones 二维树状数组
摘要:题意很简单,就是求一定范围内子矩阵的和,数据量很大,用暴力的话一定会超时,这道题用二维的树状数组做比较简单。。。#include<stdio.h>#include<string.h>#include <iostream>using namespace std;#define MAX 1030int tree[MAX][MAX];int S,ans;int lowbit(int x){return x&(-x);}void add(int x,int y,int num){ for(int i=x;i<=S;i+=lowbit(i)) { for 阅读全文

posted @ 2011-07-23 13:09 lonelycatcher 阅读(212) 评论(0) 推荐(0)

POJ 2481 cows 树状数组
摘要:题意很简单,需要注意的一点是当两头牛的range完全相同的时候后面的牛的Strongnumber==前面牛的Strongnumber,效率不是很高,2200+ms#include<stdio.h>#include<algorithm>#include<string.h>#include <iostream>using namespace std;typedef struct{ int x; int y; int index;}cow;cow cows[100010];int N;int tree[100010];int ans[100010];b 阅读全文

posted @ 2011-07-23 09:46 lonelycatcher 阅读(361) 评论(0) 推荐(0)

POJ 2352 starts 树状数组
摘要:#include <iostream>#include<stdio.h>#include<string.h>using namespace std;int N;int x,y;int tree[33000];int level[16000];int lowbit(int t){return t&(-t);}void update(int index){ while(index<33000) { tree[index]++; index+=lowbit(index); }}int getSum(int index){ int sum=0; whi 阅读全文

posted @ 2011-07-22 14:35 lonelycatcher 阅读(185) 评论(0) 推荐(0)

HDU Just a hook
摘要:更新一段区间内所有点的值#include<stdio.h>int N,Q;const int MAX=100000;struct line{ int left; int right; int Value;};line lines[MAX<<2];void buildTree(int le,int rig,int root){ lines[root].left=le; lines[root].right=rig; lines[root].Value=1; if(le==rig) { return ; } int mid=(lines[root].left+lines[ro 阅读全文

posted @ 2011-06-02 19:13 lonelycatcher 阅读(224) 评论(0) 推荐(0)

HDU 1566 color the ball
摘要:这道线段树的题目纠结了好长时间,本来的想法真是太天真了,向线段树里面插入一段线段,当时错误的想法是他的所有的子树同样要更新,这样效率自然就下降了,TLE了很长时间,后来才想出来计算(i,i)被涂色的次数,只要插入的线段包含这个区间,就把它加起来,最后求和。这道题用树状数组做挺简单,但是还没学过树状数组是神马东西,以后再学了#include<stdio.h>int N;const int MAX=100010;struct line{ int left; int right; int number;};line lines[MAX<<2];void buildTree(i 阅读全文

posted @ 2011-06-02 11:11 lonelycatcher 阅读(402) 评论(0) 推荐(0)

HDU 1166 敌兵布阵
摘要:#include<stdio.h>int N;const int MAX=50010;int numbers[MAX];char s[7];struct line{ int left; int right; int maxNum;};line lines[MAX<<2];void buildTree(int le,int ri,int root){ lines[root].left=le; lines[root].right=ri; if(le==ri) { lines[root].maxNum=numbers[le]; return ; } int mid=(le+r 阅读全文

posted @ 2011-06-01 20:10 lonelycatcher 阅读(244) 评论(0) 推荐(0)

POJ 3264线段树的应用
摘要:#include<stdio.h>#include<string.h>struct node{ int max; int min; int l; int r;};node tree[200000];int h[50005];int max,min;int MAX(int a,int b){ if(a>b) return a; return b;}int MIN(int a,int b){ if(a>b) {return b;} return a;}void build(int l,int r,int root){ tree[root].l=l; tree[r 阅读全文

posted @ 2011-05-22 22:11 lonelycatcher 阅读(224) 评论(0) 推荐(0)

HDU 1754 I hate it 线段树的应用
摘要:#include<stdio.h>#define N_MAX 200010typedef struct { int left,right; int maxScore;}seg_tree;seg_tree trees[N_MAX*4 +1];int score[N_MAX+10];int get_max(int x,int y){ return x>y?x:y;}void build_tree(int root,int left,int right){ trees[root].left = left; trees[root].right = right; if(left == 阅读全文

posted @ 2011-05-22 22:01 lonelycatcher 阅读(175) 评论(0) 推荐(0)

导航