线段树入门
来源:互联网 发布:淘宝大表情 编辑:程序博客网 时间:2024/05/29 11:50
hdu 2795 区间查询
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20597 Accepted Submission(s): 8532
Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
Input
There are multiple cases (no more than 40 cases).
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
Sample Input
3 5 524333
Sample Output
1213-1
题意:
用一个 h*W大小的板子来贴信息,每一条信息都是高度为 1 的,宽度是题目给出的大小的信息的条子
每次贴尽量贴在左上角,求每一条信息贴在多少行,如果贴不了就输出-1
题解:
建立线段树,线段树的最高顶点是表示所有行里面最大的宽度,如果小于当前输入的就没有解
其他的情况请看代码
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 200010#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1int sum[MAXN<<2];int h,w,n;void pushUp(int p){ sum[p]=max(sum[p<<1],sum[p<<1|1]);}void build(int l,int r,int p){ if(l==r){ sum[p]=w; return ; } int mid=(l+r)>>1; build(lson); build(rson); pushUp(p);}int query(int l,int r,int p,int num){ if(l==r){ sum[p]-=num; return l; } int ans; int mid=(r+l)>>1;//下面要注意,先保存ans,然后更新值后再返回 if(sum[p<<1]>=num) ans=query(lson,num); else ans=query(rson,num); pushUp(p); return ans;}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d%d",&h,&w,&n)!=EOF) { int temp; if(h>n) h=n; build(1,h,1); for(int i=0;i<n;i++){ scanf("%d",&temp); if(sum[1]<temp) printf("-1\n"); else printf("%d\n",query(1,h,1,temp)); } } return 0;}
hdu 1166 单点更新
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 81917 Accepted Submission(s): 34563
Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End
Sample Output
Case 1:63359
技巧:
删除和增加兵力的时候只是给函数传入数值的 正和负 的区别
可以再建立线段树的时候输入,因为线段树最底层的叶子只有n个
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 50010#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1#define mem(a) memset(a,0,sizeof(a[0]))int sum[MAXN<<2];void pushUp(int p){ sum[p]=sum[p<<1]+sum[p<<1|1];}void build(int l,int r,int p){ if(l==r){ scanf("%d",&sum[p]); return; } int mid=(l+r)>>1; build(lson); build(rson); pushUp(p);}int query(int l,int r,int p,int i,int j){ if(l>=i&&r<=j){ return sum[p]; } int ans=0; int mid=(l+r)>>1; if(mid<j) ans+=query(rson,i,j); if(mid>=i) ans+=query(lson,i,j); return ans;}void update(int l,int r,int p,int i,int j){ if(l==r){ sum[p]+=j; return ; } int mid=(l+r)>>1; if(mid>=i) update(lson,i,j); else update(rson,i,j); pushUp(p);}int main(){ int n,T; int cases=1; char str[10]; //freopen("in.txt","r",stdin); scanf("%d",&T); while(T--) { mem(sum); scanf("%d",&n); printf("Case %d:\n",cases++); int i,j; build(1,n,1); while(scanf("%s",str)&&strcmp(str,"End")) { scanf("%d%d",&i,&j); if(strcmp(str,"Query")==0) printf("%d\n",query(1,n,1,i,j)); else if(strcmp(str,"Add")==0) update(1,n,1,i,j); else if(strcmp(str,"Sub")==0) update(1,n,1,i,-j); } } return 0;}
hdu 1754 区间查询
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 68815 Accepted Submission(s): 26710
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659HintHuge input,the C function scanf() will work better than cin
注意:
在查询的时候搞清楚区间的关系
在查询的时候搞清楚是不是if else 关系,还是并列的 if if 关系#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 200010#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1int sum[MAXN<<2];void pushUp(int p){ sum[p]=max(sum[p<<1],sum[p<<1|1]);}void build(int l,int r,int p){ if(l==r){ scanf("%d",&sum[p]); return ; } int mid=(l+r)>>1; build(lson); build(rson); pushUp(p);}int query(int l,int r,int p,int a,int b){ if(l>=a&&b>=r) return sum[p]; int ans=0; int mid=(l+r)>>1; if(mid>=a) ans=max(ans,query(lson,a,b)); if(mid<b) ans=max(ans,query(rson,a,b)); return ans;}void update(int l,int r,int p,int a,int b){ if(l==r){ sum[p]=b; return ; } int mid=(l+r)>>1; if(a<=mid) update(lson,a,b); else update(rson,a,b); pushUp(p);}int main(){ int n,m; char ch[5]; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { int a,b; build(1,n,1); while(m--) { scanf("%s%d%d",ch,&a,&b); if(ch[0]=='Q') printf("%d\n",query(1,n,1,a,b)); else update(1,n,1,a,b); } } return 0;}
hdu 1394 求解逆序数
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17623 Accepted Submission(s): 10687
Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
Output
For each case, output the minimum inversion number on a single line.
Sample Input
101 3 6 9 0 8 5 7 4 2
Sample Output
16
题意:
一个由0..n-1组成的序列,每次可以把队首的元素移到队尾,求形成的n个序列中最小逆序对数目
题解:
线段树:
线段树的初始化为0,一边输入一边插入数值,每一次插入查询一下线段树中比当前值大的数有多少个(即逆序数)
输入完了就得到了逆序数,然后每次移动一个数字依靠下面的这个规律即可
在这一串数字中: 比 X 大的有 n-x-1 个
比 X 小的有 X个 (因为数字是从 0 到 X -1 )
那么当X 为队首元素移到队尾时
在队首的时候没有逆序数,在队尾的逆序数为 n-x-1
在队首的时候为后面序列提供了X逆序数,比如说,4 3 2 1 0,这个序列因为4而多了4个逆序数值
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1#define MAXN 5010int n;int sum[MAXN<<2];int a[MAXN];void build(int l,int r,int p){ sum[p]=0; if(l==r) return ; int mid=(l+r)>>1; build(lson); build(rson);}void update(int m,int l,int r,int p){ if(l==r){ sum[p]++; return; } int mid=(l+r)>>1; if(m<=mid) update(m,lson); else update(m,rson); sum[p]=sum[p<<1]+sum[p<<1|1];}int query(int L,int R,int l,int r,int p){ if (l>=L&&r<=R) return sum[p]; int mid=(l+r)>>1; int ans=0; if (mid>=L) ans+=query(L,R,lson); if (mid<R) ans+=query(L,R,rson); return ans;}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { build(0,n-1,1); int cnt=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); cnt+=query(a[i],n-1,0,n-1,1); update(a[i],0,n-1,1); } int ans=cnt; for(int i=0;i<n;i++){ cnt+=n-a[i]-a[i]-1; ans=ans<cnt?ans:cnt; } printf("%d\n",ans); } return 0;}
0 0
- 【线段树】线段树入门
- [线段树]线段树 入门
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 线段树入门之入门
- 线段树学习入门
- 线段树 入门
- HDU1754 线段树入门
- HDU1556 线段树入门
- HDU1166 线段树入门
- 线段树入门【转】
- 线段树入门
- 线段树入门
- 线段树入门( 转 )
- 线段树入门
- 线段树入门知识
- 异步处理(Handler&Message)的使用
- decimalformat
- C++实验(二)
- Windows10 和 Ubuntu 16.04 双系统 系统时间不统一解决
- ServiceManager源码分析
- 线段树入门
- android studio中断开SVN连接,并彻底清理项目中的.svn文件
- win8 win10 安装msi 提示2502、2503的错误代码
- FADT.sci_interrupt 的产生
- 网页微信扫码支付
- Linux环境下搭建php开发环境的操作步骤
- 行为型模式--策略模式
- JavaScript基础
- linux 中动态库和静态库的创建和使用