MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
代码
<pre lang="cpp" line="1">
/*
Mail to : miyubai@gamil.com
My Blog : www.baiyun.me
Link : http://www.cnblogs.com/MiYu || http://www.cppblog.com/MiYu
Author By : MiYu
Test : 1
Complier : g++ mingw32-3.4.2
Program :
Doc Name :
*/
//#pragma warning( disable:4789 )
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include
<map>
#include <utility>
#include <queue>
#include <stack>
#include
<list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
inline bool scan_d(int &num) //整数输入
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
struct seg_tree{
int l,r,val;
long long sum;
int mid () { return (r+l) >> 1; }
int dis () { return r-l+1; }
}seg[600000];
int v[100010];
void creat ( int x, int y, int rt = 1 ) {
seg[rt].l = x;
seg[rt].r = y;
seg[rt].val = 0;
if ( x == y ) {
return ;
}
int mid = seg[rt].mid();
creat ( x, mid, rt << 1 );
creat ( mid + 1, y, rt << 1 | 1 );
}
long long cr ( int x, int y, int rt = 1 ) {
seg[rt].val = 0;
if ( seg[rt].l == seg[rt].r ) {
seg[rt].sum = v[x];
return v[x];
}
int mid = seg[rt].mid();
return seg[rt].sum = cr ( x, mid, rt << 1 )
+ cr ( mid + 1, y, rt << 1 | 1 );
}
inline void pushdown ( int rt ) {
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
if ( seg[rt].val ) {
seg[LL].sum += (long long)seg[LL].dis() * seg[rt].val;
seg[RR].sum += (long long)seg[RR].dis() * seg[rt].val;
seg[LL].val += seg[rt].val;
seg[RR].val += seg[rt].val;
seg[rt].val = 0;
}
}
void md ( int x, int y, int val, int rt = 1 ) {
if ( seg[rt].l == x && seg[rt].r == y ) {
seg[rt].val += val;
seg[rt].sum += (long long)seg[rt].dis() * val;
return;
}
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
pushdown ( rt );
if ( x > mid ) md ( x, y, val, RR );
else if ( y <= mid ) md ( x, y, val, LL );
else {
md ( x, mid, val, LL );
md ( mid+1, y, val, RR );
}
seg[rt].sum = seg[LL].sum + seg[RR].sum;
}
long long q ( int x, int y, int rt = 1 ) {
if ( seg[rt].l == x && seg[rt].r == y ) {
return seg[rt].sum;
}
pushdown ( rt );
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
if ( x > mid ) return q ( x, y, RR );
else if ( y <= mid ) return q ( x, y, LL );
else {
return q ( x, mid, LL ) + q ( mid + 1, y, RR );
}
}
int main ()
{
int N, M;
creat ( 1, 100000 );
while ( scanf ( "%d%d", &N, &M ) == 2 ) {
for ( int i = 1; i <= N; ++ i ) scanf ( "%d", &v[i] );
cr ( 1, N );
while ( M -- ) {
char s[5];
int x, y, val;
scanf ( "%s", s );
switch ( s[0] ) {
case 'Q':
scanf ( "%d%d", &x, &y );
printf ( "%lld\n", q ( x, y ) );
break;
case 'C':
scanf ( "%d%d%d", &x, &y, &val );
md ( x, y, val );
}
}
}
return 0;
}
</pre>
/*
Mail to : miyubai@gamil.com
My Blog : www.baiyun.me
Link : http://www.cnblogs.com/MiYu || http://www.cppblog.com/MiYu
Author By : MiYu
Test : 1
Complier : g++ mingw32-3.4.2
Program :
Doc Name :
*/
//#pragma warning( disable:4789 )
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include
<map>
#include <utility>
#include <queue>
#include <stack>
#include
<list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
inline bool scan_d(int &num) //整数输入
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
struct seg_tree{
int l,r,val;
long long sum;
int mid () { return (r+l) >> 1; }
int dis () { return r-l+1; }
}seg[600000];
int v[100010];
void creat ( int x, int y, int rt = 1 ) {
seg[rt].l = x;
seg[rt].r = y;
seg[rt].val = 0;
if ( x == y ) {
return ;
}
int mid = seg[rt].mid();
creat ( x, mid, rt << 1 );
creat ( mid + 1, y, rt << 1 | 1 );
}
long long cr ( int x, int y, int rt = 1 ) {
seg[rt].val = 0;
if ( seg[rt].l == seg[rt].r ) {
seg[rt].sum = v[x];
return v[x];
}
int mid = seg[rt].mid();
return seg[rt].sum = cr ( x, mid, rt << 1 )
+ cr ( mid + 1, y, rt << 1 | 1 );
}
inline void pushdown ( int rt ) {
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
if ( seg[rt].val ) {
seg[LL].sum += (long long)seg[LL].dis() * seg[rt].val;
seg[RR].sum += (long long)seg[RR].dis() * seg[rt].val;
seg[LL].val += seg[rt].val;
seg[RR].val += seg[rt].val;
seg[rt].val = 0;
}
}
void md ( int x, int y, int val, int rt = 1 ) {
if ( seg[rt].l == x && seg[rt].r == y ) {
seg[rt].val += val;
seg[rt].sum += (long long)seg[rt].dis() * val;
return;
}
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
pushdown ( rt );
if ( x > mid ) md ( x, y, val, RR );
else if ( y <= mid ) md ( x, y, val, LL );
else {
md ( x, mid, val, LL );
md ( mid+1, y, val, RR );
}
seg[rt].sum = seg[LL].sum + seg[RR].sum;
}
long long q ( int x, int y, int rt = 1 ) {
if ( seg[rt].l == x && seg[rt].r == y ) {
return seg[rt].sum;
}
pushdown ( rt );
int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();
if ( x > mid ) return q ( x, y, RR );
else if ( y <= mid ) return q ( x, y, LL );
else {
return q ( x, mid, LL ) + q ( mid + 1, y, RR );
}
}
int main ()
{
int N, M;
creat ( 1, 100000 );
while ( scanf ( "%d%d", &N, &M ) == 2 ) {
for ( int i = 1; i <= N; ++ i ) scanf ( "%d", &v[i] );
cr ( 1, N );
while ( M -- ) {
char s[5];
int x, y, val;
scanf ( "%s", s );
switch ( s[0] ) {
case 'Q':
scanf ( "%d%d", &x, &y );
printf ( "%lld\n", q ( x, y ) );
break;
case 'C':
scanf ( "%d%d%d", &x, &y, &val );
md ( x, y, val );
}
}
}
return 0;
}
</pre>