shu_mj 的数据结构专场(二)

来源:互联网 发布:叶利钦炮打白宫知乎 编辑:程序博客网 时间:2024/06/07 19:32

2333 这个专题还是挺基础的 做完了大概线段树入门?嗯。。。骗骗自己入门了吧23333

Problem A: 求个和 (shuoj1883)

Description
给你一个长为N 的序列,M 条询问,每次问你[l, r] 区间内,所有数字的和是多少。

Input
第一行包含两个整数 N, M。
第二行有 N 个整数 Ai,表示这个序列。
接下来 M 行,每行两个整数 l r,表示一个询问。

0 < N, M < 1e5
-1e4 < Ai < 1e4
0 < l <= r < N + 1

Output
对每个询问输出区间和就好啦~~~

Sample Input
5 3
1 2 2 1 3
1 2
2 4
1 5
Sample Output
3
5
9

非常纯洁的区间查询和2333树状数组美滋滋

`

#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;//thanks to pyf ...#define N 100005int a[N];int n;int lowbit(int x){    return x&(-x);}void update(int pos,int val){    while(pos<=n)    {        a[pos]+=val;        pos+=lowbit(pos);    }}int query(int pos){    int ans = 0;    while(pos>0)    {        ans += a[pos];        pos -= lowbit(pos);    }    return ans;}int main(){    int m;    while(scanf("%d%d",&n,&m)==2)    {        memset(a,0,sizeof(a));        for(int i=1;i<=n;i++)        {            int x;            scanf("%d",&x);            update(i,x);        }        for(int i=0;i<m;i++)        {            int a,b;            scanf("%d%d",&a,&b);            printf("%d\n",query(b)-query(a-1));        }    }}

Problem B: 变个法地求个和(shuoj1884)

Description
给你一个长为 N 的序列,M 条命令,两种格式:
1 k z,让你第 k 个数字加上 z;
2 x y,问你 [x, y] 区间内的数字的和是多少。

Input
第一行包含两个整数 N M。
第二行包含 N 个整数 Ai,代表这个序列。
接下来 M 行每行 3 个整数 t x y,表示一条命令。

0 < N, M < 1e5
-1e4 < Ai < 1e4
0 < t < 3
0 < x <= y < N + 1
-1e4 < z < 1e4
0 < k < N + 1

Output
对于每个询问输出区间和。

Sample Input
5 3
1 2 2 1 3
2 1 2
1 2 4
2 1 5
Sample Output
3
13

单点更新区间查询 ,还是树状数组美滋滋23333

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;//thanks to pyf ...#define N 100005int n;int a[N];int lowbit(int x){    return x&(-x);}void update(int pos,int val){    while(pos<=n)    {        a[pos]+=val;        pos+=lowbit(pos);    }}int query(int pos){    int ans = 0;    while(pos>0)    {        ans += a[pos];        pos -= lowbit(pos);    }    return ans;}int main(){    int m;    while(scanf("%d%d",&n,&m)==2)    {        memset(a,0,sizeof(a));        for(int i=1;i<=n;i++)        {            int x;            scanf("%d",&x);            update(i,x);        }        for(int i=0;i<m;i++)        {            int op,a,b;            scanf("%d%d%d",&op,&a,&b);            if(op==1)                update(a,b);            else            {                printf("%d\n",query(b)-query(a-1));            }        }    }}

Problem C: 变着法地求个值(shuoj1885)

Description
给你一个长为 N 的序列,M 条命令,两种格式:
1 x y z,让你把 [x, y] 区间内的数字加上 z;
2 k,问你第 k 个数字是多少。

Input
第一行包含两个整数 N M。
第二行包含 N 个整数 Ai,表示这个序列。
接下来 M 行,每行包含一条命令。

0 < N, M < 1e5
-1e4 < Ai < 1e4
-1e4 < z < 1e4
0 < x <= y < N + 1
0 < k < N + 1

Output
对于每个询问,输出答案。

Sample Input
5 3
1 2 2 1 3
1 1 2 1
2 1
2 3
Sample Output
2
2

区间更新单点查询-0-2333我这种zz只会用线段树
区间更新可用lazy target 去操作优化时间 嗯。。。懒惰标记
就是 停在那里。。。不用就停着 用了再下放2333 不管你是谁看到这里就去网上找其他讲这玩意的题解吧2333或者说不定你还能直接找到我

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;//thanks to pyf ...#define N 100005struct tree{    int l,r;    long long sum;    long long vis;}t[N*4];void push_up(int step){    t[step].sum = t[step*2].sum + t[step*2+1].sum;}void push_down(int step){    if(!t[step].vis)        return;    t[step*2].vis += t[step].vis;    t[step*2+1].vis += t[step].vis;    t[step*2].sum += t[step].vis * (t[step*2].r-t[step*2].l+1);    t[step*2+1].sum += t[step].vis *(t[step*2+1].r-t[step*2+1].l+1);    t[step].vis = 0;}void build(int l,int r,int step){    t[step].l = l;    t[step].r = r;    t[step].sum = 0;    t[step].vis = 0;    if(l==r)    {        scanf("%lld",&t[step].sum);        return;    }    int mid = (l+r)/2;    build(l,mid,step*2);    build(mid+1,r,step*2+1);    push_up(step);}void update(int l,int r,int val,int step){    if(l==t[step].l&&r==t[step].r)    {        t[step].vis += val;        t[step].sum += val*(t[step].r-t[step].l+1);        return ;    }    push_down(step);    int mid = (t[step].l + t[step].r) /2;    if(r<=mid)        update(l,r,val,step*2);    else if(l>mid)        update(l,r,val,step*2+1);    else    {        update(l,mid,val,step*2);        update(mid+1,r,val,step*2+1);    }    push_up(step);}long long query(int l,int r,int step){    if(t[step].l==l&&t[step].r==r)        return t[step].sum;    push_down(step);    int mid = (t[step].l+t[step].r)/2;    if(r<=mid)        return query(l,r,step*2);    else if(l>mid)        return query(l,r,step*2+1);    else        return query(l,mid,step*2)+query(mid+1,r,step*2+1);}long long query_data(int x,int step){    if(t[step].l==x&&t[step].r==x)        return t[step].sum;    push_down(step);    int mid = (t[step].l + t[step].r)/2;    if(x<=mid)        return query_data(x,step*2);    else        return query_data(x,step*2+1);}int main(){    int n,m;    while(scanf("%d%d",&n,&m)==2)    {        build(1,n,1);        for(int i=0;i<m;i++)        {            int op;            scanf("%d",&op);            if(op==1)            {                int x,y,z;                scanf("%d%d%d",&x,&y,&z);                update(x,y,z,1);            }            else            {                int x,y;                scanf("%d",&x);                printf("%lld\n",query_data(x,1));            }        }    }    return 0;}

Problem D: 大和小(shuoj1886)

Description
给你一个长为 N 的序列。M 条命令,两种格式:
1 k b:让你改变第 k 个数字加上 b;
2 l r:问你 [l, r] 区间内的最大值,和,最小值。

Input
第一行包含两个整数 N, M。
第二行包含 N 个数字 Ai,表示这个序列。
接下来 M 行,每行包含一条命令。

0 < N, M < 1e5
0 < k < N + 1
-1e4 < Ai < 1e4
-1e4 < b < 1e4
0 < l <= r < N + 1

Output
对每个询问,输出 3 个值:区间的最大值,区间和,最小值。

Sample Input
5 3
1 2 2 1 3
1 3 2
1 4 5
2 2 5
Sample Output
6 15 2

2333 这就很僵了,同时维护区间两个最值和区间和,进行三次查询怕不是TLE-0- 不知道两次会不会T,感觉WF大佬好像T了。。用个结构体存两个最值和区间和直接返回,最后返回的结构体里的三个玩意就是答案

单点更新 区间查询

#include<iostream>#include<algorithm>#include<cstring>using namespace std;//thanks to pyf ...#define N 100005#define INF 0x3f3f3f3fstruct Ans{    int Min,Max,sum;};struct tree{    int l,r;    Ans res;}t[N*4];void push_up(int step){    t[step].res.sum = t[step*2].res.sum + t[step*2+1].res.sum;    t[step].res.Min = min(t[step*2].res.Min,t[step*2+1].res.Min);    t[step].res.Max = max(t[step*2].res.Max,t[step*2+1].res.Max);}void debug(int step){    if(t[step].l == t[step].r)    { //       cout << "**" <<  t[step].l << " " << t[step].res.Min << " " << t[step].res.sum << " " << t[step].res.Max << endl;        return ;    }    int mid  = (t[step].l+t[step].r)/2;    debug(step*2);    debug(step*2+1);}void build(int l,int r,int step){    t[step].l = l;    t[step].r = r;    t[step].res.sum = 0;    t[step].res.Max = -INF;    t[step].res.Min = INF;    if(l==r)    {        scanf("%d",&t[step].res.sum);        t[step].res.Min = t[step].res.Max = t[step].res.sum;        return;    }    int mid = (l+r)/2;    build(l,mid,step*2);    build(mid+1,r,step*2+1);    push_up(step);}void update(int x,int val,int step){    if(t[step].l==t[step].r)    {        t[step].res.sum += val;        t[step].res.Min = t[step].res.Max = t[step].res.sum;        return;    }    int mid = (t[step].l+t[step].r)/2;    if(x<=mid)        update(x,val,step*2);    else        update(x,val,step*2+1);    push_up(step);}Ans query(int l,int r,int step){    if(t[step].l==l&&t[step].r==r)        return t[step].res;    int mid = (t[step].l+t[step].r)/2;    if(r<=mid)        return query(l,r,step*2);    else if(l>mid)        return query(l,r,step*2+1);    else    {        Ans temp;        Ans ans1 = query(l,mid,step*2);        Ans ans2 = query(mid+1,r,step*2+1);        temp.Max = max(ans1.Max,ans2.Max);        temp.Min = min(ans1.Min,ans2.Min);        temp.sum = ans1.sum + ans2.sum;        return temp;    }}int main(){    int n,m;    while(scanf("%d%d",&n,&m)==2)    {        build(1,n,1);        for(int i=0;i<m;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            //cin >> a >> b >> c;            if(a==1)            {                update(b,c,1);//              debug(1);            }            else            {                Ans temp = query(b,c,1);                printf("%d %d %d\n",temp.Max,temp.sum,temp.Min);  //              cout << temp.Max << " " << temp.sum << " " << temp.Min << endl;            }        }    }    return 0;}
0 0
原创粉丝点击