A + b problem solution problem

Title: Enter two positive integers not more than 10,000, and the outputs of the two numbers

answer:

High-energy front

A segment tree template title ~

On the code:

#include<bits/stdc++.h>
using namespace std;
#define N 10000
struct Tree {int l,r,len;}a[N<<2+10];
int b[N<<2+10],num[N+10];
void Build(int l,int r,int s)
{
    a[s].l=l;
    a[s].r=r;
    a[s].len=r-l+1;
    if(l==r)
    {
        b[s]=num[l];
        return;
    }
    int mid=(r+l)>>1;
    Build(l,mid,s<<1);
    Build(mid+1,r,(s<<1)|1);
    b[s]=b[s<<1]+b[(s<<1)|1];
}
int query(int l,int r,int s)
{
    if(a[s].l>=l&&a[s].r<=r) return b[s];
    int ans=0;
    if(a[s<<1].r>=l) ans+=query(l,r,s<<1);
    if(a[(s<<1)|1].l<=r) ans+=query(l,r,(s<<1)|1);
    return ans;
}
inline int Read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
int main()
{
    int n=Read(),m=Read();
    int maxn=n>m?n:m;
    fill(num+1,num+maxn+1,1);
    Build(1,maxn,1);
    printf("%d",query(1,n,1)+query(1,m,1));
    return 0;
}

but...

Guess you like

Origin www.cnblogs.com/juruo-zzt/p/12083018.html