fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛
http://acm.fzu.edu.cn/problem.php?pid=2105
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
Sample Input
Sample Output
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
“高教社杯”第三届福建省大学生程序设计竞赛
【题解】:
这题思路想到 经过多次操作之后的区间应该是一个数字很多相同的区间:
因为如果相邻两个数经过操作之后变成相同的数了,那么再经过覆盖了该区间的操作时,那么他们的值将同时发生改变,变成另外一个相同的值,这多次操作下去,之后将生更多的相同的数字区间,这里的相同数字就是懒惰标记更新的关键:例如:OR 6 0 3, A=[6 6 6 7]; 其中6出现了3次
详见代码:
【code】:
/**
status:Accepted language:Visual C++
time:1953 ms memory:47156KB
code length:2686B author:cj
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1000010
#define lson p<<1
#define rson p<<1|1
using namespace std; struct Nod
{
int l,r;
int num; //标记区间数字是否相同
}node[N<<]; void building(int l,int r,int p)
{
node[p].l = l;
node[p].r = r;
node[p].num = -;
if(l==r)
{
scanf("%d",&node[p].num);
return;
}
int mid = (l+r)>>;
building(l,mid,lson);
building(mid+,r,rson);
if(node[lson].num!=-&&node[lson].num==node[rson].num) //向上更新
{
node[p].num = node[lson].num;
}
} int opreate(int op,int opn,int num) //操作函数 op:操作符 opn,num:操作数
{
if(op==) return opn#
if(op==) return opn|num;
if(op==) return opn^num; } void update(int l,int r,int p,int opn,int op)
{
if(node[p].l==l&&node[p].r==r&&node[p].num>=) //当找到区间并且区间被相同的数覆盖
{
node[p].num = opreate(op,opn,node[p].num);
return;
}
if(node[p].num>=) //经过该区间时,向下更新
{
node[lson].num = node[rson].num = node[p].num;
node[p].num = -;
}
int mid = (node[p].l+node[p].r)>>;
if(r<=mid) update(l,r,lson,opn,op);
else if(l>mid) update(l,r,rson,opn,op);
else
{
update(l,mid,lson,opn,op);
update(mid+,r,rson,opn,op);
}
if(node[lson].num!=-&&node[lson].num==node[rson].num) //向上更新
{
node[p].num = node[lson].num;
}
} __int64 query(int l,int r,int p)
{
if(node[p].l==l&&node[p].r==r&&node[p].num>=)
{
return node[p].num*(node[p].r-node[p].l+);
}
if(node[p].num>=) //经过该区间时,向下更新
{
node[lson].num = node[rson].num = node[p].num;
node[p].num = -;
}
int mid = (node[p].l+node[p].r)>>;
if(r<=mid) return query(l,r,lson);
else if(l>mid) return query(l,r,rson);
else return query(l,mid,lson)+query(mid+,r,rson);
if(node[lson].num!=-&&node[lson].num==node[rson].num) //向上更新
{
node[p].num = node[lson].num;
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
char op[];
building(,n,);
while(m--)
{
scanf("%s",op);
int opn,a,b;
if(op[]=='S')
{
scanf("%d%d",&a,&b);
printf("%I64d\n",query(a+,b+,));
}
else
{
scanf("%d%d%d",&opn,&a,&b);
if(op[]=='A') update(a+,b+,,opn,);
else if(op[]=='O') update(a+,b+,,opn,);
else if(op[]=='X') update(a+,b+,,opn,);
}
}
}
return ;
}
fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛的更多相关文章
- FZU 2105 Digits Count(线段树)
Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number
Mine Number 题目:http://acm.sdut.edu.cn/sdutoj/problem.php? action=showproblem&problemid=2410 Time ...
- [2012山东省第三届ACM大学生程序设计竞赛]——n a^o7 !
n a^o7 ! 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2413 Time Lim ...
- FZU 2105 Digits Count
Problem 2105 Digits Count Accept: 444 Submit: 2139 Time Limit: 10000 mSec Memory Limit : 2621 ...
- FZU 2105 Digits Count(按位维护线段树)
[题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...
- FZU 2105 Digits Count(位数计算)
Description 题目描述 Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation ...
- FZU - 2295 Human life:网络流-最大权闭合子图-二进制优化-第九届福建省大学生程序设计竞赛
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 http://acm.fzu.edu.cn/problem.php?pid=2295 htt ...
随机推荐
- pc端有弹出层 并有滚动的时候遇到的问题以及解决
有时候页面会遇到这样一个问题,页面有个弹出层 ,弹出层是有动条的,当弹出层滚完的时候,后面的页面也会滚动,但是我们希望是后面的页面不滚动;代码如下 1:弹出层出现的时候设置 $('body').css ...
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();
只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called durin ...
- Linux 命令 - less: LESS IS MORE
less 程序是为了替换早期 UNIX 中的 more 程序.less 这个名字是对短语 "less is more" 开了个玩笑,该短语是现代派建筑师和设计师们的座右铭. les ...
- Android 侧滑菜单的简单实现(SlidingMenu)
在我还没有学习Android的时候就用过侧滑菜单的APP,当时第一个感觉是:哇塞,这效果不错!当然,现在自己都已经学Android了,这效果当然也要做出来啊~ SlidingMenu是一种比较新的设置 ...
- 怎么改svn的登陆账号
svn 我想大家都很熟悉.用的也很多.可是上次遇到一个问题 ,就是用别人的svn下载的代码.选择了记住账号密码.结果提交的时候 还是用别人的不大好.结果就想改成自己的账号.第一次改 找了半天.今天给大 ...
- Cocos2d-x实例:设置背景音乐与音效-设置场景实现
设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...
- 引用类型之Array类型
Array类型 ECMAScript数组与其它语言数组一样,都是数据的有序列表.但是ECMAScript数组的每一项可以保存任何类型的数据.而且,ECMAScript数组是可以动态调整的. 1.创建和 ...
- ZigBee组网原理
Zigbee组建一个完整的网络包含两个步骤:网络初始化和节点加入网络.其中,节点加入网络可以分为通过协调器直接连接入网和通过已有父节点入网.下面来依次说明. 1. 网络初始化 ZigBee网络初始化只 ...
- 查找-find -grep
find#.#-name#"*pc"#|#xargs#grep#"Flag" “*.pc”设置要找的文件名grep后面是要找的字符串 #是空格
- Windows内存管理[转]
本文主要内容:1.基本概念:物理内存.虚拟内存:物理地址.虚拟地址.逻辑地址:页目录,页表2.Windows内存管理3.CPU段式内存管理4.CPU页式内存管理 一.基本概念1. 两个内存概念物理内存 ...