bzoj 3343 分块
因为询问比较少,所以我们可以将n个数分成sqrt(n)个块,每个块用一颗bst存一下,然后对于修改l,r,我们将l,r区间中整块的直接在bst上打一个标签,对于不是整块的我们直接暴力修改,对于询问l,r,仍然是整块的直接在bst中求大于c的个数(考虑标签),然后不是整块的部分暴力扫一遍更新答案。
我写的sbt,可能是sbt常数比较大什么的,tle了,其实对于每一个块我们可以快排保存,然后询问的时候二分就好了。然后对于不是整块的修改和询问可以直接改完之后再快排,这样常数小了很多。
懒得改了,贴sbt的吧。
/**************************************************************
Problem: 3343
User: BLADEVIL
Language: C++
Result: Accepted
Time:9976 ms
Memory:47712 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std ;
#define MAXN 2000100
#define MAXB 1100
#define L( t ) Left[ t ]
#define R( t ) Right[ t ]
#define K( t ) Key[ t ]
#define S( t ) Size[ t ]
#define update( t )S( t )=S(L( t ))+S(R( t ))+1
#define check( ch )( ch >='0'&& ch <='9')
void getint(int&t ){
int ch ;for( ch =getchar( );!check( ch ); ch =getchar( ));
t = ch -'';
for( ch =getchar( );check( ch ); ch =getchar( )) t *=, t += ch -'';
}
int Left[ MAXN ], Right[ MAXN ], Key[ MAXN ], Size[ MAXN ], V =;
void left(int&t ){
int k =R( t );
R( t )=L( k );update( t );
L( k )= t ;update( k );
t = k ;
}
void right(int&t ){
int k =L( t );
L( t )=R( k );update( t );
R( k )= t ;update( k );
t = k ;
}
void maintain(int&t ){
if(S(L(L( t )))>S(R( t ))){
right( t );
maintain(R( t ));maintain( t );
return;
}
if(S(R(R( t )))>S(L( t ))){
left( t );
maintain(L( t ));maintain( t );
return;
}
if(S(R(L( t )))>S(R( t ))){
left(L( t ));right( t );
maintain(L( t )),maintain(R( t ));
maintain( t );
return;
}
if(S(L(R( t )))>S(L( t ))){
right(R( t ));left( t );
maintain(L( t )),maintain(R( t ));
maintain( t );
return;
}
}
void Insert(int k ,int&t ){
if(! t ){
t =++ V ;
L( t )=R( t )=;
S( t )=,K( t )= k ;
return;
}
S( t )++;
Insert( k , k <K( t )?L( t ):R( t ));
maintain( t );
} void Delete(int k ,int&t ){
if( k ==K( t )){
if(!L( t )){
t =R( t );return;
}
if(!R( t )){
t =L( t );return;
}
right( t );Delete( k ,R( t ));
}else Delete( k , k <K( t )?L( t ):R( t ));
update( t );
maintain( t );
} int Rank(int k ,int t ){
if(! t )return ;
if( k <=K( t ))return S(R( t ))++Rank( k ,L( t ));
return Rank( k ,R( t ));
}
int a[ MAXN ], block[ MAXN ], head[ MAXB ], tail[ MAXB ], roof[ MAXB ], C[ MAXB ];
int n , m , b =;
int Query(int l ,int r ,int c ){
if( block[ l ]== block[ r ]){
int x = block[ l ];
if( head[ x ]== l && tail[ x ]== r )return Rank( c - C[ x ], roof[ x ]);
int cnt =;
for(int i = l ; i <= r ; i ++)if( a[ i ]>= c - C[ x ]) cnt ++;
return cnt ;
}
int cnt =Query( l , tail[ block[ l ]], c )+Query( head[ block[ r ]], r , c );
for(int i = block[ l ]+; i < block[ r ]; i ++){
cnt +=Rank( c - C[ i ], roof[ i ]);
}
return cnt ;
} void Change(int l ,int r ,int c ){
if( block[ l ]== block[ r ]){
int x = block[ l ];
if( l == head[ x ]&& r == tail[ x ]){
C[ x ]+= c ;
return;
}
for(int i = l ; i <= r ; i ++){
Delete( a[ i ], roof[ x ]);
a[ i ]+= c ;
Insert( a[ i ], roof[ x ]);
}
return;
}
Change( l , tail[ block[ l ]], c ),Change( head[ block[ r ]], r , c );
for(int i = block[ l ]+; i < block[ r ]; i ++){
C[ i ]+= c ;
}
} int main( ){
memset( roof ,,sizeof( roof ));
memset( C ,,sizeof( C ));
L()=R()=S()=;
getint( n ),getint( m );
for(int i =; i ++< n ;)getint( a[ i ]);
b =int(sqrt( n ));
for(int i =; i ++< b ;){
for(int j =( i -)* b ; j ++< i * b ;){
block[ j ]= i ;
Insert( a[ j ], roof[ i ]);
}
head[ i ]=( i -)* b +, tail[ i ]= i * b ;
}
if( b * b < n ){
head[ b +]= b * b +, tail[ b +]= n ;
for(int i = b * b ; i ++< n ;){
block[ i ]= b +;
Insert( a[ i ], roof[ b +]);
}
}
while( m --){
int ch , l , r , c ;
for( ch =getchar( );!( ch >='A'&& ch <='Z'); ch =getchar( ));
if( ch =='M'){
getint( l ),getint( r ),getint( c );
Change( l , r , c );
}else{
getint( l ),getint( r ),getint( c );
printf("%d\n",Query( l , r , c ));
}
}
return ;
}
bzoj 3343 分块的更多相关文章
- BZOJ 3343: 教主的魔法(分块+二分查找)
BZOJ 3343: 教主的魔法(分块+二分查找) 3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1172 Solved: ...
- BZOJ 3343: 教主的魔法 [分块]【学习笔记】
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1172 Solved: 526[Submit][Status][Discus ...
- 【BZOJ 3343 】 分块
3343: 教主的魔法 Description 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1. ...
- 分块+二分 BZOJ 3343
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1312 Solved: 585[Submit][Status][Discus ...
- BZOJ 3343:教主的魔法(分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3343 [题目大意] 给出一个数列,有区间加法操作,询问区间大于等于c的数字个数 [题解 ...
- Bzoj 3343: 教主的魔法(分块+二分答案)
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MB Description 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息 ...
- Bzoj 3343: 教主的魔法 分块,二分
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 364[Submit][Status][Discuss ...
- BZOJ 3343 教主的魔法(分块)
题意: 有一个1e6的数组,t次操作:将[l,r]内的值增加w,或者查询[l,r]内的值大于等于add的 思路: 分块,块大小为sqrt(n),每次只需要暴力头尾两块,中间的整块打标记, 对于查询查操 ...
- bzoj 3343 教主的魔法 分块
修改直接对整块打标记,两边暴力. 查询需要保证每个整块有序,所以在修改时排序就好啦 #include<cstdio> #include<cstring> #include< ...
随机推荐
- Qt Creator子目录项目-类似VS解决方案
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt Creator子目录项目-类似VS解决方案 本文地址:http://techie ...
- HDU 2161 Primes
http://acm.hdu.edu.cn/showproblem.php?pid=2161 Problem Description Write a program to read in a list ...
- 【转】关于增量链接(incremental linking)
增量链接(Incremental Linking)这个词语在使用Visual C++时经常会遇到(其实不只是VS系列,其它链接器也有这个特性), 就比如经常遇到的:上一个增量链接没有生成它, 正在执行 ...
- Atom IDE开发工具, ASCII艺术评论, ninimap 插件
1 ASCII Art Comments One neat trick is to use ASCII art to create huge comments visible in the minim ...
- 第30天:DOM对象操作
JS包括三部分:ECMAscript.DOM(文档对象).BOM(浏览器对象) 一.DOM(文档对象)DOM树节点(元素.属性.标签.标记等都是节点) 二.访问节点 documment.getElem ...
- hdu3625-Rooms
题目 有\(n\)个房间,\(n\)个钥匙,每个钥匙随机出现在一个房间里,一个房间里有且仅有一个钥匙.我们现在手上没有钥匙,但我们要搜索所有的房间,所以我们有\(k\)次机会把一个房间炸开.一号房间里 ...
- BZOJ 2306 幸福路径(DP)
题解来源:http://www.cnblogs.com/jianglangcaijin/p/3799494.html 最后必然是走了一条链,或者是一个环(一直绕),或者是一条链加一个环.设f[i][j ...
- BZOJ5301:[CQOI2018]异或序列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5301 https://www.luogu.org/problemnew/show/P4462 已知 ...
- BZOJ4539 [Hnoi2016]树 【倍增 + 主席树】
题目链接 BZOJ4539 题解 我们把每次复制出来的树看做一个点,那么大树实际上也就是一棵\(O(M)\)个点的树 所以我们只需求两遍树上距离: 大树上求距离,进入同一个点后在模板树上再求一次距离 ...
- BZOJ 1085 [SCOI2005]骑士精神 【A*启发式搜索】
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2838 Solved: 1663 [Submit][St ...