FZU2105-Digits Count

23 篇文章 0 订阅

Problem 2105 Digits Count

Accept: 562    Submit: 2532
Time Limit: 10000 mSec    Memory Limit : 262144 KB

 Problem Description

Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:

Operation 1: AND opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).

Operation 2: OR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).

Operation 3: XOR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).

Operation 4: SUM L R

We want to know the result of A[L]+A[L+1]+...+A[R].

Now can you solve this easy problem?

 Input

The first line of the input contains an integer T, indicating the number of test cases. (T≤100)

Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.

Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).

Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)

 Output

For each test case and for each "SUM" operation, please output the result with a single line.

 Sample Input

14 41 2 4 7SUM 0 2XOR 5 0 0OR 6 0 3SUM 0 2

 Sample Output

718

 Hint

A = [1 2 4 7]

SUM 0 2, result=1+2+4=7;

XOR 5 0 0, A=[4 2 4 7];

OR 6 0 3, A=[6 6 6 7];

SUM 0 2, result=6+6+6=18.

 Source

“高教社杯”第三届福建省大学生程序设计竞赛


题意:给n个0~15之间的数,有3种更新操作,1种询问操作。3种更新操作是:1、让一个区间的所有数字与一个数字进行逻辑与运算;2、让一个区间的所有数字与一个数字进行逻辑或运算;3、让一个区间的所有数字与一个数字进行异或运算。一种询问操作是询问某个闭区间的所有数字之和。
解题思路:线段树成段更新,因为数据必然是0~15,所以可以记录一下一个区间内的数是否全部相等


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n,q,p,y,z;
int a[1000009],x[1000009*4],lazy[1000009*4];
char ch[10];

void Merge(int k)
{
    if(lazy[k<<1]&&lazy[k<<1|1]&&x[k<<1]==x[k<<1|1]) lazy[k]=1,x[k]=x[k<<1];
    else lazy[k]=0;
}

void build(int k,int l,int r)
{
    if(l==r) {x[k]=a[l],lazy[k]=1;return;}
    int mid=(l+r)>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    Merge(k);
}

void update(int k,int l,int r,int ll,int rr,int val,int type)
{
    if(l>=ll&&r<=rr&&lazy[k])
    {
        if(type==1) x[k]&=val;
        else if(type==2) x[k]|=val;
        else x[k]^=val;
        return ;
    }
    if(lazy[k])
    {
        lazy[k<<1]=lazy[k<<1|1]=1;
        lazy[k]=0;
        x[k<<1]=x[k<<1|1]=x[k];
    }
    int mid=(l+r)>>1;
    if(ll<=mid) update(k<<1,l,mid,ll,rr,val,type);
    if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val,type);
    Merge(k);
}

int query(int k,int l,int r,int ll,int rr)
{
    if(l>=ll&&r<=rr&&lazy[k]) return x[k]*(r-l+1);
    int mid=(l+r)>>1,ans=0;
    if(lazy[k])
    {
        lazy[k<<1]=lazy[k<<1|1]=1;
        x[k<<1]=x[k<<1|1]=x[k];
        lazy[k]=0;
    }
    if(ll<=mid) ans+=query(k<<1,l,mid,ll,rr);
    if(rr>mid) ans+=query(k<<1|1,mid+1,r,ll,rr);
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        build(1,1,n);
        while(q--)
        {
            scanf("%s%d%d",ch,&y,&z);
            if(ch[0]=='S') printf("%d\n",query(1,1,n,y+1,z+1));
            else
            {
                scanf("%d",&p);
                if(ch[0]=='A') update(1,1,n,z+1,p+1,y,1);
                else if(ch[0]=='O') update(1,1,n,z+1,p+1,y,2);
                else update(1,1,n,z+1,p+1,y,3);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值