随笔分类 -  【线段树】

【SPOJ】1557 Can you answer these queries II
摘要:询问区间最大连续和,相同的只能算一次。可以不取,即为0线段树在线做不到,只好离线搞。一般最大连续和,都是以一个点为起点,往左或往右。其实,还可以以一个点为起点,往上最大是多少。用pos标记一个数之前更新到的位置。比如:-3 2 -3 1用线段树成段覆盖:(越下方越早更新到)1 1 1 1 -3 -32 2-3nowsum表示当前深度积累的总和。nowup表示当前深度从底往上的最大值。totsum,totup同理,表示的是以某个叶子为起点对应的值。加上读入优化,快了2s…… 1 #include<cstdio> 2 #include<cstring> 3 #include 阅读全文
posted @ 2012-08-30 18:08 DrunBee 阅读(715) 评论(0) 推荐(0)
【SPOJ】2916 Can you answer these queries V
摘要:操作:给出x属于[x1,y1],y属于[x2,y2],求[x,y]的最大连续和。将区间分3段考虑,答案可能由某一段的最大连续和,或者某一段往左的最大连续和,某一段往右的最大连续和组合而来。需要特判的是,区间可能存在包含关系。 1 #include<cstdio> 2 #define MAX(a,b) ((a)>(b)?(a):(b)) 3 #define MAXN 10010 4 #define oo 987654321 5 struct node { 6 int left, right, sum, val; 7 void Init() { 8 ... 阅读全文
posted @ 2012-08-29 12:28 DrunBee 阅读(343) 评论(0) 推荐(0)
【SPOJ】2713 Can you answer these queries IV
摘要:操作:1,把[x,y]每个数k变成sqrt(k),向下取整。2,查询区间的和。就算10^18,sqrt后减少的很快。当一个数为0或1时,它不会再变化了,把不会变化的区间标记,不再访问。所以暴力更新总的可以视为O(nlogn)的。 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #define EPS 1e-8 5 #define MAXN 100010 6 typedef long long LL; 7 using namespace std; 8 struct node { 9 LL 阅读全文
posted @ 2012-08-29 09:53 DrunBee 阅读(267) 评论(0) 推荐(0)
【SPOJ】1043 Can you answer these queries III
摘要:操作:1,更新某个位置的值。2,查询区间最大连续和。更新位置然后PushUp。查询同【SPOJ】1043 Can you answer these queries I。 1 #include<cstdio> 2 #define MAX(a,b) ((a)>(b)?(a):(b)) 3 #define MAXN 50010 4 #define oo 1000000000 5 struct node { 6 int left, right, sum, val; 7 void Init() { 8 sum = 0; 9 left = right = v... 阅读全文
posted @ 2012-08-29 00:16 DrunBee 阅读(343) 评论(0) 推荐(0)
【SPOJ】1043 Can you answer these queries I
摘要:操作:查询区间最大连续和。每个节点保存当前区间往左,往右的最大和。往左最大连续和=max(左区间往左最大连续和,左区间的和+右区间往左最大连续和)。区间最大值=max(左,右区间最大值,左区间右连续+右区间左连续)。返回答案时,区间需要不断的合并。 1 #include<cstdio> 2 #define MAX(a,b) ((a)>(b)?(a):(b)) 3 #define MAXN 50010 4 #define oo 1000000000 5 struct node { 6 int left, right, sum, val; 7 void Init() { 8 .. 阅读全文
posted @ 2012-08-29 00:08 DrunBee 阅读(391) 评论(0) 推荐(0)
【HDU】1754 I Hate It
摘要:线段树: 1 #include<cstdio> 2 #define INF 987654321 3 #define MAXN 200010 4 int tree[MAXN << 2]; 5 inline int MAX(int x, int y) { 6 return x > y ? x : y; 7 } 8 inline void PushUp(int rt) { 9 tree[rt] = MAX(tree[rt << 1], tree[rt << 1 | 1]);10 }11 void Build(int L, int R, int r 阅读全文
posted @ 2012-08-13 01:11 DrunBee 阅读(737) 评论(0) 推荐(0)
【总结】线段树
摘要:1、单点更新。【HDU】1166 敌兵布阵【HDU】1754 I Hate It【POJ】3264 Balanced Lineup【HDU】3074 Multiply game【POJ】2352 Stars【HDU】1394 Minimum Inversion Number【HDU】2795 Billboard【HDU】4046 Panda【POJ】2828 Buy Tickets【ZOJ】3279 Ants【FOJ】1962 新击鼓传花游戏【FOJ】1921 栀子花开【Codeforces】159C String Manipulation 1.0【POJ】2886 Who Gets the 阅读全文
posted @ 2012-06-30 20:20 DrunBee 阅读(1579) 评论(1) 推荐(0)
【UVa】11992 Fast Matrix Operations
摘要:1 #include<cstdio> 2 #define INF 0x7FFFFFFF 3 #define MAXN 1000010 4 struct node 5 { 6 int cnt,son[4],kind[4]; 7 int big,small,sum,add,cover; 8 }; 9 node tree[MAXN<<2]; 10 int size,SUM,BIG,SMALL; 11 inline int MAX(int x,int y) 12 { 13 return x>y?x:y; 14 } 15 inline int MIN(int ... 阅读全文
posted @ 2012-06-30 09:59 DrunBee 阅读(863) 评论(0) 推荐(0)
【POJ】2155 Matrix
摘要:1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 1010 4 int ans,n,tree[MAXN<<2][MAXN<<2]; 5 void SubBuild(int t,int L,int R,int rt) 6 { 7 tree[t][rt]=0; 8 if(L!=R) 9 { 10 int mid=(L+R)>>1; 11 SubBuild(t,L,mid,rt<<1); 12 SubBuild(t,mid+1,R,rt<<1|1); ... 阅读全文
posted @ 2012-06-28 20:39 DrunBee 阅读(254) 评论(0) 推荐(0)
【POJ】2019 Cornfields
摘要:1 #include<cstdio> 2 #define MAXN 300 3 struct node 4 { 5 int big[MAXN<<2],small[MAXN<<2]; 6 }; 7 node tree[MAXN<<2]; 8 int n,b; 9 inline int MAX(int x,int y) 10 { 11 return x>y?x:y; 12 } 13 inline int MIN(int x,int y) 14 { 15 return x>y?y:x; 16 } 17 void SubBuild(int t 阅读全文
posted @ 2012-06-28 11:48 DrunBee 阅读(355) 评论(0) 推荐(0)
【HDU】1823 Luck and Love
摘要:1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define MAXN 110 5 #define MAXM 1010 6 struct node 7 { 8 int big[MAXM<<2]; 9 }; 10 node tree[MAXN<<2]; 11 inline int MAX(int x,int y) 12 { 13 return x>y?x:y; 14 } 15 void SubBuild(int t,int L,int R,int rt) 1 阅读全文
posted @ 2012-06-27 22:47 DrunBee 阅读(399) 评论(0) 推荐(0)
【HDU】3333 Turing Tree
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 typedef __int64 LL; 6 #define MAXN 30010 7 #define MAXM 100010 8 int a[MAXN],b[MAXN],vis[MAXN]; 9 LL ans[MAXM],tree[MAXN<<2];10 struct node11 {12 int left,right,pos;13 friend bool operator&l 阅读全文
posted @ 2012-06-27 11:14 DrunBee 阅读(268) 评论(0) 推荐(0)
【HDU】3954 Level up
摘要:1 #include<cstdio> 2 #define MAXN 10010 3 #define MAXM 11 4 #define INF 2147483647 5 int n,k,need[MAXM]; 6 struct node 7 { 8 int big,lazy; 9 }; 10 node tree[MAXM][MAXN<<2]; 11 inline int MAX(int x,int y) 12 { 13 return x>y?x:y; 14 } 15 inline void PushUp(int t,int rt) 16 { 17 t... 阅读全文
posted @ 2012-06-26 13:16 DrunBee 阅读(427) 评论(0) 推荐(0)
【UVa】11983 Weird Advertisement
摘要:1 #include<cstdio> 2 #include<algorithm> 3 typedef long long LL; 4 #define MAXN 30010 5 using namespace std; 6 int n,k; 7 struct Seg 8 { 9 int left,right,high,flag; 10 friend bool operator<(Seg a,Seg b) 11 { 12 if(a.high==b.high) 13 return a.flag<b.flag; 14 ... 阅读全文
posted @ 2012-06-24 10:08 DrunBee 阅读(459) 评论(0) 推荐(0)
【HDU】2871 Memory Control
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define MAXN 50010 6 struct node 7 { 8 int left,right,sum,lazy,s,e,cnt,cover; 9 }; 10 node tree[MAXN<<2]; 11 inline void PushUp(int mid,int L,int R,int rt) 12 { 13 tree[rt].left=tree[rt<& 阅读全文
posted @ 2012-06-22 16:05 DrunBee 阅读(714) 评论(0) 推荐(0)
【FOJ】1608 Huge Mission
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define MAXN 50010 6 struct time 7 { 8 int s,t,ef; 9 friend bool operator<(time a,time b)10 {11 return a.ef>b.ef;12 }13 };14 struct node15 {16 int sum,lazy;17 };18 time p[MAXN*10];19 no... 阅读全文
posted @ 2012-06-21 13:55 DrunBee 阅读(481) 评论(1) 推荐(0)
【FOJ】2022 车站
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MAXN 500010 5 using namespace std; 6 int x[MAXN]; 7 bool vis[MAXN]; 8 struct cmd 9 { 10 char s[6]; 11 int val; 12 }; 13 struct node 14 { 15 int left,right,val; 16 }; 17 node tree[MAXN<<2]; 18 cmd p[MAXN]; 阅读全文
posted @ 2012-06-21 13:24 DrunBee 阅读(403) 评论(0) 推荐(0)
【FOJ】1921 栀子花开
摘要:1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define MAXN 10010 5 int tree[MAXN<<2],ca=1; 6 inline void PushUp(int rt) 7 { 8 tree[rt]=min(tree[rt<<1],tree[rt<<1|1]); 9 }10 void Build(int L,int R,int rt)11 {12 if(L==R)13 scanf("%d",&tre 阅读全文
posted @ 2012-06-21 11:02 DrunBee 阅读(361) 评论(1) 推荐(0)
【POJ】1436 Horizontally Visible Segments
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<algorithm> 5 #include<set> 6 using namespace std; 7 #define MAXN 16010 8 #define INF 123456789 9 struct Seg 10 { 11 int y1,y2,x; 12 friend bool operator<(Seg a,Seg b) 13 { 14 return a.x<b.x; 15 } 阅读全文
posted @ 2012-06-18 21:58 DrunBee 阅读(380) 评论(0) 推荐(0)
【POJ】2886 Who Gets the Most Candies?
摘要:1 #include<cstdio> 2 #define MAXN 500010 3 int n,k,ans,cnt; 4 int prime[]={2,3,5,7,11,13,17}; 5 struct node 6 { 7 char name[12]; 8 int val; 9 }; 10 node p[MAXN]; 11 int tree[MAXN<<2]; 12 void AntiPrime(int res,int count,int index,int limit) 13 { 14 if(res<=n) 15 { 16 ... 阅读全文
posted @ 2012-06-18 11:48 DrunBee 阅读(446) 评论(0) 推荐(0)