线段树(单点修改,区间求和,区间最大)
(一)线段树
Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Input
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
Output
Sample Input
5 1 2 1 0
Sample Output
2 4 5 3 1
解题思路:题意是已知每个数字前面比她小的数的个数组成pre[]数组,让我们求他们的ans[]顺序。
我们可以用暴力的方法来解决这道题:
思路是从后往前处理pre数组,例如pre[]:0 1 2 1 0
pre[5]=0,表示ans[5]前面比她小的有0个,即他是最小的,因此ans[5]=1;同时ans数组中将不再包括1
pre[4]=1,表示ans[4]前面比他小的有1个,即他是第二小,因此ans[4]=3;同时ans数组不在包括3
。。。
每次都是从剩下的编号里找第pre[i]+1小,就是ans[i]
我们解题用到三个数组,pre,ans,num[]数组记录被处理过的数字,处理过就记为-1,设置num的初始值就为下标值
代码:
#include<iostream> #include<cstdio> using namespace std; const int N=8010; int pre[N],ans[N],num[N];//数组的第0个都不用,从1开始 int main() { int n,i,j; scanf("%d",&n); pre[1]=0; for(i=2;i<=n;i++) scanf("%d",&pre[i]); for(i=1;i<=n;i++) num[i]=i;//给num赋初值 for(i=n;i>=1;i--) { int k=0; for(j=1;j<=n;j++) { if(num[j]!=-1) { k++; if(k==pre[i]+1)//找到第pre[i]+1小的数 { ans[i]=num[j];//用ans记录 num[j]=-1;//num数组标记 break; } } } } for(i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
用暴力如果数量级较大就不太适合了会TLE,那么就引出我们的线段树
我们用线断树实现的是建树与修改和查询。
代码:
#include<iostream> #include<cstdio> using namespace std; const int N=10000; struct{ int l,r,len;//len储存这个区间的数字个数 }tree[4*N]; int pre[N],ans[N]; void BuildTree(int left,int right,int u)//建树 { tree[u].l=left; tree[u].r=right; tree[u].len=right-left+1; if(left==right) { return; } BuildTree(left,(left+right)>>1,u<<1);//递归左子树 BuildTree(((left+right)>>1)+1,right,(u<<1)+1);//递归右子树 } int query(int u,int num)//维护+查询 { tree[u].len--;//该区间长度-1 if(tree[u].l==tree[u].r) return tree[u].l;//找到该点返回他的下标 if(tree[u<<1].len<num)//如果左子树的各数不够,就查询右子树起第num-tree[u<<1].len的数下标 return query((u<<1)+1,num-tree[u<<1].len); if(tree[u<<1].len>=num)//左子树个数足够,就找第num个数 return query(u<<1,num); } int main() { int n,i; scanf("%d",&n); pre[1]=0; for(i=2;i<=n;i++) scanf("%d",&pre[i]); BuildTree(1,n,1);//先建树 for(i=n;i>=1;i--)//从后往前找 ans[i]=query(1,pre[i]+1); for(i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
2.G - A Simple Problem with Integers
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
解题思路:区间修改与求和(模板题)
#include<iostream> #include<cstdio> using namespace std; const int N=1e5+10; long long sum[N<<2],add[N<<2]; void push_up(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } //更新rt的子节点 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;//取消标记 } } #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 void build(int l,int r,int rt) { add[rt]=0; if(l==r) { scanf("%lld",&sum[rt]); return; } int mid=(r+l)>>1; build(lson); build(rson); push_up(rt); } void update(int a,int b,long long c,int l,int r,int rt) { if(a<=l&&b>=r) { sum[rt]+=(r-l+1)*c; add[rt]+=c; return; } push_down(rt,r-l+1); int mid=(l+r)>>1; if(a<=mid) update(a,b,c,lson); if(b>mid) update(a,b,c,rson); push_up(rt);//更新区间和 } long long query(int a,int b,int l,int r,int rt)//区间求和 { if(a<=l&&b>=r) return sum[rt]; push_down(rt,r-l+1); int mid=(l+r)>>1; long long ans=0; if(a<=mid) ans+=query(a,b,lson); if(b>mid) ans+=query(a,b,rson); return ans; } int main() { int n,m; scanf("%d %d",&n,&m); build(1,n,1); while(m--) { char str[2]; int a,b; long long c; scanf("%s",str); if(str[0]=='C') { scanf("%d%d%lld",&a,&b,&c); update(a,b,c,1,n,1); } else { scanf("%d%d",&a,&b); printf("%lld\n",query(a,b,1,n,1)); } } return 0; }
3.H - 敌兵布阵
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
每组数据第一行一个正整数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<iostream> #include<cstdio> using namespace std; const int N=100010; int t,n,w[N]; struct node { int l,r; int sum; }tr[N*4]; void pushup(int u) { tr[u].sum=tr[u*2].sum+tr[u*2+1].sum; } void build(int u,int l,int r) { if(l==r) tr[u]={l,r,w[r]}; else { tr[u]={l,r}; int mid=l+r>>1; build(u*2,l,mid),build(u*2+1,mid+1,r); pushup(u); } } int query(int u,int l,int r) { if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum; int mid=tr[u].l+tr[u].r>>1; int sum=0; if(l<=mid) sum=query(u*2,l,r); if(r>mid) sum+=query(u*2+1,l,r); return sum; } void modify(int u,int x,int v) { if(tr[u].l==tr[u].r) tr[u].sum+=v; else { int mid=(tr[u].l+tr[u].r)>>1; if(x<=mid) modify(u<<1,x,v); else modify(u*2+1,x,v); pushup(u); } } int main() { int i,j; scanf("%d",&t); for(j=1;j<=t;j++) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&w[i]); } char str[10]; int a,b; build(1,1,n); printf("Case %d:\n",j); while(scanf("%s",str)&&str[0]!='E') { scanf("%d%d",&a,&b); if(str[0]=='Q') printf("%d\n",query(1,a,b)); else if(str[0]=='A') modify(1,a,b); else if(str[0]=='S') modify(1,a,-b); } } return 0; }
4.I - Minimum Inversion Number
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.
InputThe 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.
OutputFor 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
解题思路:题意大概是求逆序数的最小值,有n个序列求他们的逆序数的最小值,这n个序列是通过不断使第一个数放到末尾构成的
我们可以从中发现规律:每次将第一个数放到最后,其该序列的逆序数=上一个序列的逆序数+n-2*a[i]-1;因为之前有a[i]个逆序数,当放到后面后原来的逆序数变成了顺序
而原来顺序的变成了逆序,因此需要在原来序列逆序数的基础上+(n-a[i]-1)-a[i],因此就是上面的式子。
之后我们只需求出第一个序列的逆序数即可,然后通过一个循环就可以找出最小的逆序数
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N=5010; struct node { int l,r,sum; }tr[4*N]; void push_up(int m) { tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum; } void buildtree(int t,int l,int r) { tr[t].l=l; tr[t].r=r; tr[t].sum=0; if(l==r) return; int mid=(l+r)>>1; buildtree(t<<1,l,mid); buildtree(t<<1|1,mid+1,r); } int query(int t,int l,int r) { if(tr[t].l>=l&&tr[t].r<=r) return tr[t].sum; int mid=(tr[t].l+tr[t].r)>>1; int ans=0; if(l<=mid) ans+=query(t<<1,l,r); if(r>mid) ans+=query(t<<1|1,l,r); return ans; } void update(int t,int id) { if(tr[t].l==id&&tr[t].r==id) { tr[t].sum=1; return; } int mid=(tr[t].l+tr[t].r)>>1; if(id<=mid) update(t<<1,id); else update(t<<1|1,id); push_up(t); } int main() { int n,i,j; int a[N]; while(~scanf("%d",&n)&&n!=0) { buildtree(1,0,n-1); int sum=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=query(1,a[i]+1,n-1);//找比a[i]大的数的个数的和 update(1,a[i]);//将该值加入到线段树中 } int ans=sum; for(i=0;i<n;i++) { sum+=n-2*a[i]-1; ans=min(ans,sum); } printf("%d\n",ans); } return 0; }
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1 10 2 1 5 2 5 9 3
Sample Output
Case 1: The total value of the hook is 24.
解题思路:区间修改与区间求和(lazy标记)
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N=100010; struct node { int sum,lazy; }tr[4*N]; void push_up(int m) { tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum; } void push_down(int t,int l,int r) { int mid=(l+r)>>1; tr[t<<1].lazy=tr[t<<1|1].lazy=tr[t].lazy; tr[t<<1].sum=tr[t].lazy*(mid-l+1); tr[t<<1|1].sum=tr[t].lazy*(r-mid); tr[t].lazy=0; } void buildtree(int t,int l,int r) { tr[t].lazy=0; if(l==r) { tr[t].sum=1; return; } int mid=(l+r)>>1; buildtree(t<<1,l,mid); buildtree(t<<1|1,mid+1,r); push_up(t); } void update(int t,int L,int R,int l,int r,int flag) { if(L<=l&&r<=R) { tr[t].lazy=flag; tr[t].sum=(r-l+1)*flag; return; } if(tr[t].lazy) push_down(t,l,r); int mid=(l+r)>>1; if(L<=mid) update(t<<1,L,R,l,mid,flag); if(mid<R) update(t<<1|1,L,R,mid+1,r,flag); push_up(t); } long long query(int a,int b,int l,int r,int t) { if(a<=l&&r<=b) return tr[t].sum; if(tr[t].lazy) push_down(t,l,r); int mid=(l+r)>>1; long long sum=0; if(a<=mid) sum+=query(a,b,l,mid,t<<1); if(b>mid) sum+=query(a,b,mid+1,r,t<<1|1); return sum; } int main() { int t,i,j; scanf("%d",&t); for(i=1;i<=t;i++) { int n,q; scanf("%d%d",&n,&q); buildtree(1,1,n); while(q--) { int a,b,flag; scanf("%d%d%d",&a,&b,&flag); update(1,a,b,1,n,flag); } printf("Case %d: The total value of the hook is %d.\n",i,tr[1].sum); } return 0; }
6.A - I Hate It
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
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
解题思路:线段树求区间最大与单点修改
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N=200010; struct node { int l,r,ans; }tr[N*4]; int pre[N]; void buildtree(int t,int l,int r) { tr[t].l=l; tr[t].r=r; if(l==r) { tr[t].ans=pre[l];//叶子节点赋值 return; } int mid=(l+r)>>1; buildtree(t<<1,l,mid); buildtree(t<<1|1,mid+1,r); tr[t].ans=max(tr[t<<1].ans,tr[t<<1|1].ans);//求出区间最大 } void update(int t,int id,int key)//单点修改 { if(tr[t].l==id&&tr[t].r==id) { tr[t].ans=key; return; } int mid=(tr[t].l+tr[t].r)>>1; if(id<=mid) update(t<<1,id,key); else update(t<<1|1,id,key); tr[t].ans=max(tr[t<<1].ans,tr[t<<1|1].ans);//更新区间的最大值 } int query(int t,int l,int r)//区间求最大值 { if(tr[t].l>=l&&tr[t].r<=r) return tr[t].ans; int mid=(tr[t].l+tr[t].r)>>1; int ans=-100; if(l<=mid)//找出左半边的最大值 ans=query(t<<1,l,r); if(r>mid)//将作半边的最大值与右半边的最大值比较 ans=max(ans,query(t<<1|1,l,r)); return ans; } int main() { int n,m,i; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { scanf("%d",&pre[i]); } buildtree(1,1,n); while(m--) { char s[2]; int a,b; scanf("%s",&s); scanf("%d%d",&a,&b); if(s[0]=='Q') { printf("%d\n",query(1,a,b)); } if(s[0]=='U') { update(1,a,b); } } } return 0; }
7.B - Billboard
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.
InputThere 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.OutputFor 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<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N=200010; int maxx[N*4]; int h,w,n; void push_up(int m) { maxx[m]=max(maxx[m<<1],maxx[m<<1|1]); } void build(int t,int l,int r) { if(l==r) { maxx[t]=w; return; } int mid=(l+r)>>1; build(t<<1,l,mid); build(t<<1|1,mid+1,r); push_up(t); } int update(int t,int val,int l,int r) { if(val>maxx[t]) return -1; if(l==r) { if(val<=maxx[t]) { maxx[t]-=val; return l; } else return -1; } int mid=(l+r)>>1; int flag=-1; flag=update(t<<1,val,l,mid); if(flag<0) flag=update(t<<1|1,val,mid+1,r); push_up(t); return flag; } int main() { int i,j; while(scanf("%d%d%d",&h,&w,&n)!=EOF) { if(h>n) h=n; build(1,1,h); int val; int k; for(i=1;i<=n;i++) { scanf("%d",&val); printf("%d\n",update(1,val,1,h)); } } return 0; }
总结:lazy标记线段树需要加强,区间修改操作的不熟练需要加强。