线段树 初探 - BenFromHRBUST

线段树 初探



[kuangbin带你飞]专题七 线段树

A HDU 1166 敌兵布阵

Problem Description

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input

第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

Output

对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=5e4+5;

struct S
{
    int l;//左端点
    int r;//右端点
    int ans;//需要维护的内容
} tree[4*N];//线段树需要开4倍

//建树
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    if(l==r)//叶子节点
    {
        scanf("%d",&tree[k].ans);
        return ;
    }
    int m=(l+r)/2;
    build(l,m,k*2);//左孩子
    build(m+1,r,k*2+1);//右孩子
    tree[k].ans=tree[k*2].ans+tree[k*2+1].ans;//状态合并,根据题意维护
}

int ans;

//单点修改
void add(int pos,int k,int b)
{
    if(tree[k].l==tree[k].r)//找到目标位置
    {
        tree[k].ans+=b;
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(pos<=m)
        add(pos,k*2,b);
    else
        add(pos,k*2+1,b);
	//所有包含结点k的结点状态更新(需要维护的内容)
    tree[k].ans=tree[k*2].ans+tree[k*2+1].ans;//所有包含结点k的结点状态更新
}

//区间查询
void sum(int x,int y,int k)
{
    if(tree[k].l>=x&&tree[k].r<=y)
    {
        ans+=tree[k].ans;
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(x<=m)
        sum(x,y,k*2);
    if(y>m)
        sum(x,y,k*2+1);
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int n;
        scanf("%d",&n);
        build(1,n,1);
        char str[10];
        printf("Case %d:\n",cas);
        while(scanf("%s",str)!=EOF&&str[0]!='E')
        {
            if(str[0]=='Q')
            {
                int l,r;
                ans=0;
                scanf("%d%d",&l,&r);
                sum(l,r,1);
                printf("%d\n",ans);
            }
            else if(str[0]=='A')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,1,y);
            }
            else if(str[0]=='S')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,1,-y);
            }
//            for(int i=1;i<=4*n;i++)
//            {
//                cout<<i<<"\t"<<tree[i].ans<<"\t"<<tree[i].l<<"\t"<<tree[i].r<<endl;
//            }
        }
    }
    return 0;
}

B HDU 1754 I Hate It

Problem Description

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input

本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取’Q’或’U’) ,和两个正整数A,B。
当C为’Q’的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为’U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。

Output

对于每一次询问操作,在一行里面输出最高成绩。

Sample Input

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output

5
6
5
9


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

struct S
{
    int l;//左端点
    int r;//右端点
    int ans;//需要维护的内容
} tree[4*N];//线段树需要开4倍

//建树
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    if(l==r)//叶子节点
    {
        scanf("%d",&tree[k].ans);
        return ;
    }
    int m=(l+r)/2;
    build(l,m,k*2);//左孩子
    build(m+1,r,k*2+1);//右孩子
    tree[k].ans=max(tree[k*2].ans,tree[k*2+1].ans);//状态合并,根据题意维护
}

int ans;

//单点修改
void add(int pos,int k,int b)
{
    if(tree[k].l==tree[k].r)//找到目标位置
    {
        tree[k].ans=b;
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(pos<=m)
        add(pos,k*2,b);
    else
        add(pos,k*2+1,b);
    //所有包含结点k的结点状态更新(需要维护的内容)
    tree[k].ans=max(tree[k*2].ans,tree[k*2+1].ans);//所有包含结点k的结点状态更新
}

//区间查询
void sum(int x,int y,int k)
{
    if(tree[k].l>=x&&tree[k].r<=y)
    {
        ans=max(ans,tree[k].ans);
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(x<=m)
        sum(x,y,k*2);
    if(y>m)
        sum(x,y,k*2+1);
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        char str[10];
//        printf("Case %d:\n",cas);
        while(m--)
        {
            scanf("%s",str);
            if(str[0]=='Q')
            {
                int l,r;
                ans=0;
                scanf("%d%d",&l,&r);
                sum(l,r,1);
                printf("%d\n",ans);
            }
            else if(str[0]=='U')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,1,y);
            }
//            for(int i=1;i<=4*n;i++)
//            {
//                cout<<i<<"\t"<<tree[i].ans<<"\t"<<tree[i].l<<"\t"<<tree[i].r<<endl;
//            }
        }
    }
    return 0;
}


C POJ 3468 A Simple Problem with Integers

Description

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.


代码


#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

const int N=2e5+5;

struct S
{
    int l,r;
    ll w;//需要维护的内容
    ll laz;//lazy标记
} tree[4*N];//开4倍数组

//建树
void build(int k,int l,int r)
{
    tree[k].l=l,tree[k].r=r;
    if(tree[k].l==tree[k].r)
    {
        scanf("%lld",&tree[k].w);//也可以传入叶节点的值
        return;
    }
    int m=(l+r)/2;
    build(k*2,l,m);
    build(k*2+1,m+1,r);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;//状态合并,维护内容
}

//lazy标记下传
void down(int k)
{
    tree[k*2].laz+=tree[k].laz;
    tree[k*2+1].laz+=tree[k].laz;
    tree[k*2].w+=tree[k].laz*(tree[k*2].r-tree[k*2].l+1);
    tree[k*2+1].w+=tree[k].laz*(tree[k*2+1].r-tree[k*2+1].l+1);
    tree[k].laz=0;
}

ll ans=0;

//区间查询,a,b为查询区间
void ask_interval(int a,int b,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        ans+=tree[k].w;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        ask_interval(a,b,k*2);
    if(b>m)
        ask_interval(a,b,k*2+1);
}

//区间修改,a,b为修改的区间,y为修改的值
void change_interval(int a,int b,ll y,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        tree[k].w+=(tree[k].r-tree[k].l+1)*y;
        tree[k].laz+=y;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        change_interval(a,b,y,k*2);
    if(b>m)
        change_interval(a,b,y,k*2+1);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    build(1,1,n);
    while(q--)
    {
        char ch[3];
        scanf("%s",ch);
        if(ch[0]=='Q')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            ans=0;
            ask_interval(x,y,1);
            printf("%lld\n",ans);
        }
        else
        {
            int x,y;
            ll k;
            scanf("%d%d%lld",&x,&y,&k);
            change_interval(x,y,k,1);
        }
    }
    return 0;
}


D POJ 2528 Mayor’s posters

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
Every candidate can place exactly one poster on the wall.
All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
The wall is divided into segments and the width of each segment is one byte.
Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters’ size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,… , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
在这里插入图片描述

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4


代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;

const int N=20005;

int ans;
int l[N],r[N];
int all[4*N];
int vis[4*N];
int cnt=0;

struct S
{
    int l,r;
    int w;//需要维护的内容
    int laz;//lazy标记
} tree[16*N];//开4倍数组

//建树
void build(int k,int ll,int rr)
{
    tree[k].l=ll,tree[k].r=rr;
	tree[k].laz=0;
    if(tree[k].l==tree[k].r)
    {
        tree[k].w=0;
        return;
    }
    int m=(ll+rr)/2;
    build(k*2,ll,m);
    build(k*2+1,m+1,rr);
    tree[k].w=0;//状态合并,维护内容
}

//lazy标记下传
void down(int k)
{
    tree[k*2].laz=tree[k].laz;
    tree[k*2+1].laz=tree[k].laz;
    tree[k*2].w=tree[k].laz;
    tree[k*2+1].w=tree[k].laz;
//    tree[k].w=0;
    tree[k].laz=0;
}

//区间查询,a,b为查询区间
void ask_interval(int a,int b,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b&&tree[k].l==tree[k].r)
    {
        if(vis[tree[k].w]==0&&tree[k].w!=0)
        {
            vis[tree[k].w]=1;
            cnt++;
        }
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        ask_interval(a,b,k*2);
    if(b>m)
        ask_interval(a,b,k*2+1);
}

//区间修改,a,b为修改的区间,y为增加的值
void change_interval(int a,int b,int y,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        tree[k].w=y;
        tree[k].laz=y;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        change_interval(a,b,y,k*2);
    if(b>m)
        change_interval(a,b,y,k*2+1);
    tree[k].w=0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        int n;
        scanf("%d",&n);
        build(1,1,4*n);
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            int li,ri;
            scanf("%d%d",&li,&ri);
            l[i]=li;
            r[i]=ri;
            all[i*2]=li;
            all[i*2-1]=ri;
        }
        sort(all+1,all+2*n+1);
        int counter=unique(all+1,all+2*n+1)-all-1;
        int m=counter;
        for(int i=2;i<=m;i++)
        {
            if(all[i]-all[i-1]>1&&all[i]-all[i-1]!=0)
                all[++counter]=all[i]-1;
        }
        sort(all+1,all+counter+1);
        for(int i=1;i<=n;i++)
        {
            int li=lower_bound(all+1,all+counter+1,l[i])-all;
            int ri=lower_bound(all+1,all+counter+1,r[i])-all;
            change_interval(li,ri,i,1);
        }
        ask_interval(1,2*n+counter,1);
        printf("%d\n",cnt);
    }
    return 0;
}


E HDU 1698 Just a Hook

Problem Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
在这里插入图片描述
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.

Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define per(i,a,b) for(ll i=b;i>=a;i--)

const int N=1e5+5;
const double PI=acos(-1);
const ll MOD=1e9+7;
const double EPS=1e-9;

ll ans;

struct node
{
    int l,r;
    int w;
    int laz;//lazy标记
} tree[4*N];//开4倍数组

//建树
void build(int k,int ll,int rr)
{
    tree[k].l=ll,tree[k].r=rr;
	tree[k].laz=0;
    if(tree[k].l==tree[k].r)
    {
        tree[k].w=1;
        return;
    }
    int m=(ll+rr)/2;
    build(k*2,ll,m);
    build(k*2+1,m+1,rr);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;//状态合并,维护内容
}

//lazy标记下传
void down(int k)
{
    tree[k*2].laz=tree[k].laz;
    tree[k*2+1].laz=tree[k].laz;
    tree[k*2].w=tree[k].laz*(tree[k*2].r-tree[k*2].l+1);
    tree[k*2+1].w=tree[k].laz*(tree[k*2+1].r-tree[k*2+1].l+1);
    tree[k].laz=0;
}

//单点查询,pos为查询的点
void ask_point(int pos,int k)
{
    if(tree[k].l==tree[k].r)
    {
        ans=tree[k].w;
        return ;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(pos<=m)
        ask_point(pos,k*2);
    else
        ask_point(pos,k*2+1);
}

//单点修改,pos为修改的点,y为修改的值
void change_point(int pos,int k,int y)
{
    if(tree[k].l==tree[k].r)
    {
        tree[k].w=y;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(pos<=m)
        change_point(pos,k*2,y);
    else
        change_point(pos,k*2+1,y);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;//维护
}

//区间查询,a,b为查询区间
void ask_interval(int a,int b,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        ans+=tree[k].w;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        ask_interval(a,b,k*2);
    if(b>m)
        ask_interval(a,b,k*2+1);
}

//区间修改,a,b为修改的区间,y为增加的值
void change_interval(int a,int b,int y,int k)
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        tree[k].w=(tree[k].r-tree[k].l+1)*y;
        tree[k].laz=y;
        return;
    }
    if(tree[k].laz)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        change_interval(a,b,y,k*2);
    if(b>m)
        change_interval(a,b,y,k*2+1);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;
}

int main()
{
    int T;
    scanf("%d",&T);
    rep(cas,1,T)
    {
        ans=0;
        int n;
        scanf("%d",&n);
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            change_interval(x,y,z,1);
        }
        ask_interval(1,n,1);
//        printf("%lld\n",ans);
        printf("Case %lld: The total value of the hook is %lld.\n",cas,ans);
    }
    return 0;
}



其他

Codeforces Round #197 (Div. 2) 339D

Problem Description

Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, …, a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, …, a^2n - 1^ or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.
Let’s consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let’s write down all the transformations (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.
You are given Xenia’s initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

Input

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, …, a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Output

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

Examples

input
2 4
1 6 3 5
1 4
3 4
1 2
1 2

output

1
3
3
3


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

struct S
{
    int l;
    int r;
    int ans;
    int loop;
} tree[4*N];

int build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    if(l==r)//叶子节点
    {
        scanf("%d",&tree[k].ans);
        tree[k].loop=1;
        return 1;
    }
    int m=(l+r)/2;
    int lop=build(l,m,k*2);//左孩子
    lop=build(m+1,r,k*2+1);//右孩子
    lop++;
    tree[k].loop=lop;
    if(lop%2==1)
        tree[k].ans=tree[k*2].ans^tree[k*2+1].ans;//状态合并
    else
        tree[k].ans=tree[k*2].ans|tree[k*2+1].ans;//状态合并
    return lop;

}

void add(int pos,int k,int b)
{
    if(tree[k].l==tree[k].r)//找到目标位置
    {
        tree[k].ans=b;
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(pos<=m)
        add(pos,k*2,b);
    else
        add(pos,k*2+1,b);
    if(tree[k].loop%2==0)
        tree[k].ans=tree[k*2].ans|tree[k*2+1].ans;//所有包含结点k的结点状态更新
    else
        tree[k].ans=tree[k*2].ans^tree[k*2+1].ans;//所有包含结点k的结点状态更新
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int nn=1;
    for(int i=0; i<n; i++)
    {
        nn*=2;
    }
    build(1,nn,1);
    while(m--)
    {
        int p,b;
        scanf("%d%d",&p,&b);
        add(p,1,b);
//        for(int i=1; i<=4*n; i++)
//        {
//            cout<<i<<"\t"<<tree[i].ans<<"\t"<<tree[i].l<<"\t"<<tree[i].r<<"\t"<<tree[i].loop<<endl;
//        }
        printf("%d\n",tree[1].ans);
    }
    return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值