点击题号跳转

A3763 B1360 C3646 D1231 E3035

F3635 G3686 H5310 I2920 J1223

A.Unique Encryption Keys回到顶部

题意

m([1,10^6])个数a[i]([0,2^30]),q([0,10^6])次询问,每次询问区间[L,R]是否存在相同的数字

题解

思考一个问题,令pre[i]为值为i的前一个数字的位置

询问区间[L,R]相当于区间内的所有数,pre[a[i]]都在外面即pre[a[i]]<l则合法OK

转化一下,相当于区间内max(pre[a[i]])<l

令f[i]为[1,i]出现重复数的最大位置,那么可以推出f[i]=max(f[i-1],pre[a[i]])

那么答案就是f[r]<l?OK:Error,复杂度O(nlogn)

PS:卡了一下常数,把unordered_map改成离散化,内存少了1倍,时间快了1.5倍

TZOJ 挑战题库随机训练03

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e6+5;
 4 int a[N],b[N],pre[N];
 5 bool ans[N];
 6 struct node{
 7     int l,r,id;
 8     bool operator<(const node &d)const{
 9         return r<d.r||(r==d.r&&l<d.l);
10     }
11 }q[N];
12 inline int read() {
13     char ch = getchar(); int x = 0, f = 1;
14     while(ch < '0' || ch > '9') {
15         if(ch == '-') f = -1;
16         ch = getchar();
17     } while('0' <= ch && ch <= '9') {
18         x = x * 10 + ch - '0';
19         ch = getchar();
20     } return x * f;
21 }
22 int main(){
23     int n,m;
24     while(scanf("%d%d",&n,&m)!=EOF,n||m){
25         for(int i=1;i<=n;i++){
26             a[i]=read();
27             b[i]=a[i];
28         }
29         sort(b+1,b+1+n);
30         int sz=unique(b+1,b+1+n)-b-1;
31         for(int i=1;i<=sz;i++)pre[i]=0;
32         for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+sz,a[i])-b;
33         for(int i=1;i<=m;i++){
34             q[i].l=read();q[i].r=read();
35             q[i].id=i;
36         }
37         sort(q+1,q+1+m);
38         int p=1,r=0;
39         for(int i=1;p<=m&&i<=n+1;i++){
40             while(p<=m&&i>q[p].r)ans[q[p].id]=(r<q[p].l),p++;
41             if(pre[a[i]])r=max(r,pre[a[i]]);
42             pre[a[i]]=i;
43         }
44         for(int i=1;i<=m;i++)printf("%s\n",ans[i]?"OK":"Error");
45         puts("");
46     }
47     return 0;
48 }
A,离散化+快读500MS

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-11-01
  • 2021-12-24
  • 2021-09-16
  • 2021-05-07
  • 2021-06-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案