题目传送门
思路
题目中 最大能达到 ,且时限仅 ,必须使用线性 做法,那么考虑单调栈模拟。
设全局最大值 出现在位置 。
若存在某个 ,满足 :
- M 在 前面,,构成一组逆序对
- M 不在 紧邻的连续大数段里
- 这组逆序对不会被计入弹栈次数 于是 ,不满足条件。 反过来:全程所有数字都比之前全局最大值更大:
- 没有任何逆序对
- 栈不会弹出任何元素 满足 。
只要任何位置出现数字小于目前全局最大值且不是紧跟在最大值后面,直接不合法。
代码
AC Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define endl '\n'
inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
const int N = 3e7 + 5;
struct IO {
#define mxsz (1 << 21)
char buf[mxsz], * p1, * p2;
IO() : p1(buf), p2(buf) {}
inline char gc() {
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, mxsz, stdin);
return p1 == p2 ? ' ' : *p1++;
}
inline int read() {
int r = 0; char c = gc(); bool rev = 0;
while (c < '0' || c > '9') rev |= (c == '-'), c = gc();
while (c >= '0' && c <= '9') r = r * 10 + (c ^ 48), c = gc();
return rev ? ~r + 1 : r;
}
} io;
ll a[N];
const int mod = 993244853;
Modint<mod> res = 0;
Modint<mod> pow131(Modint<mod> n) {
return Modint<mod>(131) ^ n.val;
}
int i = 1;
stack<ll> st;
Modint<mod> solve() {
int n;
n = io.read();
Modint<mod> ans = 65537;
ll maxn = -1, cur = 0;
rep(i, 1, n) {
a[i] = io.read();
a[i] += cur;
if (maxn > a[i]) {
ans = 13579;
}
while (!st.empty() && st.top() > a[i]) {
maxn = max(maxn, st.top());
st.pop();
}
st.push(a[i]);
cur = a[i];
}
return ans;
}
int main() {
fast_io();
int t;
t = io.read();
while (t--) {
while (st.size()) st.pop();
res += pow131(i++) * solve();
}
cout << res;
return 0;
}代码中使用了自定义的 Modint 用于快速取模运算,具体请见这里。
复杂度分析
- 时间复杂度为 ,也可写作 。
- 空间复杂度为 。