Codeforces Round #532 (Div. 2)F. Ivan and Burgers【线性基】

F. Ivan and Burgers

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan loves burgers and spending money. There are nn burger joints on the street where Ivan lives. Ivan has qq friends, and the ii-th friend suggested to meet at the joint lili and walk to the joint riri (li≤ri)(li≤ri). While strolling with the ii-th friend Ivan can visit all joints xx which satisfy li≤x≤rili≤x≤ri.

For each joint Ivan knows the cost of the most expensive burger in it, it costs cici burles. Ivan wants to visit some subset of joints on his way, in each of them he will buy the most expensive burger and spend the most money. But there is a small issue: his card broke and instead of charging him for purchases, the amount of money on it changes as follows.

If Ivan had dd burles before the purchase and he spent cc burles at the joint, then after the purchase he would have d⊕cd⊕c burles, where ⊕⊕denotes the bitwise XOR operation.

Currently Ivan has 22100−122100−1 burles and he wants to go out for a walk. Help him to determine the maximal amount of burles he can spend if he goes for a walk with the friend ii. The amount of burles he spends is defined as the difference between the initial amount on his account and the final account.

Input

The first line contains one integer nn (1≤n≤5000001≤n≤500000) — the number of burger shops.

The next line contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤1060≤ci≤106), where cici — the cost of the most expensive burger in the burger joint ii.

The third line contains one integer qq (1≤q≤5000001≤q≤500000) — the number of Ivan's friends.

Each of the next qq lines contain two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — pairs of numbers of burger shops between which Ivan will walk.

Output

Output qq lines, ii-th of which containing the maximum amount of money Ivan can spend with the friend ii.

Examples

input

Copy

4
7 2 3 4
3
1 4
2 3
1 3

output

Copy

7
3
7

input

Copy

5
12 14 23 13 7
15
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5

output

Copy

12
14
27
27
31
14
25
26
30
23
26
29
13
13
7

Note

In the first test, in order to spend the maximum amount of money with the first and third friends, Ivan just needs to go into the first burger. With a second friend, Ivan just go to the third burger.

In the second test for a third friend (who is going to walk from the first to the third burger), there are only 8 options to spend money — 00, 1212, 1414, 2323, 12⊕14=212⊕14=2, 14⊕23=2514⊕23=25, 12⊕23=2712⊕23=27, 12⊕14⊕23=2012⊕14⊕23=20. The maximum amount of money it turns out to spend, if you go to the first and third burger — 12⊕23=2712⊕23=27.

 

 

分析:因为不带修改操作,所以离线在线都能做。

首先排除线段树维护,复杂段显然不对。

离线的话我们可以将区间的右端点排序,然后维护一个1到r的线性基,并且对于每位,我们只存id最大的那个元素,即相当于循环到第i位时,如果x的最高位是i并且x的id大于原来这个位置的元素的下标,那么我们将x放在这里,把原来的向后推,只需要swap一下就可以了。这样相当于一个贪心的过程,即保证高位的元素一定出现在低位的元素的后面,正确性是显而易见的。查询的时候则多判断一下左端点就可以了。

在线的话同理,开5e5个数组的前缀的线性基就可以了,和离线同理。

#include <bits/stdc++.h>

using namespace std;
struct L_B{
    int d[22],pos[22];
    void init()
    {
        memset(d,0,sizeof(d));
        memset(pos,0,sizeof(pos));
    }
    bool insert(int val,int x)
    {
        for (int i=20;i>=0;i--)
            if (val&(1LL<<i))
            {
                if (!d[i])
                {
                    d[i]=val;
                    pos[i]=x;
                    break;
                }
                if(x > pos[i]){
                    swap(x,pos[i]);
                    swap(val,d[i]);
                }
                val^=d[i];
            }
        return val>0;
    }
    int query_max(int x)
    {
        int ret=0;
        for (int i=20;i>=0;i--)
            if ((ret^d[i])>ret && pos[i]>= x)
                ret^=d[i];
        return ret;
    }
}lb[500004];
int main(){
    int n,x;
    cin>>n;
    lb[0].init();
    for (int i = 1; i <= n; ++i) {
        lb[i].init();
        for (int j = 0; j <= 20; ++j) {
            lb[i].d[j]=lb[i-1].d[j];
            lb[i].pos[j]=lb[i-1].pos[j];
        }
        scanf("%d",&x);
        lb[i].insert(x,i);
    }
    int q;cin>>q;
    while (q--){
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",lb[r].query_max(l));
    }
    return 0;
}

 

内容概要:本文介绍了一种基于双层优化的微电网系统规划设计方法,旨在通过Matlab代码实现,解决微电网在规划与运行中的多目标、多层次决策问题。该方法将优化过程分为上下两层:上层通常负责容量配置、设备选址等长期规划决策,下层则聚焦于能量管理、出力调度等短期运行优化,通过迭代交互实现全局最优。文中详细阐述了模型构建、约束条件设定、目标函数设计及求解算法实现流程,并提供了完整的Matlab代码供复现实验,有助于深入理解微电网系统的设计逻辑与优化机制。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事新能源、微电网、综合能源系统等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 学习和掌握双层优化理论在微电网规划设计中的具体应用;② 通过阅读和运行Matlab代码,复现并改进经典优化模型,用于学位论文、科研项目或实际工程方案设计;③ 深入理解微电网中分布式能源、储能与负荷的协同优化调度策略。; 阅读建议:此资源以Matlab代码实现为核心,强调理论与实践的结合。建议读者先理解双层优化的基本思想和数学模型,再结合代码逐行分析,重点关注变量定义、约束条件的代码转化以及主从问题间的迭代逻辑。鼓励在提供的代码基础上进行参数调整、场景扩展或算法改进,以深化学习效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值