[BZOJ3212] Pku3468 A Simple Problem with Integers

原创 2015年11月20日 20:40:53

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=3212

题目大意

区间修改,区间查询和

题解

水题
树状数组版本 POJ 954MS

const
    maxn=200000;
type
    data=array[0..maxn]of int64;
var
    x,y,z,c,d:data;
    i,j,k,l:longint;
    n,m,t:longint;
    a,b,val,ans:int64;
    cha:char;
procedure update(var a:data;pos,val:int64);
begin
    while pos<=n do
        begin
            a[pos]:=a[pos]+val;
            inc(pos,pos and (-pos));
        end;
end;

function query(var a:data;pos:int64):int64;
var sum:int64;
begin
    sum:=0;
    while pos>0 do
        begin
            sum:=sum+a[pos];
            dec(pos,pos and (-pos));
        end;
    exit(sum);
end;

begin
    readln(n,m); x[0]:=0;
    for i:=1 to n do
        begin
            read(x[i]);
            y[i]:=x[i]-x[i-1];
            z[i]:=y[i]*i;
            update(c,i,y[i]);
            update(d,i,z[i]);
        end;
    readln;
    for i:=1 to m do
        begin
            read(cha);
            if cha='Q'
            then
                begin
                    readln(a,b);
                    ans:=((b+1)*query(c,b)-query(d,b))-((a)*query(c,a-1)-query(d,a-1));
                    writeln(ans);
                end
            else
                begin
                    readln(a,b,val);
                    update(c,a,val); update(c,b+1,-val);
                    y[a]:=y[a]+val; y[b+1]:=y[b+1]-val;
                    update(d,a,y[a]*a-z[a]); update(d,b+1,y[b+1]*(b+1)-z[b+1]);
                    z[a]:=y[a]*a; z[b+1]:=y[b+1]*(b+1);
                end;
        end;
end.

线段树版本 POJ 2172MS

const
 maxn=100005;
type
 aa=record
  l,r:longint; val,tag:int64;
 end;
var
 w:array[0..8*maxn]of aa;
 i,j,k:longint;
 n,m:longint;
 a,b,c:longint;
 ch:char;
procedure build(a,l,r:longint);
var mid:longint;
begin
 w[a].l:=l; w[a].r:=r; w[a].val:=0; w[a].tag:=0;
 if l=r then begin read(w[a].val); exit; end;
 mid:=(l+r)>>1;
 build(a<<1,l,mid); build(a<<1+1,mid+1,r);
 w[a].val:=w[a<<1].val+w[a<<1+1].val;
end;

procedure pushdown(a:longint);
begin
 if w[a].l=w[a].r then begin w[a].tag:=0; exit; end;
 inc(w[a<<1].val,w[a].tag*(w[a<<1].r-w[a<<1].l+1));
 inc(w[a<<1+1].val,w[a].tag*(w[a<<1+1].r-w[a<<1+1].l+1));
 inc(w[a<<1].tag,w[a].tag); inc(w[a<<1+1].tag,w[a].tag);
 w[a].tag:=0;
end;

procedure update(a,l,r,c:longint);
var mid:longint;
begin
 if w[a].tag<>0 then pushdown(a);
 if (l=w[a].l)and(w[a].r=r) then begin inc(w[a].val,c*(w[a].r-w[a].l+1)); inc(w[a].tag,c); exit; end;
 mid:=(w[a].l+w[a].r)>>1;
 if r<=mid then update(a<<1,l,r,c) else
 if l>mid then update(a<<1+1,l,r,c)
 else begin update(a<<1,l,mid,c); update(a<<1+1,mid+1,r,c); end;
 w[a].val:=w[a<<1].val+w[a<<1+1].val;
end;

function query(a,l,r:longint):int64;
var mid:longint;
begin
 if w[a].tag<>0 then pushdown(a);
 if (l=w[a].l)and(w[a].r=r) then exit(w[a].val);
 mid:=(w[a].l+w[a].r)>>1;
 if r<=mid then exit(query(a<<1,l,r)) else
 if l>mid then exit(query(a<<1+1,l,r))
 else exit(query(a<<1,l,mid)+query(a<<1+1,mid+1,r));
end;

begin
 readln(n,m);
 build(1,1,n); readln;
 for i:=1 to m do
  begin
   read(ch);
   case ch of
   'Q':begin readln(a,b); writeln(query(1,a,b)); end;
   'C':begin readln(a,b,c); update(1,a,b,c); end;
   end;
  end;
end.
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

BZOJ P3212 Pku3468 A Simple Problem with Integers

线段树区间加区间查询 记得要开long long #include #include #include #include #include using namespace std; int n,m...

【bzoj 3212】【POJ 3468】A Simple Problem with Integers(线段树)

寒乍起,孤寂伴,长立风中,愿为风散

PKU3468(A Simple Problem with Integers)线段树的成段更新

/*********************************************** 题目大意: Q a b :查询区间[a,b]的和; C a b x : 更新区间[a,b],区间所...
  • Jarily
  • Jarily
  • 2013-03-07 15:25
  • 1039

PKU 3468 A Simple Problem with Integers

题目:http://poj.org/problem?id=3468 题意:有N个整数,有两个操作,一个是对给定的区间增加值,一个是查询给定区间的和 #include const int maxn...

pku 3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 4...

PKU 3468 A Simple Problem with Integers

线段树区间求和

Pku oj 3468 A Simple Problem with Integers(线段树Lazy标记)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Subm...

线段树系列-pku-3468-A Simple Problem with Integers-区间修改区间求和

一道简单的区间修改区间求和 #include #include #include #include #include using namespace std; struct seg { in...

PKU 3468 A Simple Problem with Integers

线段树 bit

pku -- 3468 A Simple Problem with Integers(线段树)

题目连接:http://poj.org/problem?id=3468       Time Limit: 5000MS Memory Limit: 131072K Total Submis...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)