[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 比较显然的分块题,分块后块内排序,维护整块的附 ...
随机推荐
- PB笔记之数据窗口大小自适应的方式
1.在OPEN 事件中设置数据窗口大小属性 tab_1.tabpage_6.dw_6.x=0tab_1.tabpage_6.dw_6.y=0tab_1.tabpage_6.dw_6.width=thi ...
- 音量调节条-封装通用的ProgressBar组件
import React, { Component } from 'react' import PropTypes from 'prop-types' import assign from 'obje ...
- React Hooks中父组件中调用子组件方法
React Hooks中父组件中调用子组件方法 使用到的hooks-- useImperativeHandle,useRef /* child子组件 */ // https://reactjs.org ...
- Codeforces Round #419 (Div. 1) (ABCD)
1. 815A Karen and Game 大意: 给定$nm$矩阵, 每次选择一行或一列全部减$1$, 求最少次数使得矩阵全$0$ 贪心, $n>m$时每次取一列, 否则取一行 #inclu ...
- Thymeleaf模板引擎与springboot关联后,在html中无法使用el表达式获取model存的值
头部引入了thymeleaf <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thy ...
- C# ObservableCollection两个字段排序的情况
相对于System.Linq的OrderBy及OrderByDescending方法,调用后产生IOrderedEnumberable对象,这个对象为排序后的返回值,但原对象未发生变化. 试想,有这种 ...
- 【ES6】数组的扩展
1.Array.from(): 将伪数组对象和遍历的对象转为真数组 如果一个对象的键都是正整数或者0,并且有 Length属性,那么这个对象很想数组,称它为伪数组. 伪数组: let obj = { ...
- XPATH中text()和string()的使用区别
<table style="WIDTH: 95.45%; BORDER-COLLAPSE: collapse; EMPTY-CELLS: show; MARGIN-LEFT: 4.55 ...
- linux 系统运维工具13款
1. 查看进程占用带宽情况 - Nethogs Nethogs 是一个终端下的网络流量监控工具可以直观的显示每个进程占用的带宽. 下载:http://sourceforge.net/projects/ ...
- VBA字符串(十二)
字符串是一个字符序列,可以由字母,数字,特殊字符或全部字符组成. 如果一个变量被包含在双引号""中,则被认为是一个字符串. 语法 variable_name = "thi ...