Prolem A - ١١(❛ᴗ❛)【树状数组】-数星星 Stars
题意简述
给定 个点,求对于一个点有多少个点的横纵坐标都小于等于点坐标。
思路
其实可以暴力……
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e4 + 10;
int n, a[MAXN];
struct Node {
int x, y, idx;
};
Node q[MAXN];
int ans[MAXN];
bool cmp(Node x, Node y) {
if (x.x == y.x) {
return x.y < y.y;
}
return x.x < y.x;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> q[i].x >> q[i].y;
q[i].idx = i;
}
sort(q + 1, q + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
int res = 0;
for (int j = i - 1; j > 0 ; j--) {
if (q[j].y <= q[i].y) res++;
}
ans[res]++;
}
for (int i = 0; i <= n - 1; i++) {
cout << ans[i] << endl;
}
return 0;
}
Problem B - 最大值
题意简述
好像不用简化。
给定一个正整数数列 ,每一个数都在 之间。可以对这列数进行两种操作:
- 添加操作:向序列后添加一个数,(上一次询问答案),序列长度变成 ;
- 询问操作:询问这个序列中最后 个数中最大的数是多少。
程序运行的最开始,整数序列为空。写一个程序,读入操作的序列,并输出询问操作的答案。
思路
考虑分块。本题可以理解为给定一个长度为 的序列 和一个计数器 ,修改操作使得 ,查询操作查询 的最大值。
直接分块模板。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 2e5 + 10;
int m, p, cube[MAXN], a[MAXN], n = 0, cnt, len, color[MAXN];
int ask(int l, int r) {
int i = l, ans = INT_MIN;
for (; i <= r and i % len != 1; i++) {
ans = max(ans, a[i]);
}
for (int j = color[i]; j * len <= r; j++) {
i = j*len + 1;
ans = max(ans, cube[j]);
}
for (; i <= r; i++) {
ans = max(ans, a[i]);
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> m >> p;
len = sqrt(m) + 1;
for (int i = 1; i <= m; i++) {
cube[i] = INT_MIN;
color[i] = color[i - 1];
if (i % len == 1) color[i]++;
}
int last = 0;
while (m--) {
char opt;
int x;
cin >> opt >> x;
if (opt == 'A') {
n++;
a[n] = (x + last) % p;
cube[color[n]] = max(cube[color[n]], a[n]);
} else if (opt == 'Q') {
last = ask(n - x + 1, n);
cout << last << "\n";
}
}
return 0;
}Problem C - 赶路
题意简述
给定一个长度为 的序列 ,代表一些点的位置,每次可以从一个点到达所有位置差小于等于 的点,有 次询问,询问从 号点开始到 最小步数。
思路
不是我一开始还以为顺序是无序的还排序又离散化了一下。
ST 表板子题,可惜考试的时候我以为老师把二分卡了那应该也把 ST 表卡了,一直在想更优的思路。快考完的时候才想起来二分比起 ST 表有一个很大很大的常数。
本人的伟人二分:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, qwq[MAXN], q, l,flag[MAXN];
struct Node{
int x;int idx;
};
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 - '0', ch = getchar();
return x * f;
}
void write(int x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
return;
}
Node a[MAXN];
bool cmp(Node x,Node y){
return x.x<y.x;
}
signed main() {
n=read();
for (int i = 1; i <= n; i++) {
qwq[i]=read();
}
l=read();q=read();
while (q--) {
int fr, t,ans=0;
fr=read();
t=read();
int now = fr;
if(now>t) swap(now,t);
while(now<t){
ans++;
int x=lower_bound(qwq+now,qwq+1+n,qwq[now]+l) - qwq;
if(qwq[x]-qwq[now]>l) x--;
now=x;
}
write(ans);putchar('\n');
}
return 0;
}直接考虑 ST 表。定义 代表从 出发跳 步到达的最远的点。倍增初始化要注意不是下一个点而是二分找到最远的点。
int t = upper_bound(a + 1, a + 1 + n, a[i] + l) - a;
bz[i][0] = t - 1; // upper_bound 找的是第一个大于的点然后正常 ST 表初始化:
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= n; j++) {
bz[j][i] = bz[bz[j][i - 1]][i - 1];
}
}剩下的不用多说了,会 ST 表板子的都会。
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, a[MAXN], l, q;
int bz[MAXN][21];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> l >> q;
for (int i = 1; i <= n; i++) {
int t = upper_bound(a + 1, a + 1 + n, a[i] + l) - a;
bz[i][0] = t - 1;
}
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= n; j++) {
bz[j][i] = bz[bz[j][i - 1]][i - 1];
}
}
while (q--) {
int f, t;
cin >> f >> t;
int ans = 0;
if (f > t) swap(f, t);
for (int i = 20; i >= 0; i--) {
if (bz[f][i] < t) {
ans += (1 << i);
f = bz[f][i];
}
}
cout << ++ans << endl;
}
return 0;
}Prolem D - 山峰
暂时没写。
另外给芒果看看 怎吗卡的。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
unordered_map<int, int>mp;
int n, a[MAXN];
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 - '0', ch = getchar();
return x * f;
}
void write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
return;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
mp[a[i]] = i;
}
if (n <= 10000) {
for (int i = 1; i <= n; i++) {
for (int j = i + 2; j <= n; j++) {
int mid = (a[i] + a[j]) >> 1;
if (a[i] - mid != mid - a[j]) continue;
if (mp[mid]) {
if (mp[mid] > i and mp[mid] < j) {
cout << "YES";
return 0;
}
}
}
}
cout << "NO";
} else {
cout << "YES";
}
return 0;
}