codeforces 549F Yura and Developers
题意
给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数。
题解
分治,对于一个区间,找出最大值之后,分成两个区间。
至于统计答案,可以枚举小的那一端。
也可以结合熟练剖分的思想,由于dfs解决答案的过程是一棵二叉树,所以用全局变量保存当前信息,先做重儿子即可。
代码
(O(nlog_2n))
PS:由于搜索树是二叉树,所以可以直接用全局变量维护当前处理区间的信息。
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "
"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 303030, M = 1010101;
int n, k;
int a[N], f[22][N], b[M], g[N];
ll ans;
ll s[N];
inline int Max(int i, int j) {
return a[i] > a[j] ? i : j;
}
inline int st(int l, int r) {
int _ = log2(r-l+1);
return Max(f[_][l], f[_][r-(1<<_)+1]);
}
inline void upd(int p, int c) {
b[g[p]] += c;
}
void solve(int l, int r) {
if(l>=r) {
if(l==r) upd(l, -1), ++ans;
return ;
}
int mid = st(l, r);
int l1 = l-1, r1 = mid-1;
int l2 = mid, r2 = r;
if(r1-l1 < r2-l2) {
rep(i, l, mid) upd(i, -1);
rep(i, l1, r1+1) {
ans += b[(s[i]+a[mid])%k];
}
upd(mid, -1);
solve(mid+1, r);
rep(i, l, mid) upd(i, 1);
solve(l, mid-1);
} else {
rep(i, mid, r+1) upd(i, -1);
upd(l-1, 1);
rep(i, l2, r2+1) {
ans += b[(s[i]-a[mid])%k];
}
upd(l-1, -1);
solve(l, mid-1);
rep(i, mid+1, r+1) upd(i, 1);
solve(mid+1, r);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
rep(i, 1, n+1) cin >> a[i], s[i] = s[i-1] + a[i], f[0][i] = i, g[i] = s[i]%k, upd(i, 1);
for(int i = 1; (1<<i) <= n; ++i) {
for(int j = 1; j+(1<<i)-1 <= n; ++j) {
f[i][j] = Max(f[i-1][j], f[i-1][j+(1<<(i-1))]);
}
}
solve(1, n);
cout << ans - n << endl;
return 0;
}
(O(nlog_2^2n))
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "
"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 303030, M = 1010101;
int n, k;
int a[N], pr[N], ne[N];
ll s[N];
vi b[M];
pii e[N];
inline int qry(int l, int r, int x) {
int res = upper_bound(all(b[x]), r) - upper_bound(all(b[x]), l-1);
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
b[0].pb(0);
rep(i, 1, n+1) cin >> a[i], s[i] = s[i-1] + a[i], b[s[i]%k].pb(i), e[i] = mp(a[i], i);
sort(e+1, e+1+n);
rep(i, 1, n+1) pr[i] = i-1, ne[i] = i+1;
ll ans = 0;
rep(_, 1, n+1) {
int i = e[_].se;
int l = pr[i]+1, r = ne[i]-1;
int l1 = l-1, r1 = i-1;
int l2 = i, r2 = r;
if(r1-l1 < r2-l2) {
rep(j, l1, r1+1) {
ans += qry(l2, r2, (s[j]+a[i])%k);
}
} else {
rep(j, l2, r2+1) {
ans += qry(l1, r1, (s[j]-a[i])%k);
}
}
pr[ne[i]] = pr[i];
ne[pr[i]] = ne[i];
}
cout << ans - n << endl;
return 0;
}