4241: 历史研究
Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 456 Solved: 122
[ Submit][ Status][ Discuss]
Description
IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记。JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件。
日记中记录了连续N天发生的时间,大约每天发生一件。
事件有种类之分。第i天(1<=i<=N)发生的事件的种类用一个整数Xi表示,Xi越大,事件的规模就越大。
JOI教授决定用如下的方法分析这些日记:
1.
选择日记中连续的一些天作为分析的时间段
2.
事件种类t的重要度为t*(这段时间内重要度为t的事件数)
3.
计算出所有事件种类的重要度,输出其中的最大值
现在你被要求制作一个帮助教授分析的程序,每次给出分析的区间,你需要输出重要度的最大值。
Input
第一行两个空格分隔的整数N和Q,表示日记一共记录了N天,询问有Q次。
接下来一行N个空格分隔的整数X1...XN,Xi表示第i天发生的事件的种类
接下来Q行,第i行(1<=i<=Q)有两个空格分隔整数Ai和Bi,表示第i次询问的区间为[Ai,Bi]。
Output
输出Q行,第i行(1<=i<=Q)一个整数,表示第i次询问的最大重要度
Sample Input
5 5
9 8 7 8 9
1 2
3 4
4 4
1 4
2 4
9 8 7 8 9
1 2
3 4
4 4
1 4
2 4
Sample Output
9
8
8
16
16
8
8
16
16
HINT
1<=N<=10^5
1<=Q<=10^5
1<=Xi<=10^9 (1<=i<=N)
Source
静态的区间询问,询问内容又那么,,,嗯
所以就用莫队吧
不过,题目需要的是最大值
普通莫队在左端点的移动时涉及新增与删除,于是,,想要维护Max,就得多一个数据结构
这样就多了一个log的复杂度,GG
那我们就用另一种莫队,
还是按照原来的双关键字排序
在左端点属于同一块时,右端点是递增的,就是说,右端点只有增加的操作
左端点可能左移右移,因而每次都在左端点所在块暴力一次
当左端点往下一块转移时,就丢弃当前储存的一切信息
反正各种维护下
复杂度就能保证在根号
总的来说,如果情况是增加信息O(1)而删除是O(log),就用这种莫队
嗯,,一开始没开long long WA一发,GG
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn = 1E5 + 10;
typedef long long LL;
struct data{
int l,r,pos,flag;
bool operator < (const data &b) const {
if (flag < b.flag) return 1;
if (flag > b.flag) return 0;
return r < b.r;
}
}Query[maxn];
LL tot[maxn],now[maxn],ans[maxn];
int n,m,cur = 1,cnt,Sqrt,a[maxn],vis[maxn],num[maxn];
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
for (int i = 1; i <= n; i++) scanf("%d",&num[i]),a[i] = num[i];
sort(a + 1,a + n + 1);
for (int i = 2; i <= n; i++)
if (a[i] != a[i-1])
a[++cur] = a[i];
for (int i = 1; i <= n; i++)
num[i] = lower_bound(a + 1,a + cur + 1,num[i]) - a;
Sqrt = sqrt(n);
for (int i = 1; i <= m; i++) {
scanf("%d%d",&Query[i].l,&Query[i].r);
Query[i].flag = Query[i].l / Sqrt + 1;
if (Query[i].l % Sqrt == 0) --Query[i].flag;
Query[i].pos = i;
}
sort(Query + 1,Query + m + 1);
int L,R,Rnow; L = 1; R = Sqrt;
LL Ans;
for (int i = 1; i <= m; i++) {
if (Query[i].flag != Query[i-1].flag) {
++cnt; Ans = 0;
while (R < Query[i].l) L += Sqrt,R += Sqrt;
R = min(R,n);
Rnow = R;
}
for (int j = Rnow + 1; j <= Query[i].r; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,tot[num[j]] = 0;
tot[num[j]] += 1LL*a[num[j]];
Ans = max(Ans,tot[num[j]]);
}
Rnow = max(Rnow,Query[i].r);
LL ans2 = Ans; int tail = min(Query[i].r,R);
for (int j = Query[i].l; j <= tail; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,tot[num[j]] = 0;
now[num[j]] += 1LL*a[num[j]];
ans2 = max(ans2,now[num[j]] + tot[num[j]]);
}
for (int j = Query[i].l; j <= tail; j++)
now[num[j]] -= 1LL*a[num[j]];
ans[Query[i].pos] = ans2;
}
for (int i = 1; i <= m; i++) printf("%lld\n",ans[i]);
return 0;
}