bryce1010专题训练——线段树习题汇总
一、区间查询,无单点更新 hdu2795
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25062 Accepted Submission(s): 10274
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.
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.
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 5 2 4 3 3 3
Sample Output
1 2 1 3 -1
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=200010;
int sum[maxn<<2];//数组开四倍
int h,w,n;
//p表示下标
void pushUp(int p)
{
//p表示下标,赋值为两个儿子中的最大值
sum[p]=max(sum[p<<1],sum[p<<1|1]);
}
//建树
void build(int l,int r,int p)
{
if(l==r)//区间长度为0时结束递归
{
sum[p]=w;
return ;
}
//否则的话,分别递归左儿子和右儿子
int mid=(r+l)>>1;
build(l,mid,p<<1);
build(mid+1,r,p<<1|1);
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(l,mid,p<<1,num);
}
else
{
ans=query(mid+1,r,p<<1|1,num);
}
pushUp(p);
return ans;
}
int main()
{
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
memset(sum,0,sizeof(sum));
if(h>n)h=n;
int temp;
build(1,h,1);//建树区间为[1,h],下标开始序号为1
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
if(sum[1]<temp)
cout<<"-1"<<endl;
else
cout<<query(1,h,1,temp)<<endl;
}
}
return 0;
}
二、单点更新+区间求和 hdu1166
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 101938 Accepted Submission(s): 43081
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的责骂的.
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条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Sample Output
Case 1: 6 33 59
#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;
}
三、区间查询最值 hdu1754
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 85331 Accepted Submission(s): 32731
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。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
#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|1
int 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;
}
四、求操作步数(逆序数)hdu1394
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21849 Accepted Submission(s): 13059
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.
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
10 1 3 6 9 0 8 5 7 4 2
Sample Output
16
#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 5010
int 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;
}
区间覆盖 区间增加
#include "ctime"
#include "cctype"
#include "cstdio"
#include "cstdlib"
#define rep(i, m, n) for(register int i = (m); i <= (n); ++i)
template <class T>
inline bool readIn(T& x) {
char ch;
int opt = 1;
while(!isdigit((ch = getchar())) && ch != '-' && ch != -1);
if(ch == -1) return false;
if(ch == '-') {
ch = getchar();
opt = -1;
}
for(x = ch - '0'; isdigit((ch = getchar())); x = (x << 1) + (x << 3) + ch - '0');
ungetc(ch, stdin);
x *= opt;
return true;
}
template <class T>
inline void write(T x) {
if (x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
template <class T>
inline bool writeIn(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
write(x);
return true;
}
typedef long long LL;
const int MAXN = 100005;
struct Node;
void modify( Node *nd, int lf, int rg, int L, int R, LL value );
void change( Node *nd, int lf, int rg, int L, int R, LL delta );
struct Node {
LL sum;
int type;
LL delta, value;
Node *ls, *rs;
void update() {
sum = ls -> sum + rs -> sum;
}
void pushdown( int lf, int rg ) {
if( type == 0 ) return;
int mid = (lf + rg) >> 1;
if( type == 1 ) {
change( ls, lf, mid, lf, mid, delta );
change( rs, mid+1, rg, mid+1, rg, delta );
type = 0;
} else {
modify( ls, lf, mid, lf, mid, value );
modify( rs, mid+1, rg, mid+1, rg, value );
type = 0;
}
}
}pool[MAXN << 1], *tail = pool, *root;
int n, m, l, r, x, a[MAXN];
Node *build( int lf, int rg ) {
Node *nd = ++tail;
if( lf == rg ) {
nd -> sum = a[lf];
nd -> type = 0;
} else {
int mid = (lf + rg) >> 1;
nd -> ls = build( lf, mid );
nd -> rs = build( mid+1, rg );
nd -> type = 0;
nd -> update();
}
return nd;
}
void modify( Node *nd, int lf, int rg, int L, int R, LL value ) {
if( L <= lf && rg <= R ) {
nd -> sum = (LL)(rg - lf + 1) * value;
nd -> type = 2;
nd -> value = value;
return;
}
int mid = (lf + rg) >> 1;
nd -> pushdown(lf,rg);
if( L <= mid ) modify( nd -> ls, lf, mid, L, R, value );
if( R > mid ) modify( nd -> rs, mid+1, rg, L, R, value );
nd -> update();
}
void change( Node *nd, int lf, int rg, int L, int R, LL delta ) {
if( L <= lf && rg <= R ) {
nd -> sum += (LL)(rg - lf + 1) * delta;
if( nd -> type == 0 ) {
nd -> type = 1;
nd -> delta = delta;
} else if( nd -> type == 1 )
nd -> delta += delta;
else if( nd -> type == 2 )
nd -> value += delta;
return;
}
int mid = (lf + rg) >> 1;
nd -> pushdown(lf,rg);
if( L <= mid ) change( nd -> ls, lf, mid, L, R, delta );
if( R > mid ) change( nd -> rs, mid+1, rg, L, R, delta );
nd -> update();
}
LL query( Node *nd, int lf, int rg, int L, int R ) {
if( L <= lf && rg <= R )
return nd -> sum;
int mid = (lf + rg) >> 1;
nd -> pushdown(lf, rg);
LL rt = 0;
if( L <= mid ) rt += query( nd -> ls, lf, mid, L, R );
if( R > mid ) rt += query( nd -> rs, mid+1, rg, L, R );
return rt;
}
int main() {
freopen("setmod.in", "r", stdin);
freopen("setmod.out", "w", stdout);
readIn(n);readIn(m);
rep(i, 1, n) readIn(a[i]);
root = build(1, n);
while( m-- ) {
static char s;
while((s = (char) getchar()) == ' ' || s == '\n');
if(s == 'q') {
readIn(l), readIn(r);
writeIn(query(root, 1, n, l, r));
putchar(10);
} else {
readIn(l);readIn(r);readIn(x);
if(s == 'c') change(root, 1, n, l, r, x);
else modify(root, 1, n, l, r, x);
}
}
}
五、区间更新+区间求和
(增加延迟标记以及pushdown操作)
http://poj.org/problem?id=3468
#include <iostream>
#include <stdio.h>
#define ll long long
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
const int MAXN = 1e5 + 10;
ll sum[MAXN << 2];
ll add[MAXN << 2];
void push_up(int rt){//向上更新
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void push_down(int rt, int m){
if(add[rt]){//若有标记,则将标记向下移动一层
add[rt << 1] += add[rt];
add[rt << 1 | 1] += add[rt];
sum[rt << 1] += (m - (m >> 1)) * add[rt];
sum[rt << 1 | 1] += (m >> 1) * add[rt];
add[rt] = 0;//取消本层标记
}
}
void build(int l, int r, int rt){//建树
add[rt] = 0;
if(l == r){
scanf("%lld", &sum[rt]);
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
push_up(rt);//向上更新
}
void update(int L, int R, ll key, int l, int r, int rt){//区间更新
if(L <= l && R >= r){
sum[rt] += (r - l + 1) * key;
add[rt] += key;
return;
}
push_down(rt, r - l + 1);//向下更新
int mid = (l + r) >> 1;
if(L <= mid) update(L, R, key, lson);
if(R > mid) update(L, R, key, rson);
push_up(rt);//向上更新
}
ll query(int L, int R, int l, int r, int rt){//区间求和
if(L <= l && R >= r) return sum[rt];
push_down(rt, r - l + 1);//向下更新
int mid = (l + r) >> 1;
ll ans = 0;
if(L <= mid) ans += query(L, R, lson);
if(R > mid) ans += query(L, R, rson);
return ans;
}
int main(void){
int n, m;
scanf("%d%d", &n, &m);
build(1, n, 1);
while(m--){
char str[3];
int x, y;
ll z;
scanf("%s", str);
if(str[0] == 'C'){
scanf("%d%d%lld", &x, &y, &z);
update(x, y, z, 1, n, 1);
}else{
scanf("%d%d", &x, &y);
printf("%lld\n", query(x, y, 1, n, 1));
}
}
}
六、矩形面积并
http://acm.hdu.edu.cn/showproblem.php?pid=1255【HDU1255】
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
const int MAXN=1e5+10;
double y[MAXN<<2];
struct Line
{
double x;//记录线段位置
double y_up,y_down;
int flag;//左线段为1,右线段为-1
}line[MAXN<<2];
struct Node
{
double l,r,x;//l维护区间,r维护右区间,x记录上一个x的位置,用于求面积
bool flag;//标记只有一个区间的节点,将连续的区间离散化为节点
int cover;//记录覆盖的线段数
}tree[MAXN<<2];
void pushup(int rt)
{
tree[rt].l=tree[rt>>1].l;
tree[rt].r=tree[rt>>1|1].r;
}
void build(int l,int r,int rt)
{
tree[rt].l=y[l];
tree[rt].r=y[r];
tree[rt].x=-1;//前一个位置
tree[rt].flag=false;//不是只有一个区间
tree[rt].cover=0;
if(l+1==r)//因为是区间是连续数
{
tree[rt].flag=true;//叶子节点
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);//区间是连续的,m不需要+1
}
//flag表示左边线段还是右边线段,碰到左边线段+1,碰到右边线段-1
double query(double l,double r,int rt,double x,int flag)
{
//如果查找区间不在范围内
if(l>=tree[rt].r||r<=tree[rt].l)
{
return 0;
}
//如果是只有一个区间的节点,叶子节点
if(tree[rt].flag)
{
//观察当前范围是否有覆盖区间
if(tree[rt].cover>1)
{
//计算面积
double pre=tree[rt].x;
double ans=(x-pre)*(tree[rt].r-tree[rt].l);
tree[rt].x=x;//更新这个节点,方便下一次计算
tree[rt].cover+=flag;
return ans;
}
else
{
tree[rt].x=x;
tree[rt].cover+=flag;
return 0;
}
}
double ans1,ans2;
ans1=query(l,r,rt<<1,x,flag);
ans2=query(l,r,rt<<1|1,x,flag);
return ans1+ans2;
}
bool cmp(Line l1,Line l2)
{
return l1.x<l2.x;
}
int main()
{
int t,n;
double x1,y1,x2,y2;
cin>>t;
while(t--)
{
cin>>n;
int cnt=-1;
for(int i=0;i<n;i++)
{
cin>>x1>>y1>>x2>>y2;
y[++cnt]=y1;
line[cnt].x=x1;
line[cnt].y_down=y1;
line[cnt].y_up=y2;
line[cnt].flag=1;
y[++cnt]=y2;
line[cnt].x=x2;
line[cnt].y_down=y1;
line[cnt].y_up=y2;
line[cnt].flag=-1;
}
//将所有高度排序
sort(y,y+cnt+1);
//将所有线段排序
sort(line,line+cnt+1,cmp);
build(0,cnt,1);
//扫描线扫描所有的线段
double area=0;
for(int i=0;i<=cnt;i++)
{
area+=query(line[i].y_down,line[i].y_up,1,line[i].x,line[i].flag);
}
printf("%.2f\n",area);
}
return 0;
}