一:线段树问题(单点与区间)
1:线段树问题 (单点修改,区间内单点询问)(题目链接)
Problem Description 很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
Input 本题目包含多组测试,请处理到文件结束。
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 注意:1:此题要用scanf输入,不然会超时2:此题scanf后要用getchar()接受换行符,不然会超时 |
|
2:线段树(区间修改(更换数据),区间访问,传递懒惰标志)题目链接
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.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.
Input The 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.
Output For 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.
|
3:线段树(区间修改(累加数据),区间访问,传递懒惰标志)题目链接
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 165482 | Accepted: 50987 | |
Case Time Limit: 2000MS |
Description
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
Hint
The sums may exceed the range of 32-bit integers.
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
ll l,r,sum,lazy;
}a[400000];
ll num[100001];
void update(ll k){
a[k].sum=a[k<<1].sum+a[k<<1|1].sum;
return ;
}
//在pushdown函数中和上一种区间修改有很大不同
void pushdown(ll k){
if(a[k].lazy==0)
return ;//退出条件不要忘记,老是忘
a[k<<1].sum+=(a[k<<1].r-a[k<<1].l+1)*a[k].lazy;
a[k<<1|1].sum+=(a[k<<1|1].r-a[k<<1|1].l+1)*a[k].lazy;
//不同点开始,左右孩子的懒惰标志累加上父亲的懒惰标志
//不是直接赋值
a[k<<1].lazy+=a[k].lazy;
a[k<<1|1].lazy+=a[k].lazy;
a[k].lazy=0;//继续让父亲的懒惰标志清零
}
//同上
void build(ll k,ll l,ll r){
a[k].l=l;
a[k].r=r;
a[k].lazy=0;
if(l==r){
a[k].sum=num[l];
return ;
}
ll mid=(a[k].l+a[k].r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
update(k);
return ;
}
//注意
void change(ll k,ll x,ll y,ll z){
if(a[k].l>=x&&a[k].r<=y){
a[k].sum+=(a[k].r-a[k].l+1)*z;
a[k].lazy+=z;//这里必须也是累加,不能直接覆盖
return ;
}
pushdown(k);
ll mid=(a[k].l+a[k].r)>>1;
if(y>mid){
change(k<<1|1,x,y,z);
}
if(x<=mid){
change(k<<1,x,y,z);
}
update(k);
pushdown(k);
return ;
}
//同上
ll query(ll k,ll x,ll y){
if(a[k].l>=x&&a[k].r<=y){
return a[k].sum;
}
ll mid=(a[k].l+a[k].r)>>1;
pushdown(k);
ll sum=0;
if(y>mid){
sum+=query(k<<1|1,x,y);
}
if(x<=mid){
sum+=query(k<<1,x,y);
}
return sum;
}
int main(){
ll n,m;
scanf("%lld %lld",&n,&m);
for(ll i=1;i<=n;i++){
scanf("%lld",&num[i]);
}
build(1,1,n);//建线段树
getchar();
char c;
ll x,y,z;
while(m--){
scanf("%c",&c);
getchar();
if(c=='C'){
scanf("%lld %lld %lld",&x,&y,&z);
getchar();
change(1,x,y,z);
continue;
}
scanf("%lld %lld",&x,&y);
getchar();
printf("%lld\n",query(1,x,y));
}
return 0;
}
二:树状数组(适用于求区间的和问题)题目链接
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<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
//找x从最右边数第一个不是零的位置(不准确)
ll lowbit(ll x){
return x&(-x);
}
ll a[50001],c[50001],n;
//修改POS位置上的数加k
void update(ll pos,ll k){
while(pos<=n){
c[pos]+=k;
pos+=lowbit(pos);//背过,加法
}
}
//询问前POS项的和
ll query(ll pos){
ll sum=0;
while(pos>0){
sum+=c[pos];
pos-=lowbit(pos);//背过,减法
}
return sum;
}
int main(){
ll t,cnt=1;
cin>>t;
while(t--){
cout<<"Case "<<cnt++<<":"<<endl;
memset(c,0,sizeof(c));//树状数组,初始化为0
cin>>n;
for(ll i=1;i<=n;i++){
cin>>a[i];
}
// C[i] = A[i - 2^k+1] + A[i - 2^k+2] + ... + A[i];
// k为i的二进制中从最低位到高位连续零的长度
for(ll i=1;i<=n;i++){
ll k=lowbit(i);//其实lowbit(i)就是2^k
for(ll cnt=1;cnt<=k;cnt++){
c[i]+=a[i-k+cnt];//利用上边的推导式计算出树状数组
}
}
string s;
while(1){
cin>>s;
ll i,j;
if(s=="End"){
break;
}
if(s=="Add"){
cin>>i>>j;
update(i,j);
continue;
}
if(s=="Sub"){
cin>>i>>j;
update(i,-j);
continue;
}
cin>>i>>j;
cout<<query(j)-query(i-1)<<endl;
}
}
return 0;
}