P4147 玉蟾宫
题目给出的矩阵内的若干个子矩阵,在满足同列的情况下,可以用单调栈维护和统计子矩阵最大面积,需要预处理每个数据的行左右边界,也就是子矩阵的宽,然后我们通过枚举每一列就可以得出答案。
代码如下:
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e3+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,m,f[N][N],ans;
char a[N][N],b[N][N];
int s[N],h[N],top;
signed main() {
fast();
cin>>n>>m;
rep(i,1,n)rep(j,1,m)cin>>a[i][j];
rep(i,1,n)rep(j,1,m) if(a[i][j]=='F')f[i][j]=f[i][j-1]+1;
rep(j,1,m) {
top=1;
s[top]=1;
h[top]=f[1][j];
int sum=0;
rep(i,2,n) {
int l=0;
while(top&&h[top]>=f[i][j]) {
l+=s[top];
sum=max(sum,h[top]*l);
top--;
}
s[++top]=l+1,h[top]=f[i][j];
}
int l=0;
while(top) {
l+=s[top];
sum=max(sum,h[top]*l);
top--;
}
ans=max(ans,sum);
}
cout<<ans*3;
return 0;
}P8818 [CSP-S 2022] 策略游戏
- 小Q的策略:
-
当A[x] > 0时:选择B的最小值使乘积最小
-
当A[x] < 0时:选择B的最大值使乘积最小
-
当A[x] = 0时:乘积恒为0
- 小L的策略:
-
需要选择x使得在小Q的最优选择下,得分尽可能大
-
需考虑A中正数、负数和零的不同情况
那就可以用稀疏表(ST)预处理A的正数最大值和最小值、 A的负数最大值和最小值、B的最大值和最小值。
对于每次查询,分类讨论求解,对所有存在的解求最大值即可。
#include<bits/stdc++.h>
#define int long long
#define I_love_Foccarus return
#define in(a) a = read_int()
#define inp(a,b) a = read_int(), b = read_int()
#define inl(a) a = read_long_long()
using namespace std;
const int Size = 1 << 14;
const int N = 1e5 + 5;
const long long inf = 1e18;
inline int read_int() {
int x = 0;
char ch = getchar();
bool f = 0;
while('9' < ch || ch < '0') f |= ch == '-', ch = getchar();
while('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
I_love_Foccarus f ? -x : x;
}
int lg[N], a[N], b[N];
int st[6][21][N];
int z[N];
int query(int l, int r, int v, bool is_max) {
int k = lg[r - l + 1];
if (is_max) {
return max(st[v][k][l], st[v][k][r - (1 << k) + 1]);
} else {
return min(st[v][k][l], st[v][k][r - (1 << k) + 1]);
}
}
bool Z(int l, int r) {
return z[r] - z[l - 1] > 0;
}
signed main() {
int n, m, q;
in(n); in(m); in(q);
for(int i = 2; i < N; i++) lg[i] = lg[i >> 1] + 1;
for(int i = 1; i <= n; i++) {
in(a[i]);
if(a[i] >= 0) {
st[0][0][i] = st[1][0][i] = a[i];
st[2][0][i] = -inf , st[3][0][i] = inf;
} else {
st[0][0][i] = -inf , st[1][0][i] = inf;
st[2][0][i] = st[3][0][i] = a[i];
}
z[i] = z[i - 1] + (a[i] == 0);
}
for(int i = 1; i <= m; i++) {
in(b[i]);
st[4][0][i] = st[5][0][i] = b[i];
}
for(int k = 1; k <= lg[n]; k++) {
for(int j = 1; j + (1 << k) - 1 <= n; j++) {
st[0][k][j] = max(st[0][k-1][j], st[0][k-1][j + (1 << (k-1))]);
st[1][k][j] = min(st[1][k-1][j], st[1][k-1][j + (1 << (k-1))]);
st[2][k][j] = max(st[2][k-1][j], st[2][k-1][j + (1 << (k-1))]);
st[3][k][j] = min(st[3][k-1][j], st[3][k-1][j + (1 << (k-1))]);
}
for(int j = 1; j + (1 << k) - 1 <= m; j++) {
st[4][k][j] = max(st[4][k-1][j], st[4][k-1][j + (1 << (k-1))]);
st[5][k][j] = min(st[5][k-1][j], st[5][k-1][j + (1 << (k-1))]);
}
}
while(q--) {
int l1, r1, l2, r2;
in(l1); in(r1); in(l2); in(r2);
int maxa1 = query(l1, r1, 0, true);
int mina1 = query(l1, r1, 1, false);
int maxa2 = query(l1, r1, 2, true);
int mina2 = query(l1, r1, 3, false);
int minb = query(l2, r2, 5, false);
int maxb = query(l2, r2, 4, true);
bool p = Z(l1, r1);
int ans = -inf;
if(maxa1 != -inf) ans = max(ans, maxa1 * minb);
if(mina1 != inf) ans = max(ans, mina1 * minb);
if(maxa2 != -inf) ans = max(ans, maxa2 * maxb);
if(mina2 != inf) ans = max(ans, mina2 * maxb);
if(p) ans = max(ans, 0LL);
cout<<ans<<'\n';
}
I_love_Foccarus 0;
}Cow Sorting
说白了就是给逆序对加了权,求序列的总权。
那就是开两个 BIT,一个求逆序对,一个对逆序对的权值。
统计答案时,就是拿逆序对数乘上当前枚举的数再减去与自己逆序的所有数的总和(雾)。
然后就做完了。
#include<bits/stdc++.h>
#define int long long
#define I_love_Foccarus return
#define in(a) a = read_int()
using namespace std;
const int N = 1e5 + 5;
const int inf = INT_MAX;
inline int read_int() {
int x = 0;
char ch = getchar();
bool f = 0;
while('9' < ch || ch < '0') f |= ch == '-' , ch = getchar();
while('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0' , ch = getchar();
I_love_Foccarus f ? -x : x;
}
int a[N];
int n;
struct BIT {
int c[N] = {};
void change(int x , int v) {
for(int i = x ; i <= N - 5 ; i += (i & -i)) {
c[i] += v;
}
}
int query(int x) {
int ans = 0;
for(int i = x ; i ; i -= (i & -i)) {
ans += c[i];
}
return ans;
}
}u , v;
signed main() {
ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i ++) cin>>a[i];
int ans = 0;
for(int i = 1 ; i <= n ; i ++) {
ans += (u.query(N - 5) - u.query(a[i])) * a[i] + (v.query(N - 5) - v.query(a[i]));
u.change(a[i] , 1);
v.change(a[i] , a[i]);
}
cout<<ans<<'\n';
I_love_Foccarus 0;
}CF1311F Moving Points
因为 t 为小数,所以对于 ,只有满足 且 两人不是背向而行,就一定有解,贡献为 。所以只需要枚举所以背向而行的和速度一致的组合即可。
数据较大,需要离散化,我们按 升序排序,以 为值域建立两个权值 BIT,一个记录数量,一个记录 的前缀。
很明显,对于 ,一定满足 ,也就是 永远追不上 ,对其求一遍贡献即可。
和上面那题一样,开两个 BIT,一个存数量,一个存权值。
拿数量乘以当前枚举的速度减去比它小的速度的所有 的下标和。
然后就做完了qwq
#include<bits/stdc++.h>
#define int long long
#define I_love_Foccarus return
#define cin_fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define endl '\n'
//#define getchar getc
#define pii pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pd(a) push_back(a)
#define inui(a) a = read_unsigned_int()
#define in(a) a = read_int()
#define inp(a , b) a = read_int() , b = read_int()
#define inl(a) a = read_long_long()
using namespace std;
const int Size = 1 << 14;
const int N = 2e5 + 5;
const int inf = INT_MAX;
inline char getc() {
static char syn[Size] , *begin = syn , *end = syn;
if(begin == end) begin = syn , end = syn + fread(syn , 1 , Size , stdin);
I_love_Foccarus *begin ++;
}
inline int read_int() {
int x = 0;
char ch = getchar();
bool f = 0;
while('9' < ch || ch < '0') f |= ch == '-' , ch = getchar();
while('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0' , ch = getchar();
I_love_Foccarus f ? -x : x;
}
int n , ans;
pair<int , int>p[N];
int a[N] , tot;
#define lb(x) (x & -x)
struct BIT {
int b[N]= {};
void change(int x , int v) {
for(int i = x ; i <= n ; i += lb(i)) b[i] += v;
}
int query(int x) {
int ans = 0;
for(int i = x ; i > 0 ; i -= lb(i)) ans += b[i];
return ans;
}
} u , v;
bool cmp(pair<int , int> x , pair<int , int> y) {
if(x.se == y.se) return x.fi < y.fi;
return x.se < y.se;
}
signed main() {
in(n);
for(int i = 1 ; i <= n ; i ++) in(p[i].fi) , a[++ tot] = p[i].fi;
for(int i = 1 ; i <= n ; i ++) in(p[i].se);
sort(a + 1 , a + tot + 1);
tot = unique(a + 1 , a + tot + 1) - a - 1;
for(int i = 1 ; i <= n ; i ++) {
p[i].fi = lower_bound(a + 1 , a + tot + 1 , p[i].fi) - a;
}
sort(p + 1 , p + n + 1 , cmp);
for(int i = 1 ; i <= n ; i ++) {
ans += v.query(p[i].fi) * a[p[i].fi] - u.query(p[i].fi);
u.change(p[i].fi , a[p[i].fi]) , v.change(p[i].fi , 1);
}
cout<<ans<<" ";
I_love_Foccarus 0;
}