| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
蒟蒻给跪了,怎么写都是TLE,求大神解释一下#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <functional>
#include <iterator>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef __int64 LL;
typedef unsigned int Uint;
const int INF=0x7ffffff;
//==============struct declaration==============
//==============var declaration=================
LL addv[100000*3];
LL sum[100000*3];
int n,q;
#define y1 leftboundary
#define y2 rightboundary
int y1,y2;
LL v;
LL _sum;
LL cmd;
//==============function declaration============
void add(LL l,LL r,LL o);
void query(LL l,LL r,LL o,LL sumadd);
void maintain(LL l,LL r,LL o);
//==============main code=======================
int main()
{
#ifdef DEBUG
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d%d",&n,&q);
memset(addv,0,sizeof(addv));
memset(sum,0,sizeof(sum));
For(i,1,n)
{
scanf("%I64d",&v);
y1=y2=i;
add(1,n,1);
}
For(i,1,q)
{
cin.ignore(10,10);
scanf("%c %d %d",&cmd,&y1,&y2);
if (cmd=='Q')
{
_sum=0;
query(1,n,1,0);
printf("%I64d\n",_sum);
}
else
{
scanf("%I64d",&v);
add(1,n,1);
}
}
return 0;
}
//================fuction code====================
void maintain(LL l,LL r,LL o)
{
int m=(l+r)/2;
int lc=2*o,rc=2*o+1;
if (l<r)
{
maintain(l,m,lc);
maintain(m+1,r,rc);
sum[o]=sum[lc]+sum[rc]+addv[o]*(r-l+1);
}
else
sum[o]=addv[o];
}
void add(LL l,LL r,LL o)
{
int m=(l+r)/2;
int lc=2*o,rc=2*o+1;
if (y1<=l&&r<=y2)
addv[o]+=v;
else
{
if (m>=y1)
add(l,m,lc);
if (m<y2)
add(m+1,r,rc);
}
maintain(l,r,o);
}
void query(LL l,LL r,LL o,LL sumadd)
{
int m=(l+r)/2;
int lc=2*o,rc=2*o+1;
if (y1<=l&&r<=y2)
_sum+=(r-l+1)*(sumadd)+sum[o];
else
{
if (m>=y1)
query(l,m,lc,sumadd+addv[o]);
if (m<y2)
query(m+1,r,rc,sumadd+addv[o]);
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator