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- ...
随机推荐
- 【学习总结】【多线程】 线程 & 进程 & NSThread(多线程的一套API)
一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开 Chrome.Xcode,系统就会分别启动2个进 ...
- 基于SuperSocket实现的WebSocket(后端)
关于WebSocket其实很早就想发了,奈何之前项目中的WebSocket的后端不是我做的,而我又想前后端都发出来和大家讨论讨论~于是挤出点时间研究了一下WebSocket的后端实现(所以才有了这篇文 ...
- DB天气app冲刺第十一天
今天是第十一天了.今天遇到了一个很麻烦的问题 就是程序好好的然后调试运行之后能够安装成功 但总是运行不了 一直闪退.最主要的问题是代码还没有问题,这是最让人揪心的一个问题了.因为有bug的话还可以改, ...
- java第一课:环境、变量、数据类型
一.java编程注意事项1.java区分大小写2.每条语句结尾有分号3.上下级代码注意缩进4.大括号要成对出现5.标点符号要用英文半角(半角全角区别)二.eclipse1.eclipse是自编译及时编 ...
- MCM试题原文及翻译 AB题 2014美国数学建模竞赛
MCM试题原文及翻译 AB题 2014美国数学建模竞赛 原创翻译,如有瑕疵,敬请谅解. 转载请注明:过客小站 » MCM试题原文及翻译 AB题 2014美国数学建模竞赛 PROBLEM A: The ...
- Hex string convert to integer with stringstream
#include <sstream>#include <iostream>int main() { unsigned int x; std::stringstream ss; ...
- linux ubuntu 11.04 samba 服务器设置
安装 SAMBA 组件 sudo apt-get install samba smbfs smbclient 配置相关参数 sudo gedit /etc/samba/smb.conf 文件中相关 ...
- 6大排序算法,c#实现
using System; using System.Text; using System.Collections.Generic; namespace ArithmeticPractice { st ...
- mybatis怎样配置数据库集群
场景:一个读数据源一个读写数据源. 原理:借助spring的[org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource] ...
- Protected Functions 是理解OO的难点和关键
Protected Functions 是理解OO的难点和关键 private和public函数都好理解,这里就不多说了,夹在中间的prortected却有许多精妙之处,说说我的几个疑问和看法:1. ...