单调栈、单调队列

contest 3

单调栈

Q:求每个数右边第一个比他大的数?

Ascending sequence

单调队列
注意不用long long 会爆精度
取模运算消耗的时间比较的多

ac code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mp make_pair
#define X first
#define Y second
const int maxn = 1e7+10;
int a[maxn];
int n, m, k, p, q, r, mod;
int A[maxn], B[maxn];
int main(){
ios::sync_with_stdio(false);
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d%d%d%d%d%d", &n, &m, &k, &p, &q, &r, &mod);
for(int i=1; i<=k; i++){
scanf("%d", &a[i]);
}
for(int i=k+1; i<=n; i++){
a[i] = (1ll*p*a[i-1]+1ll*q*i+r)%mod;
}
deque<pii> dq;
while(!dq.empty()){
dq.pop_back();
}
dq.push_back(mp(a[n], n));
for(int i=n-1; i>=n-m+1; i--){
//cout<<"keke"<<(*--dq.end()).X<<endl;
while(!dq.empty()&&(*--dq.end()).X<a[i]){
//cout<<"tets"<<(*--dq.end()).X<<" "<<a[i]<<endl;
dq.pop_back();
}
dq.push_back(mp(a[i], i));
}
// while(!dq.empty()){
// printf("%d ", dq.front());
// dq.pop_front();
// }
// printf("\n");
B[n-m+1] = dq.size();
A[n-m+1] = (*dq.begin()).X;
for(int i=n-m; i>=1; i--){
if(((*dq.begin()).Y)>m+i-1){
dq.pop_front();
}
while(!dq.empty()&&(*--dq.end()).X<=a[i]){
dq.pop_back();
}
dq.push_back(mp(a[i], i));
B[i] = dq.size();
A[i] = (*dq.begin()).X;
}
ll ans1=0, ans2 = 0;
for(int i=1; i<=n-m+1; i++){
ans1 += (ll(A[i])^(i));
ans2 += (ll(B[i])^(i));
}
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}

未解决的问题

文章目录
  1. 1. 单调栈
  2. 2. Ascending sequence
    1. 2.1. ac code
  3. 3. 未解决的问题
{{ live2d() }}