---
$1.$ 暴力。
这里主要教大家卡常。
---
$1.$吸氧。
$2.$开 `c++98`,开寄存器。如:`for(register int i = 1;i <= n;i ++)`,快很多( `c++11` 寄存器变量就形同虚设了) 。
$3.$`i++` 改成 `++i`,快一些。
$4.$`x%2` 等同于 `x&1`,`x*2` 等同于 `x<<1`,`x/2` 等价于 `x>>1`;当 $x$ 为 $0$ 或 $1$ 时,`1-x` 等价于 `x^1`(`^`是异或)。
$5.$可以把一个循环 `F(name=i,range1=1,range2=1000,add1)cnt+=i;` 搞成`F(i,1,i+3<=1000,add4)cnt+=i;cnt+=i+1;cnt+=i+2;cnt+=i+3;` 快很多!
$6.$有些时候,如果 `map` 不够快,请用 `undordered_map`,用法与 `map` 大体类似,但是效率可能更高一些(如果出题人卡哈希的话,就悲剧了,不过一般来说不大可能)
$7.$`x % (2^k)` 等价于 `x & (2^k-1)`。
$8.$把模数设置为常数,用 `const` 修饰,快很多。(如果是字符串哈希,强烈建议使用 $19260817$,而不是自然溢出)。
$9.$`memset` 与 `memcpy` 通常比循环快。
$10.$有神一般的快读快出:
```cpp
namespace fast_io {
inline char read(){static const int IN_LEN=1000000;static char buf[IN_LEN],*s,*t;return s==t?(((t=(s=buf)+fread(buf,1,IN_LEN,stdin))==s)?-1:*s++) : *s++;}
inline void read(int &x){static bool iosig;static char c;for (iosig=false,c=read();!isdigit(c);c=read()){if(c=='-')iosig=true;if(c==-1)return;}for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');if(iosig)x=-x;}
inline void read(char *a){static char c = read();while(c!=-1&&(c==' '||c=='\n'||c=='\r'))c=read();while(c!=-1&&c!='\r'&&c!=' '&&c!='\n') *a++=c,c=read();*a=0;}
const int OUT_LEN=1000000;char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c){if(ooh==obuf+OUT_LEN) fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;*ooh++ = c;}
inline void print(int x){static int buf[30],cnt;if(x==0)print('0');else{if(x<0)print('-'),x=-x;for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;while (cnt) print((char)buf[cnt--]);}}
inline void print(char *a){while(*a) print(*a++);}inline void print(const char *a){while(*a) print(*a++);}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}using namespace fast_io;
```
读入 $10^7$ 个整数几乎不需要时间!
---
实战演练。[教主的魔法](https://www.luogu.com.cn/problem/P2801)这题,请读者拿到 $90$ 分!
参考代码(~~就差 $0.01$ 秒,死活卡不过去~~):
(感谢@tianhangj 修正,现在可以卡过去[$\text\color{red}{666}$](https://www.luogu.com.cn/record/78149875))
```cpp
#include <bits/stdc++.h>
//O2!O2!
#define R register
#define inl inline
#define fastios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define Debug(file) freopen(file".in","r",stdin);freopen(file".out","w",stdout);
/*洛谷万岁*/
using namespace std;
const int N = 1e6 + 1;//定义常数
int* a = new int[N];//指针数组
int main() {
R int n,q,t,ans,tot = 0;//寄存器变量
fastios;//cin/cout 关闭同步,快很多
cin >> n >> q;
for(R int i = 1;i + 3 <= n;i += 4){//寄存器变量
cin >> a[i] >> a[i+1] >> a[i+2] >> a[i+3];t = i+3;//循环 4 次展开
}
for(++t;t <= n;++ t)cin >> a[t];//不足 4 次,暴力读入
while(q--){
R char op;R int l,r,c;//寄存器变量
cin >> op >> l >> r >> c;t = l - 1;//读入
if(op == 'M'){
for(R int i = l;i + 3 <= r;i += 4){
a[i] += c;a[i+1] += c;a[i+2] += c;a[i+3] += c;t = i + 3;//循环展开
}
for(++t;t <= r;++ t)a[t] += c;
}
if(op == 'A'){
ans = 0;
for(R int i = l;i + 3 <= r;i += 4){
t = i + 3;
ans += (a[i]>=c)+(a[i+1]>=c)+(a[i+2]>=c)+(a[i+3]>=c);
//循环展开
}
for(++t;t <= r;++ t)ans += (a[t] >= c);
cout << ans << endl;
}
}
return 0;
}
```
$\Huge\color{blue}\text{azaz,卡过去了}
#include <cstdio>
unsigned* a = new unsigned[1000005];
unsigned read() {
unsigned x = 0;
char c = getchar();
while ( c < '0' || c > '9' ) {
c = getchar();
}
while ( c >= '0' && c <= '9' ) {
x = x * 10 + ( c - '0' );
c = getchar();
}
return x;
}
int main() {
register int n, q, t, ans;
register char op;
register int l, r, c;
register int i;
n = read(), q = read();
for ( i = 1 ; i <= n ; ++i ) {
a[i] = read();
}
while ( q-- ) {
scanf( " %c", &op );
l = read(), r = read(), c = read();
t = l - 1;
r -= 3;
if ( op & 4 ) {
for ( i = l; i <= r; i += 4 ) {
a[i] += c;
a[i + 1] += c;
a[i + 2] += c;
a[i + 3] += c;
t = i + 3;
}
r += 3;
for ( ++t; t <= r; ++t ) {
a[t] += c;
}
} else {
ans = 0;
for ( i = l; i <= r; i += 4 ) {
t = i + 3;
ans += int( a[i] >= c ) + int( a[i + 1] >= c ) + int( a[i + 2] >= c ) + int( a[i + 3] >= c );
}
r += 3;
for ( ++t; t <= r; ++t )
ans += int( a[t] >= c );
printf( "%d\n", ans );
}
}
}
还有,非常男女,请 AC
参考代码:
#include <bits/stdc++.h>
#define R register
#define inl inline
#define fastios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define Debug(file) freopen(file".in","r",stdin);freopen(file".out","w",stdout);
using namespace std;
int n,s[100000 + 5],ans = -(1<<30);bool a[100000 + 5];
int check(R int len){
if(len & 1)return false;if(! len)return true;//位运算优化
for(R int i = 1;i + len - 1 <= n;i ++){//寄存器变量
if(len == (s[i + len - 1] - s[i - 1])*2){
return true;
}
}
return false;
}
int main() {
cin >> n;
for(R int i = 1;i <= n;i ++){
cin >> a[i];
s[i] = s[i-1] + a[i];//前缀和降维
}
for(R int i = 0;i <= n;i ++)if(check(i))ans = i;
cout << ans << endl;
return 0;
}
懒得码字了,看我 P2797 题解吧,[gate](https://www.luogu.com.cn/blog/302394/solution-c)。
(虽然是错解,但是还 AC 了是不是,管理别撤啊……QwQ)
$3.$假·数据结构
这里仅举假线段树一例。[教主的魔法](https://www.luogu.com.cn/problem/P2801),没错,又是它!
这里,我们发现,如果区间的最小值都大于等于 $c$,则其整体都满足要求。
```cpp
#include <bits/stdc++.h>
#define R register
#define inl inline
#define fastios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define Debug(file) freopen(file".in","r",stdin);freopen(file".out","w",stdout);
/*洛谷万岁*/
using namespace std;
const int N = 1e6 + 5;
int *a = new int[N];
int n, m;
struct node {
int l, r;
int minn, add;
} tr[N * 4];
void pushup(int u) {
tr[u].minn = min(tr[u << 1].minn, tr[u << 1 | 1].minn);
}
void pushdown(int u) {
node &l = tr[u << 1];
node &r = tr[u << 1 | 1];
l.add += tr[u].add;
l.minn += tr[u].add;
r.add += tr[u].add;
r.minn += tr[u].add;
tr[u].add = 0;
}
void build(int u, int l, int r) {
if (l == r)
tr[u] = {l, r, a[l], 0};
else {
tr[u] = {l, r, 0, 0};
const int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u, int l, int r, int v) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].add += v;
tr[u].minn += v;
} else {
pushdown(u);
const int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
modify(u << 1, l, r, v);
if (r > mid)
modify(u << 1 | 1, l, r, v);
pushup(u);
}
}
int query(int u, int l, int r, int c) {
if(tr[u].l==tr[u].r)return tr[u].minn>=c;
//printf("Debug:u=%d,l=%d,r=%d,c=%d\n", u, tr[u].l, tr[u].r, c);
if (l <= tr[u].l && tr[u].r <= r && tr[u].minn >= c) {
return tr[u].r - tr[u].l + 1;
} else {
const int mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
int ans = 0;
if (l <= mid)
ans += query(u << 1, l, r, c);
if (r > mid)
ans += query(u << 1 | 1, l, r, c);
return ans;
}
}
int main() {
fastios;
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
build(1, 1, n);
while (m--) {
char op;
int l, r, c;
cin >> op >> l >> r >> c;
if (op == 'M')
modify(1, l, r, c);
else
cout << query(1, l, r, c) << endl;
}
return 0;
}
```