锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include <cstdio>
using namespace std;
struct CNode
{
int L, R;
CNode * pLeft, * pRight;
long long nSum, Inc;
};
CNode Tree[1000000];
int nCount = 0;
void BuildTree( CNode * pRoot, int L, int R )
{
pRoot->L = L;
pRoot->R = R;
pRoot->nSum = 0;
pRoot->Inc = 0;
if( L == R ) return;
nCount++;
pRoot->pLeft = Tree + nCount;
nCount++;
pRoot->pRight = Tree + nCount;
BuildTree( pRoot->pLeft, L, ( L + R ) / 2 );
BuildTree( pRoot->pRight, ( L + R ) / 2 + 1, R );
}
void Insert( CNode * pRoot, int i, int v )
{
if( pRoot->L == i && pRoot->R == i )
{
pRoot->nSum = v;
return;
}
pRoot->nSum += v;
if( i <= ( pRoot->L + pRoot->R ) / 2 )
Insert( pRoot->pLeft, i, v );
else
Insert( pRoot->pRight, i, v);
}
void Add( CNode * pRoot, int a, int b, long long c )
{
if( a == pRoot->L && b == pRoot->R )
{
pRoot->Inc += c;
return;
}
pRoot->nSum += ( b - a + 1 ) * c;
if( b <= ( pRoot->L + pRoot->R ) / 2)
Add( pRoot->pLeft, a, b, c );
else if ( a >= (pRoot->L + pRoot->R ) / 2 + 1 )
Add ( pRoot->pRight, a, b, c );
else
{
Add( pRoot->pLeft, a, ( pRoot->L + pRoot->R ) / 2, c );
Add( pRoot->pRight, (pRoot->L + pRoot->R ) / 2 + 1, b, c );
}
}
long long QuerynSum( CNode * pRoot, int a, int b )
{
if ( a == pRoot->L && b == pRoot->R )
return (pRoot->nSum + (pRoot->R - pRoot->L + 1 ) * pRoot->Inc);
pRoot->nSum += (pRoot->R - pRoot->L + 1 ) * pRoot->Inc;
Add( pRoot->pLeft, pRoot->L, (pRoot->L + pRoot->R ) / 2, pRoot->Inc );
Add( pRoot->pRight, (pRoot->L + pRoot->R ) / 2 + 1, pRoot->R, pRoot->Inc );
pRoot->Inc = 0;
if( b <= (pRoot->L + pRoot->R ) /2 )
return QuerynSum( pRoot->pLeft, a, b );
else if ( a >= (pRoot->L + pRoot->R ) / 2 + 1 )
return QuerynSum ( pRoot->pRight, a, b);
else
return QuerynSum( pRoot->pLeft, a, (pRoot->L +pRoot->R ) / 2 ) + QuerynSum( pRoot->pRight, (pRoot->L + pRoot->R ) / 2 + 1, b ) ;
}
int main()
{
int n,q;
scanf("%d%d", &n, &q );
nCount = 0;
BuildTree( Tree, 1, n);
int temp;
for( int i = 1; i <= n; i++ )
{
scanf("%d", &temp);
Insert( Tree, i, temp );
}
char c_temp[10];
int a, b, c;
for( int i = 0; i < q; i++ )
{
scanf("%s", c_temp);
if( c_temp[0] == 'C' )
{
scanf("%d%d%d", &a, &b, &c );
Add( Tree, a, b, c);
}
else
{
scanf("%d%d", &a, &b );
printf("%I64d\n",QuerynSum( Tree, a, b ));
}
}
return 0;
}
]]>
#include <cstdio>
#include <algorithm>
const int MY_MAX = -99999999;
const int MY_MIN = 99999999;
using namespace std;
struct CNode
{
int R, L;
int nMax, nMin;
CNode * pLeft, * pRight;
}Tree[1000000];
//CNode Tree[1000000];
int nMax, nMin;
int nCount = 0;
void BuildTree( CNode * pRoot, int L, int R )
{
pRoot->L = L;
pRoot->R = R;
pRoot->nMax = MY_MAX;
pRoot->nMin = MY_MIN;
if( R != L )
{
nCount++;
pRoot->pLeft = Tree + nCount;
nCount++;
pRoot->pRight = Tree + nCount;
BuildTree( pRoot->pLeft, L, ( L + R ) / 2 );
BuildTree( pRoot->pRight, ( L + R ) / 2 + 1, R );
}
}
void Insert( CNode * pRoot, int i, int v )
{
if( pRoot->L == i &&pRoot-> R == i )
{
pRoot-> nMin = pRoot-> nMax = v;
return ;
}
pRoot->nMin = min( pRoot->nMin,v );
pRoot->nMax = max( pRoot->nMax, v );
if( i <= ( pRoot->L + pRoot->R ) / 2 )
Insert( pRoot->pLeft, i, v );
else
Insert( pRoot->pRight, i, v );
}
void Query( CNode * pRoot, int s, int e )
{
if( pRoot->nMax <= nMax && pRoot->nMin >= nMin )
return ;
if( s == pRoot->L && e == pRoot->R )
{
nMax = max(pRoot->nMax, nMax);
nMin = min(pRoot->nMin,nMin);
return;
}
if( e <= ( pRoot->L + pRoot->R ) / 2 )
Query( pRoot->pLeft, s, e );
else if ( s >= ( pRoot->L + pRoot->R ) / 2 + 1 )
Query( pRoot->pRight, s, e );
else
{
Query( pRoot->pLeft, s, ( pRoot->L + pRoot->R ) / 2 );
Query( pRoot->pRight, ( pRoot->L + pRoot->R) / 2 + 1, e ) ;
}
}
int main()
{
int n, q, s, e;
int h;
scanf("%d%d", &n, &q);
nCount = 0;
BuildTree( Tree, 1, n);
for( int i = 1; i <= n; i++ )
{
scanf("%d", &h);
Insert( Tree, i, h );
}
for( int i = 0; i < q; i++)
{
scanf("%d%d", &s,&e );
nMax = MY_MAX;
nMin = MY_MIN;
Query( Tree, s, e );
printf("%d\n", nMax - nMin) ;
}
return 0;
}
]]>
#include <algorithm>
#include <math.h>
using namespace std;
int n;
struct CPost
{
int L,R;
};
CPost posters[10100];
int x[20200];
int hash[10000010];
struct CNode
{
int L,R;
bool bCovered; //鏈尯闂存槸鍚﹀凡緇忚瀹屽叏瑕嗙洊
CNode * pLeft, * pRight;
};
CNode Tree[100000];
int nNodeCount = 0;
int Mid( CNode * pRoot)
{
return (pRoot->L + pRoot->R)/2;
}
void BuildTree( CNode * pRoot, int L, int R)
{
pRoot->L = L;
pRoot->R = R;
pRoot->bCovered = false;
if( L == R )
return;
nNodeCount ++;
pRoot->pLeft = Tree + nNodeCount;
nNodeCount ++;
pRoot->pRight = Tree + nNodeCount;
BuildTree( pRoot->pLeft,L,(L+R)/2);
BuildTree( pRoot->pRight,(L+R)/2 + 1,R);
}
bool Post( CNode *pRoot, int L, int R)
{
if( pRoot->bCovered )
return false;
if( pRoot->L == L && pRoot->R == R) {
pRoot->bCovered = true;
return true;
}
bool bResult ;
if( R <= Mid(pRoot) )
bResult = Post( pRoot->pLeft,L,R);
else if( L >= Mid(pRoot) + 1)
bResult = Post( pRoot->pRight,L,R);
else {
bool b1 = Post(pRoot->pLeft ,L,Mid(pRoot));
bool b2 = Post( pRoot->pRight,Mid(pRoot) + 1,R);
bResult = b1 || b2;
}
//瑕佹洿鏂版牴鑺傜偣鐨勮鐩栨儏鍐?/span>
if( pRoot->pLeft->bCovered && pRoot->pRight->bCovered )
pRoot->bCovered = true;
return bResult;
}
int main()
{
int t;
int i,j,k;
scanf("%d",&t);
int nCaseNo = 0;
while(t--) {
nCaseNo ++;
scanf("%d",&n);
int nCount = 0;
for( i = 0;i < n;i ++ ) {
scanf("%d%d", & posters[i].L,& posters[i].R );
x[nCount++] = posters[i].L;
x[nCount++] = posters[i].R;
}
sort(x,x+nCount);
nCount = unique(x,x+nCount) - x; //鍘繪帀閲嶅鍏冪礌
for( i = 0;i < nCount;i ++ )
hash[x[i]] = i;
nNodeCount = 0;
BuildTree( Tree,0,nCount - 1);
int nSum = 0;
for( i = n - 1;i >= 0;i -- ) { // 浠庡悗寰鍓嶇湅鏉挎槸鍚︾湅寰楄
if( Post(Tree,hash[posters[i].L],hash[posters[i].R]))
nSum ++;
}
printf("%d\n",nSum);
}
return 0;
}
]]>