CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了一遍过~~
为了表示每位的二进制数...线段树开成二维的...第一维老样子~记是树中哪个点..第二维记当前段之和的不进位二进制数...因为最多到10^5...也就是不会超过2^20...第二维开个20就够了....
区间更新如: 3 3 这段全xor 3...3+3的不进位二进制数为(2,2)...xor 3,3的二进制为(1,1)..将x二进制为1的改为len-原来的...那么(2-2,2-2)=0
2 4 这段全xor 3...2+4的不进位二进制数为(1,1,0).....将x二进制为1的改为len-原来的..那么(1,2-1,2-0)=(1,1,2)=2+2+4=8
不知道这货是不是叫二维线段树.....二维线段树应该是树中有树吧..也就是第一颗树的每个节点又是线段树....
Program:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#define ll long long
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 100005
using namespace std;
struct node
{
int a[22];
};
int sum[MAXN<<2][22],col[MAXN<<2][22];
ll h[22];
void PushDown(int len,int now)
{
int i;
for (i=0;i<20;i++)
if (col[now][i])
{
sum[now<<1][i]=(len-(len>>1))-sum[now<<1][i];
sum[(now<<1)|1][i]=(len>>1)-sum[(now<<1)|1][i];
col[now<<1][i]=1-col[now<<1][i];
col[(now<<1)|1][i]=1-col[(now<<1)|1][i];
}
for (i=0;i<20;i++) col[now][i]=0;
return;
}
void update(int L,int R,int x,int l,int r,int now)
{
int i;
if (L<=l && R>=r)
{
for (i=0;i<20;i++)
if (x&(1<<i))
{
sum[now][i]=(r-l+1)-sum[now][i];
col[now][i]=1-col[now][i];
}
return;
}
PushDown(r-l+1,now);
int mid=(l+r)>>1;
if (L<=mid) update(L,R,x,l,mid,now<<1);
if (R>mid) update(L,R,x,mid+1,r,(now<<1)|1);
for (i=0;i<20;i++) sum[now][i]=sum[now<<1][i]+sum[(now<<1)|1][i];
return;
}
node query(int L,int R,int l,int r,int now)
{
int i;
node h;
memset(h.a,0,sizeof(h.a));
if (L<=l && R>=r)
{
for (i=0;i<20;i++) h.a[i]=sum[now][i];
return h;
}
PushDown(r-l+1,now);
int mid=(l+r)>>1;
if (L<=mid)
{
node p=query(L,R,l,mid,now<<1);
for (i=0;i<20;i++) h.a[i]+=p.a[i];
}
if (R>mid)
{
node p=query(L,R,mid+1,r,(now<<1)|1);
for (i=0;i<20;i++) h.a[i]+=p.a[i];
}
return h;
}
int main()
{
int i,n,m;
while (~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
memset(col,0,sizeof(col));
for (i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
update(i,i,x,1,n,1);
}
scanf("%d",&m);
while (m--)
{
int tp,l,r;
scanf("%d%d%d",&tp,&l,&r);
if (tp==1)
{
ll ans,x;
node h=query(l,r,1,n,1);
ans=0,x=1;
for (i=0;i<20;i++)
{
ans+=x*h.a[i];
x*=2;
}
printf("%I64d\n",ans);
}else
{
int x;
scanf("%d",&x);
update(l,r,x,1,n,1);
}
}
}
return 0;
}
CodeForces 242E - XOR on Segment 二维线段树?的更多相关文章
- CodeForces 242E二维线段树
E. XOR on Seg ...
- Codeforces 453E - Little Pony and Lord Tirek(二维线段树+ODT)
Codeforces 题目传送门 & 洛谷题目传送门 一道难度 *3100 的 DS,而且被我自己搞出来了! 不过我终究还是技不如人,因为这是一个 \(n\log^2n\) + 大常数的辣鸡做 ...
- codeforces 677D D. Vanya and Treasure(二维线段树)
题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)
[链接]我是链接 [题意] 接上一篇文章 [题解] 接(点我进入)上一篇文章. 这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法. 我们可以把n个正方形用n棵线段树来维护. 第i棵线段树维护 ...
- 【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)
[BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一 ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
随机推荐
- android系统360浏览器使用append动态添加元素出现黑色错乱背景
去掉样式 backface-visibility:hidden;
- Nginx 反向代理配置实例(转)
user www www; worker_processes ; error_log logs/error.log notice; pid logs/nginx.pid; worker_rlimit_ ...
- Windows7电脑上不去网,ipconfig查询时默认网关会出现0.0.0.0问题的解决
用ipconfig查看网络配置,发现其他都正确,唯独默认网关上多了一条0.0.0.0的记录,.禁用网络连接再启用也不能恢复.网上找了一下有说改注册表的,打开注册表找到 HKEY_LOCAL_MACHI ...
- 【转】Understanding and Using rem Units in CSS
CSS units have been the subject of several articles here on SitePoint (such as A Look at Length Unit ...
- 用POP动画引擎实现弹簧动画(POPSpringAnimation)
效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @proper ...
- windows下安装mysql笔记
接着上几篇文章再来看下windows下安装mysql. 我这里是windows7 64位, 安装过程中还是遇到一些坑,这里记录下. 一.下载安装包 打开mysql官网下载页面:http://dev.m ...
- [BZOJ]3737 [Pa2013]Euler
从这个FB开始写博客啦. 也不知道会坚持多久…… = =似乎要加一句转载请注明出处 http://www.cnblogs.com/DancingOnTheTree/p/4026076.html htt ...
- MyEclipse导入Maven项目报错 Plugin execution not covered by lifecycle configuration:
web项目使用到mybatis,需要使用mybatis的自动生成代码插件,配置build部分如下: <build> <pluginManagement></pluginM ...
- switch语句中的选择因子
switch语句能否用作用在byte上,能否作用在long上,能否作用在String上? switch选择语句的格式为: switch(intergral-selector){ case integ ...
- Mysql 5.7.9 cmake boost.cmake 处理
环境Centos 6.7 x64 mininal 今天突然编译Mysql 5.7.9 按之前的cmake .的方式 发现报错了..提示 需要boost -- BOOST_INCLUDE_DIR /us ...