zoukankan      html  css  js  c++  java
  • [poj3468]A Simple Problem with Integers

    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<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    LL read()
    {
    	LL 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*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    int n,q;
    struct data{LL s,tag;}t[400050];
    inline void pushup(int k)
    {
    	t[k].s=t[k<<1].s+t[k<<1|1].s;
    }
    inline void pushdown(int k,int l,int r)
    {
    	LL x=t[k].tag;t[k].tag=0;
    	if(x==0)return;
    	int mid=(l+r)>>1;
    	t[k<<1].s+=x*(mid-l+1),t[k<<1].tag+=x;
    	t[k<<1|1].s+=x*(r-mid),t[k<<1|1].tag+=x;
    }
    void build(int k,int l,int r)
    {
    	t[k].tag=0;
    	if(l==r){t[k].s=read();return;}
    	int mid=(l+r)>>1;
    	build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    	pushup(k);
    }
    void updata(int k,int l,int r,int a,int b,int key)
    {
    	if(l==a&&r==b)
    	{
    		t[k].s+=(LL)(r-l+1)*key;
    		t[k].tag+=key;
    		return;
    	}
    	int mid=(l+r)>>1;pushdown(k,l,r);
    	if(b<=mid)updata(k<<1,l,mid,a,b,key);
    	else if(a>mid)updata(k<<1|1,mid+1,r,a,b,key);
    	else updata(k<<1,l,mid,a,mid,key),updata(k<<1|1,mid+1,r,mid+1,b,key);
    	pushup(k);
    }
    LL getsum(int k,int l,int r,int a,int b)
    {
    	if(l==a&&r==b)return t[k].s;
    	int mid=(l+r)>>1;pushdown(k,l,r);
    	if(b<=mid)return getsum(k<<1,l,mid,a,b);
    	else if(a>mid)return getsum(k<<1|1,mid+1,r,a,b);
    	else return getsum(k<<1,l,mid,a,mid)+getsum(k<<1|1,mid+1,r,mid+1,b);
    }
    int main()
    {
    	n=read(),q=read();
    	build(1,1,n);
    	while(q--)
    	{
    		char op[5];scanf("%s",op);
    		if(op[0]=='Q')
    		{
    			int x=read(),y=read();
    			printf("%lld
    ",getsum(1,1,n,x,y));
    		}
    		if(op[0]=='C')
    		{
    			int x=read(),y=read(),z=read();
    			updata(1,1,n,x,y,z);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    java实验四
    C# 和SQL Server 类型转换
    C# 通过this关键字来扩展方法
    类型转换
    获取屏幕高度,兼容性问题解决
    LinqToDataTable
    jQuery层级元素选择器
    Javascript 常用系统内置函数
    JSON-JQuery常用技巧
    .net 调用SAP RFC函数获取数据的两种方式
  • 原文地址:https://www.cnblogs.com/ljzalc1022/p/8762174.html
Copyright © 2011-2022 走看看