HDU6579 Operation
2024-10-07 04:47:41
问题分析
区间求异或和最大,比较自然的想到了线性基。而每次求一个区间的线性基显然是行不通的。我们考虑在每个位置求出首位置到当前位置的线性基。同时我们要使线性基中高位的位置所选的数尽量靠后。这样我们维护线性基的时候在同时维护一个位置信息就好了。
参考程序
#include <bits/stdc++.h>
//#define Debug
using namespace std;
const int Maxn = 1000010;
const int MaxBit = 30;
int n, m, A[ Maxn ], BitBase[ Maxn ][ MaxBit ], Pos[ Maxn ][ MaxBit ];
void Work();
int main() {
int TestCases;
scanf( "%d", &TestCases );
for( ; TestCases--; ) Work();
return 0;
}
void Work() {
scanf( "%d%d", &n, &m );
for( int i = 1; i <= n; ++i ) scanf( "%d", A + i );
memset( BitBase, 0, sizeof( BitBase ) );
memset( Pos, 0, sizeof( Pos ) );
for( int i = 1; i <= n; ++i ) {
for( int j = 0; j < MaxBit; ++j ) BitBase[ i ][ j ] = BitBase[ i - 1 ][ j ], Pos[ i ][ j ] = Pos[ i - 1 ][ j ];
int Position = i;
for( int j = MaxBit - 1; j >= 0; --j )
if( ( A[ i ] >> j ) & 1 )
if( BitBase[ i ][ j ] ) {
if( Pos[ i ][ j ] < Position ) {
swap( BitBase[ i ][ j ], A[ i ] );
swap( Pos[ i ][ j ], Position );
}
A[ i ] ^= BitBase[ i ][ j ];
} else {
BitBase[ i ][ j ] = A[ i ];
Pos[ i ][ j ] = Position;
break;
}
}
#ifdef Debug
for( int i = 1; i <= n; ++i ) {
for( int j = 0; j < MaxBit; ++j ) printf( "(%d,%d), ", BitBase[ i ][ j ], Pos[ i ][ j ] );
printf( "\n" );
}
#endif
int LastAns = 0;
for( int i = 1; i <= m; ++i ) {
int Opt; scanf( "%d", &Opt );
if( Opt == 1 ) {
++n; scanf( "%d", A + n );
A[ n ] ^= LastAns;
for( int j = 0; j < MaxBit; ++j ) BitBase[ n ][ j ] = BitBase[ n - 1 ][ j ], Pos[ n ][ j ] = Pos[ n - 1 ][ j ];
int Position = n;
for( int j = MaxBit - 1; j >= 0; --j )
if( ( A[ n ] >> j ) & 1 )
if( !BitBase[ n ][ j ] ) {
BitBase[ n ][ j ] = A[ n ];
Pos[ n ][ j ] = Position;
break;
} else {
if( Pos[ n ][ j ] < Position ) {
swap( BitBase[ n ][ j ], A[ n ] );
swap( Pos[ n ][ j ], Position );
}
A[ n ] ^= BitBase[ n ][ j ];
}
} else {
int l, r; scanf( "%d%d", &l, &r );
l = ( l ^ LastAns ) % n + 1;
r = ( r ^ LastAns ) % n + 1;
if( l > r ) swap( l, r );
LastAns = 0;
for( int j = MaxBit - 1; j >= 0; --j )
if( ( ( LastAns >> j ) & 1 ) == 0 && Pos[ r ][ j ] >= l )
LastAns ^= BitBase[ r ][ j ];
printf( "%d\n", LastAns );
}
}
return;
}
最新文章
- 附录C 编译安装Hive
- NopCommerce 增加 Customer Settings
- 新接触PHP课程,给自己定制的目标
- spring mvc学习笔记二:@RequestMapping
- OpenCV图像轮廓检测
- 一、HTML和CSS基础--HTML+CSS基础课程--第2部分
- Mondriaan的梦(状态压缩dp)
- 创建指定日期java Date对象
- HTML5 Canvas 概述
- JQuery中$.ajax()方法参数
- 【Eclipse DDMS】 Can&#39;t bind to local 8600 for debugger
- vue学习笔记 实例(二)
- MongoDB学习之路(五)
- Odoo Tech World 2018(上海)互联网开源技术大会通告
- java生成UUID
- Leetcode 349. 两个数组的交集 By Python
- 闹钟AlarmAndMusic 和支持播放音乐效果《IT蓝豹》
- Spring 属性注入(四)属性键值对 - PropertyValue
- Maven 插件管理
- VMware Ubuntu 窗口太小 未安装VMwareTools
热门文章
- MySQL_入手<;一>;增--数据库操作
- Elasticsearch6.2集群搭建, centos7
- 使用zookeeper报错 stat is not executed because it is not in the whitelist. envi is not executed because it is not in the whitelist.
- 用git创建仓库关联本地项目,又一直上传不上去
- 关于redis的几件小事(十)redis cluster模式
- vue组件之子组件和父组件
- slf4j日志的使用-学习笔记
- 多线程编程-- part 5.2 JUC锁之Condition条件
- CentOS MySql5.6编译安装
- httpclient 实现的http工具类