线段树专题训练

Minimum Inversion Number

HDU - 1394

题意:找最小逆序数。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,m,rt<<1
 4 #define rson m+1,r,rt<<1|1
 5
 6 const int maxn=5010;
 7 int sum[maxn<<2];
 8
 9 void pushup(int rt){
10     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
11 }
12 void build(int l,int r,int rt){
13     if(l==r){
14         sum[rt]=1;
15         return ;
16     }
17     int m=(l+r)>>1;
18     build(lson);
19     build(rson);
20     pushup(rt);
21 }
22
23 void update(int p,int l,int r,int rt){
24     if(l==r){
25         sum[rt]=0;
26         return;
27     }
28     int m=(l+r)>>1;
29     if(p<=m) update(p,lson);
30     else update(p,rson);
31     pushup(rt);
32 }
33 int query(int L,int R,int l,int r,int rt){
34     if(L<=l&&r<=R) {
35         return sum[rt];
36     }
37     int m=(l+r)>>1;
38     int ans=0;
39     if(L<=m) ans+=query(L,R,lson);
40     if(R>m) ans+=query(L,R,rson);
41     return ans;
42 }
43 int a[maxn];
44 int n;
45
46 int main(){
47     while(scanf("%d",&n)!=EOF){
48         build(1,n,1);
49         int ans=0;
50         for(int i=1;i<=n;i++){
51             scanf("%d",&a[i]);
52             a[i]++;
53             update(a[i],1,n,1);
54             ans+=query(1,a[i],1,n,1);
55         }
56         //printf("逆序数==%d\n",ans);
57         int temp=ans;
58         for(int i=1;i<n;i++){
59             temp=temp-(a[i]-1)+(n-a[i]);
60             ans=min(ans,temp);
61         }
62         printf("%d\n",ans);
63     }
64 }

Billboard

HDU - 2795

题意:在板子上贴海报,尽量从左下方开始贴,问最低贴在哪层。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,m,rt<<1
 4 #define rson m+1,r,rt<<1|1
 5 const int maxn=200010;
 6 int maxx[maxn<<2];
 7 int h,w,m;
 8 void pushup(int rt){
 9     maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
10 }
11
12 void build(int l,int r,int rt){
13     if(l==r){
14         maxx[rt]=w;
15         return ;
16     }
17     int m=(l+r)>>1;
18     build(lson);
19     build(rson);
20     pushup(rt);
21 }
22
23 int query(int x,int l,int r,int rt){
24     if(l==r){
25         maxx[rt]-=x;
26         return l;
27     }
28     int m=(l+r)>>1;
29     int ans;
30     if(maxx[rt<<1]>=x) ans=query(x,lson);
31     else  ans=query(x,rson);
32     pushup(rt);
33
34     return ans;
35 }
36
37 int main(){
38     while(scanf("%d%d%d",&h,&w,&m)!=EOF){
39         int n=min(h,m);
40         build(1,n,1);
41         while(m--){
42             int x;
43             scanf("%d",&x);
44             int ans;
45             if(maxx[1]<x) ans=-1;
46             else ans=query(x,1,n,1);
47             printf("%d\n",ans);
48         }
49     }
50     return 0;
51 }

时间: 2024-08-03 07:58:23

线段树专题训练的相关文章

vj线段树专题

vj线段树专题题解 单点更新模板 void build(int x,int l,int r){//sum[x]控制l-r区域 if(l==r){Sum[x]=num[l];return ;} int mid=l+((r-l)>>1); build(x<<1,l,mid); build(x<<1|1,mid+1,r); Sum[x]=Sum[x<<1]+Sum[x<<1|1]; } void add(int a,int b,int l,int r,

[kuangbin]带你飞之&#39;线段树&#39;专题(未完成)

// 带飞网址 https://vjudge.net/article/187 专题七 线段树 HDU 1166 敌兵布阵HDU 1754 I Hate It√ POJ 3468 A Simple Problem with IntegersPOJ 2528 Mayor's postersHDU 1698 Just a HookZOJ 1610 Count the ColorsPOJ 3264 Balanced LineupHDU 4027 Can you answer these queries?

线段树专题(一)

Acmer可怜啊,根本没有休息,昨天才刚刚完成了矩阵专题,今天又要开线段树专题了.唉,等我以后月薪15K的时候,我要好好享受人生......呃,扯远了.线段树是一个非常重要的数据结构,以前就学习过,但是没有系统的刷过难题,这次我决定将kuangbin先生的专题和NotOnlySuccess大神的专题一起刷掉.因为题目多又难,所以分成几个部分(最多三个把). 对于线段树的话,主要是理解它的树形结构吧,推荐和树状数组一起学习.似乎树状数组就是线段树的退化,树状数组节约了差不多一半的空间,但是必须满足

[题解]线段树专题测试2017.1.21

很单纯的一道线段树题.稍微改一下pushDown()就行了. Code(线段树模板竟然没超100行) 1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<cctype> 8 #include<queue> 9

非递归线段树专题

学习了自底向上的非递归线段树,深感精妙!! 大牛的博客:http://blog.csdn.net/zearot/article/details/48299459 张坤玮---统计的力量 The Union of k-Segments CodeForces - 612D 题意:求被覆盖k次及以上的点或者线段. 看到别人直接用扫描线写的,更方便一些. 不过拿来练习线段树也不错. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const in

线段树专题 POJ3468 A Simple Problem with Integers

题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long 表示,假设是在hust上交结果要用%I64d.poj的话则用%lld 代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algor

线段树专题(持续更新中...)

单点更新:最最基础的线段树,只更新叶子节点,然后把信息用PushUP(int r)这个函数更新上来 hdu1166 敌兵布阵 线段树功能:update:单点增减 query:区间求和 1 #include <iostream> 2 #include<cstring> 3 #include<stdio.h> 4 using namespace std; 5 #define M 50005 6 int sum[M<<2]; 7 void pushup(int r

线段树专题(不定期更新)

1.hdu 1166 敌兵布阵(★☆☆☆☆) 题意:有n个营地,每个营地初始各有若干人,每次询问[l,r]营地的总人数,或者对某个营地加上或减去若干人数. 思路:线段树单点更新,区间查询 1 //线段树单点更新,区间查询 2 #include<iostream> 3 using namespace std; 4 const int maxn = 50010; 5 int N; 6 int barracks[maxn]; 7 int Tree[4 * maxn]; 8 void Create(i

Kuangbin 带你飞-线段树专题 题解

HDU 1166 敌兵布阵 单调更新区间查询和 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #inc