H

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 const int N=5e4+10,M=(1<<10);
 7 int a[N],c[N],n,m;
 8 vector<int>g[M];
 9 vector<int>id[N];
10 int last[M];
11 int ans[N];
12 //maxn<=10^4,a[i]<=2^10
13 struct node
14 {
15     int l,r,id;
16     bool operator <(const node&o)const
17     {
18         return r<o.r;
19     }
20 }q[N];
21 
22 void init()
23 {
24     int tot=0;
25     for (int i=1;i<M;i++) {
26         g[i].clear();
27         for (int k=i;k;k=((k-1)&i)) {
28             g[i].push_back(k);
29             tot++;
30         }
31     }
32   //  printf("###%.3lf ",(double)tot/M);
33 }
34 
35 int lowbit(int x){return x&-x;}
36 void update(int x,int k)
37 {
38     for (;x;x-=lowbit(x)) c[x]=max(c[x],k);
39 }
40 int query(int x)
41 {
42     int ret=0;
43     for (;x<=n;x+=lowbit(x)) ret=max(ret,c[x]);
44     return ret;
45 }
46 
47 int baoli(int l,int r)
48 {
49     int ret=0;
50     for (int i=l;i<=r;i++) {
51         for (int j=i+1;j<=r;j++) {
52             ret=max(ret,a[i]&a[j]);
53         }
54     }
55     return ret;
56 }
57 
58 int main()
59 {
60    // freopen("1.in","r",stdin);
61    // freopen("1.out","w",stdout);
62     init();
63     while (~scanf("%d%d",&n,&m)) {
64         for (int i=1;i<=n;i++) {
65             scanf("%d",&a[i]); id[i].clear();

66         }
67         for (int i=0;i<m;i++) {
68             scanf("%d%d",&q[i].l,&q[i].r);
69             id[q[i].r].push_back(i);
70         }
71         memset(c,0,sizeof(c));
72         memset(last,0,sizeof(last));
73         for (int i=1;i<=n;i++) {
74             for (int j=0,k=a[i];j<g[k].size();j++) {
75                 int t=g[k][j];
76                 if (last[t]) {
77                     update(last[t],t);
78                 }
79                 last[t]=i;
80             }
81           //  printf("!!!%d ",i);
82             for (unsigned int j=0;j<id[i].size();j++) {
83                 int k=id[i][j];
84                 ans[k]=query(q[k].l);
85             }
86         }
87         for (int i=0;i<m;i++) {
88             printf("%d ",ans[i]);
89          //   printf("--%d ",baoli(q[i].l,q[i].r));
90         }
91     }
92     return 0;
93 }

原文地址:https://www.cnblogs.com/acvc/p/4303970.html