RMQ能干的,线段树凭什么不能!!
经过了无数的卡常数,终于AC了。
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m;
int w[N];
struct Node
{
int l, r;
int maxn;
}tr[N * 4];
inline void pushup(int u)
{
tr[u].maxn = max(tr[u << 1].maxn, tr[u << 1 | 1].maxn);
}
inline void build(int u, int l, int r)
{
if(l == r) tr[u] = {l, r, w[r]};
else
{
tr[u] = {l, r};
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
inline int query(int u, int l, int r)
{
if(tr[u].l >= l && tr[u].r <= r) return tr[u].maxn;
int mid = tr[u].l + tr[u].r >> 1;
int ans = -0x3f3f3f3f;
if(r <= mid) ans = max(ans, query(u << 1, l, r));
else if(l > mid) ans = max(ans, query(u << 1 | 1, l, r));
else
{
int left = query(u << 1, l, r);
int right = query(u << 1 | 1, l, r);
ans = max(ans, max(left, right));
}
return ans;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++) scanf("%d", &w[i]);
build(1, 1, n);
while(m --)
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query(1, x, y));
}
}
想问一下……
# 这些是用来干啥的?
手动开O3优化
好的,谢谢!
还有……
我线段树没超时啊……
(虽然很慢,到达了 3352 ms……)
线段树写出来为什么永远超时。。。
请问卡常数是什么意思?线段树和常数有联系吗
卡常数项时间,比如scanf和cin都是基本函数,但是他们的时间还是会不一样,他们的时间都叫常数时间
事实上这题也可以用树桩数组当然了,分块是不是也可以啊
感觉可以哈哈,这题是「一个简单的整数问题2」的简化版
嗯