[Luogu] 教主的魔法
https://www.luogu.org/problemnew/show/P2801
分块
对于每一块进行排序存储在另一个数组中
二分查询
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath> using namespace std; #define gc getchar() const int N = 1e6 + ;
const int M = 1e3 + ; int A[N], B[N], Pos[N], Add[M];
int n, Q, cnt, block; inline int read(){
int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x * f;
} inline void Sort(int x){
int l = (x - ) * block + , r = min(n, x * block);
for(int i = l; i <= r; i ++) B[i] = A[i];
sort(B + l, B + r + );
} inline void Sec_G(int x, int y, int z){
if(Pos[x] == Pos[y]){for(int i = x; i <= y; i ++) A[i] += z; Sort(Pos[x]); return ;}
else {
for(int i = x; i <= Pos[x] * block; i ++) A[i] += z;
for(int i = (Pos[y] - ) * block + ; i <= y; i ++) A[i] += z;
Sort(Pos[x]); Sort(Pos[y]);
}
for(int i = Pos[x] + ; i < Pos[y]; i ++) Add[i] += z;
} inline int to_calc(int x, int num){
int l = (x - ) * block, r = min(x * block, n), last = r, ans;
while(l <= r){
int mid = (l + r) >> ;
if(B[mid] < num) ans = mid, l = mid + ;
else r = mid - ;
}
return last - ans + ;
} inline int Sec_A(int x, int y, int z){
int answer();
if(Pos[x] == Pos[y]){for(int i = x; i <= y; i ++) if(A[i] + Add[Pos[x]] >= z) answer ++; return answer;}
else {
for(int i = x; i <= Pos[x] * block; i ++) if(A[i] + Add[Pos[x]] >= z) answer ++;
for(int i = (Pos[y] - ) * block + ; i <= y; i ++) if(A[i] + Add[Pos[y]] >= z) answer ++;
}
for(int i = Pos[x] + ; i < Pos[y]; i ++) answer += to_calc(i, z - Add[i]);
return answer;
} int main()
{
freopen("gg.in", "r", stdin);
n = read(); Q = read();
block = sqrt(n);
for(int i = ; i <= n; i ++) A[i] = read();
for(int i = ; i <= n; i ++) Pos[i] = (i - ) / block + ;
for(int i = ; i <= n; i ++) Sort(i);
if(n % block) cnt = n / block + ;
else cnt = n / block;
while(Q --){
string s; cin >> s;
int x = read(), y = read(), z = read();
if(s[] == 'A') Sec_G(x, y, z);
else cout << Sec_A(x, y, z) << endl;
} return ;
}
/*
5 3
1 2 3 4 5
A 1 5 4
M 3 5 1
A 1 5 4
*/
[Luogu] 教主的魔法的更多相关文章
- Luogu 2801 教主的魔法 | 分块模板题
Luogu 2801 教主的魔法 | 分块模板题 我犯的错误: 有一处l打成了1,还看不出来-- 缩小块大小De完bug后忘了把块大小改回去就提交--还以为自己一定能A了-- #include < ...
- luogu P2801 教主的魔法
题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是 ...
- 洛谷P2801 教主的魔法 [分块,二分答案]
题目传送门 教主的魔法 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. ...
- BZOJ3343 & 洛谷2801:教主的魔法——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 https://www.luogu.org/problemnew/show/2801 题目描述 ...
- BZOJ——3343: 教主的魔法 || 洛谷—— P2801 教主的魔法
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 || https://www.luogu.org/problem/show?pid=280 ...
- BZOJ 3343: 教主的魔法 [分块]【学习笔记】
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1172 Solved: 526[Submit][Status][Discus ...
- 【BZOJ-3343】教主的魔法 分块
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 950 Solved: 414[Submit][Status][Discuss ...
- 【BZOJ3343】教主的魔法 分块+二分
Description 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的 ...
- BZOJ3343: 教主的魔法 分块
2016-05-28 10:27:19 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3343 比较显然的分块题,分块后块内排序,维护整块的附 ...
随机推荐
- 【SCALA】3、模拟电路
Simulation package demo17 abstract class Simulation { type Action = () => Unit case class WorkIte ...
- Android--单选对话框
import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterfa ...
- AX 2009中现有量画面修改
前端时间开发一个东西,需要在现有量画面增加一个字段 但是发现这个display方法写在任何数据源下面都不行,数据取的不对. 因为InventSum这个表只有所有维度都出来时才会有对应关联的invent ...
- Hibernate持久化,生命周期
一 .生命周期 1.1 . 说明 持久化类就是我们所说的实体类,实体类(持久化类)对象是有状态的. 为什么实体类对象会有状态? 答:由于HIbernate框架是一个先映射,后操作的框架.所谓的状 ...
- 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)
原文:在论坛中出现的比较难的sql问题:16(取一个字段中的数字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 问题:取一个字段中的数字http://bbs.csdn ...
- POJ3255(Roadblocks)--次短路径
点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...
- solr的post.jar
http://iamyida.iteye.com/blog/2207920 跟益达学Solr5之玩转post.jar
- JQuery里input属性赋值,取值prop()和attr()方法?
一.赋值的时候 如果是<input type="checkbox" checked>这样的只有属性名就能生效的属性 推荐prop,即:$('input').prop(' ...
- BLE 广播格式定义
低功耗蓝牙两类报文 : 广播报文 和 数据报文. 本文讨论广播报文数据段,不包括完整报文其他部分,比如前导,接入地址等 蓝牙设备通过广播表明自己的存在,等待被连接, 就好象一个人站在接口大喊“我要脱单 ...
- 表格分页——tablePagination
背景:表格是最为通用的展示方式,为了展示的统一性,以及分页组件的重用,这里写一个分页组件,供比较多或者较少数据2种表格进行分页展示. 分页组件: <template> <el-pag ...