FZU 2105-Digits Count(线段树延时标记)
题意:
每次操作区间每个数进行一种(&或|、或^ )给定的一个数,到sum时统计给定区间的和。
分析:
这个题让我觉得我的思维很不活跃,对懒惰标记理解,还远远不够,通过这道题我对懒惰标记加深了不少。
经过各种操作和区间会变成相同数都相邻的情况,若在操作会变成另一个相同数。
选区间内相同数字为懒惰标记进行更新。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 1000100
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int same[N*],n,q,a[N];
void pushup(int rt){
if(same[rt<<]==same[rt<<|]&&same[rt<<]!=-){
same[rt]=same[rt<<];
}
}
void pushdown(int rt){
if(same[rt]!=-){
same[rt<<]=same[rt<<|]=same[rt];
same[rt]=-;
}
}
void build(int l,int r,int rt){
same[rt]=-;
if(l==r){
same[rt]=a[l];
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int op,int opn,int L,int R,int l,int r,int rt){
if(L<=l&&R>=r&&same[rt]!=-){
if(op==)
same[rt]&=opn;
else if(op==)
same[rt]|=opn;
else
same[rt]^=opn;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(L<=m)update(op,opn,L,R,lson);
if(R>m)update(op,opn,L,R,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r&&same[rt]!=-){
return (r-l+)*same[rt];
}
pushdown(rt);
int num=;
int m=(l+r)>>;
if(L<=m)num+=query(L,R,lson);
if(R>m)num+=query(L,R,rson);
pushup(rt);
return num;
}
int main()
{
int t;
scanf("%d",&t);
char ans[];
while(t--){
scanf("%d%d",&n,&q);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
build(,n,);
int x,y,p;
while(q--){
scanf("%s",ans);
if(ans[]=='A'){
scanf("%d%d%d",&p,&x,&y);
update(,p,x+,y+,,n,);
}
else if(ans[]=='O'){
scanf("%d%d%d",&p,&x,&y);
update(,p,x+,y+,,n,);
}
else if(ans[]=='X'){
scanf("%d%d%d",&p,&x,&y);
update(,p,x+,y+,,n,);
}
else{
scanf("%d%d",&x,&y);
printf("%d\n",query(x+,y+,,n,));
}
}
}
return ;
}
FZU 2105-Digits Count(线段树延时标记)的更多相关文章
- FZU 2105 Digits Count(线段树)
Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...
- 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 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 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 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记
E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...
- hdu-3397 Sequence operation 线段树多种标记
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...
随机推荐
- C# json to dynamic object
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); string greeting = obj.greeting; R ...
- 用Canvas制作小游戏——贪吃蛇
今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不 ...
- 1049: [HAOI2006]数字序列 - BZOJ
Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大.Input 第一行包含一个数n ...
- 对jQuery.extend()方法的分析
jQuery.extend方法是我们常用的方法,也是jQuery源码中的基础方法.它的主要作用是:将一个或多个“源对象”合并到一个“目标对象”中,并返回目标对象.它主要有三种表现形式: a.jQuer ...
- lib和dll文件的区别和联系
什么是lib文件,lib和dll的关系如何 (2008-04-18 19:44:37) (1)lib是编译时需要的,dll是运行时需要的. 如果要完成源代码的编译,有lib就够了. 如果也使动态 ...
- java版AC自动机
class Trie { int [][]Next=new int[500005][128]; int []fail=new int[500005]; int []end=new int[500005 ...
- Git教程之分支管理之一
分支在实际中有什么用呢? 你创建了一个属于你自己的分支,别人看不到,别人还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交,直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又 ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- Android日期时间选择器实现以及自定义大小
本文主要讲两个内容:1.如何将DatePicker和TimePicker放在一个dialog里面:2.改变他们的宽度: 问题1:其实现思路就是自定义一个Dialog,然后往里面同时放入DatePick ...
- linux命令之-pstree使用说明
pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitte ...