Codevs 4919 线段树练习4

4919 线段树练习4
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold
题目描述 Description
给你N个数,有两种操作
1:给区间[a,b]内的所有数都增加X
2:询问区间[a,b]能被7整除的个数
输入描述 Input Description
第一行一个正整数n,接下来n行n个整数,再接下来一个正整数Q,表示操作的个数. 接下来Q行每行若干个整数。如果第一个数是add,后接3个正整数a,b,X,表示在区间[a,b]内每个数增加X,如果是count,表示统计区间[a,b]能被7整除的个数
输出描述 Output Description
对于每个询问输出一行一个答案
样例输入 Sample Input
3
2 3 4
6
count 1 3
count 1 2
add 1 3 2
count 1 3
add 1 3 3
count 1 3
样例输出 Sample Output
0
0
0
1
数据范围及提示 Data Size & Hint
10%:1

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct Node{
    int l,r,lazy,s[7];
    Node* child[2];
}*root=NULL;
int n,m,a[100010];
int init(){
    int x=0;char s;int f=0;s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    if(f==1)return -x;return x;
}
void Sum_Bage(Node *cur){
    for(int i=0;i<=6;i++)
        cur->s[i] = cur->child[0]->s[i] +
                     cur->child[1]->s[i];
}
void Built(Node *&cur,int l,int r){
    cur=new Node;
    cur->l=l; cur->r=r;
    for(int i=0;i<=6;i++) cur->s[i] = 0;// 不加会WA的
    if(l==r){
        cur->s[a[l]%7] ++;
        return ;
    }
    int mid=(l+r)>>1;
    Built(cur->child[0],l,mid);
    Built(cur->child[1],mid+1,r);
    Sum_Bage(cur);
}
void Down(Node *cur){
    if(cur->l == cur->r) return;
    int tmp[8];
    cur->child[0]->lazy += cur->lazy;
    cur->child[1]->lazy += cur->lazy;
    for(int i=0;i<=6;i++) tmp[i] = cur->child[0]->s[i];
    for(int i=0;i<=6;i++){
        cur->child[0]->s[(i+ cur->lazy)%7] = tmp[i];
        tmp[i] = cur->child[1]->s[i];
    }
    for(int i=0;i<=6;i++)
        cur->child[1]->s[(i+ cur->lazy)%7]=tmp[i];
    cur->lazy = 0;
}
int Query(Node* cur,int l,int r){
    if(l== cur->l && cur->r ==r) return cur->s[0];
    if(cur->lazy) Down(cur);
    int mid=(cur->l + cur->r)>>1;
    if(l>mid) return Query(cur->child[1],l,r);
    else if(r<=mid) return Query(cur->child[0],l,r);
    else return Query(cur->child[0],l,mid)
                +Query(cur->child[1],mid+1,r);
}
void Change(Node* cur,int l,int r,int value){
    if(l == cur->l && cur->r == r){
        int tmp[8];
        for(int i=0;i<=6;i++) tmp[i] = cur->s[i];//Copy
        for(int i=0;i<=6;i++) 
            cur->s[ (i+value) %7 ] = tmp[i];
        cur->lazy += value;
        return ;
    }
    if(cur->lazy) Down(cur);
    int mid=(cur->l + cur->r)>>1;
    if(l>mid) Change(cur->child[1],l,r,value);
    else if(r<=mid) Change(cur->child[0],l,r,value);
    else Change(cur->child[0],l,mid,value),
            Change(cur->child[1],mid+1,r,value);
    Sum_Bage(cur);
    return ;
}
int main(){
    n=init();
    for(int i=1;i<=n;i++) a[i]=init(),a[i]%=7;
    Built(root,1,n);
    m=init(); char ss[50];
    for(int i=1,z,x,y;i<=m;i++){
        scanf("%s",ss);
        if(ss[0]=='c'){
            x=init(); y=init();
            printf("%d\n",Query(root,x,y));
        }
        else if(ss[0]=='a'){
            x=init(); y=init(); z=init();
            Change(root,x,y,z%7);
        }
    }
    return 0;
}

用s数组表示 该区间内部 余数为1、2、3…的分别有几个,修改时开一个临时数组,copy一下~~

数组版:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define Max 100000
struct Node{
    int l,r,lazy,s[7];
}tre[Max<<2|1];
int read(){
    int x=0,f=1; char s=getchar();
    while(s<'0'||s>'9'){if(s=='-') f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    return x*f;
}
int n,m;
void Sum(int now){
    for(int i=0;i<=6;i++)
        tre[now].s[i] = tre[now<<1].s[i] 
                        + tre[now<<1|1].s[i];
    return ;
}
void Built(int now,int l,int r){
    tre[now].l=l; tre[now].r=r;
    for(int i=0;i<=6;i++) tre[now].s[i]=0;
    if(l==r){
        int a=read(); tre[now].s[(a%7)]++; return ;
    }
    int mid=(l+r) >> 1;
    Built(now<<1,l,mid);
    Built(now<<1|1,mid+1,r);
    Sum(now);
}
void Down(int now){
    if(tre[now].l==tre[now].r) return;
    int tmp[7];
    tre[now<<1].lazy += tre[now].lazy;
    tre[now<<1|1].lazy += tre[now].lazy;
    for(int i=0;i<=6;i++) tmp[i]=tre[now<<1].s[i];
    for(int i=0;i<=6;i++){
        tre[now<<1].s[(i+tre[now].lazy)%7] = tmp[i];
        tmp[i] = tre[now<<1|1].s[i];
    }
    for(int i=0;i<=6;i++)
        tre[now<<1|1].s[(i+tre[now].lazy)%7] = tmp[i];
    tre[now].lazy = 0;
}
int Query(int now,int l,int r){
    if(tre[now].l==l&&tre[now].r==r) return tre[now].s[0];
    if(tre[now].lazy) Down(now);
    int mid=(tre[now].l + tre[now].r)>>1;
    if(l>mid) return Query(now<<1|1,l,r);
    else if(r<=mid) return Query(now<<1,l,r);
    else return Query(now<<1,l,mid)
                +Query(now<<1|1,mid+1,r);
}
void Change(int now,int l,int r,int p){
    if(tre[now].l==l&&tre[now].r==r){
        int tmp[8];
        for(int i=0;i<=6;i++) tmp[i]=tre[now].s[i];
        for(int i=0;i<=6;i++) tre[now].s[(i+p)%7]=tmp[i];
        tre[now].lazy += p;
        return ;
    }
    if(tre[now].lazy) Down(now);
    int mid=(tre[now].l + tre[now].r)>>1;
    if(l>mid) Change(now<<1|1,l,r,p);
    else if(r<=mid) Change(now<<1,l,r,p);
    else Change(now<<1,l,mid,p),
            Change(now<<1|1,mid+1,r,p);
    Sum(now);
}
int main(){
    n=read();
    Built(1,1,n);
    m=read();
    char str[50];
    for(int i=1,x,y,z;i<=m;i++){
        scanf("%s",str);
        if(str[0] == 'c'){
            x=read(); y=read();
            printf("%d\n",Query(1,x,y));
        }
        else if(str[0] == 'a'){
            x=read(); y=read(); z=read();
            Change(1,x,y,z);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值