P10995 【MX-J3-T2】Substring 解题报告
P10995 【MX-J3-T2】Substring
也是在赛时做出黄题了(后话:现在降
首先考虑暴力求出所有字串,排序一遍,时间复杂度
思考如何不求出所有字串就可以查询出第
3 6
3 1 2
1
2
3
4
5
6
答案:
2 2
2 3
3 3
1 1
1 2
1 3
观察到数字
解释:令
并且每个
接下来看具体实现:
于是对于每个查询
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#define int long long
using namespace std;
const int maxn = 3e5 + 5;
int n, q, k;
int a[maxn];
map<int, int> pos;
int len[maxn], pre[maxn];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
pos[a[i]] = i;
}
for (int i = 1, num; i <= n; i++)
{
num = pos[i];
len[i] = n - num + 1;
}
// for (int i = 1; i <= n; i++) {
// cout << len[i];
// }
// cout << endl;
for (int i = 2; i <= n + 1; i++)
{
pre[i] = pre[i - 1] + len[i - 1];
}
// for (int i = 1; i <= n + 1; i++) {
// cout << pre[i] << " ";
// }
// cout << endl;
while (q--)
{
scanf("%lld", &k);
// cout << k << " ";
int x = lower_bound(pre + 1, pre + n + 2, k) - pre - 1;
// cout << x << "# ";
if (x == n + 1)
{
cout << pos[n] << " " << pos[n] + len[n] - 1 << endl;
}
else
{
if (k == pre[x])
{
cout << pos[x] << " " << pos[x] + k - pre[x] << endl;
}
else
{
cout << pos[x] << " " << pos[x] + k - pre[x] - 1 << endl;
}
}
}
return 0;
}
本文作者:vanueber
本文链接:https://www.cnblogs.com/vanueber/p/18668588
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步