HDU-6356

题意:有m次操作,每次操作通过给定的随机函数生成 l , r , v,使得在 到 区间内,所有的a【i】变为max(a[i] , v).

  最后输出n个a【i】* i的异或和。

 

思路:线段树操作,每次维护区间的最小值,如果当前的v小于区间的最小值,直接return,lazy标记维护区间未加的最大值,必要时pushdown就ok;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
// #define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; // template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
unsigned X,Y,Z,W;
unsigned RNG61(){
X = X ^ (X<<);
X = X ^ (X>>);
X = X ^ (X<<);
X = X ^ (X>>);
W = X ^ (Y ^ Z);
X = Y;
Y = Z;
Z = W;
return Z;
} const ll MOD = (<<);
const int maxn = 1e5+;
int n,m; ll sum[maxn*],lazy[maxn*];
void build(int l,int r,int rt){
sum[rt] = ;
lazy[rt] = ;
if(l==r){
return;
}
int mid = (l + r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
void pushup(int rt){
sum[rt] = min(sum[rt<<] , sum[rt<<|]);
}
void pushdown(int rt){
if(lazy[rt]){
lazy[rt<<] = max(lazy[rt],lazy[rt<<]);
lazy[rt<<|] = max(lazy[rt],lazy[rt<<|]);
sum[rt<<] = max(sum[rt<<] , lazy[rt]);
sum[rt<<|] = max(sum[rt<<|] , lazy[rt]);
lazy[rt] = ;
}
}
void update(int l, int r, int rt,int L,int R,ll val){
if(sum[rt] > val)return;
if(l>=L && r <=R){ sum[rt] = max(sum[rt],val); lazy[rt] = max(lazy[rt],val);
return;
}
pushdown(rt);
int mid = (l+r)/;
if(mid >= L)update(l,mid,rt<<,L,R,val);
if(mid < R)update(mid+,r, rt<<|, L,R,val);
pushup(rt);
}
ll ans = 0ll;
void g_ans(int l,int r,int rt,int L,int R){
if(l==r){
// debug(sum[rt]);
ans ^= (1ll*l*sum[rt]);
return;
}
pushdown(rt);
int mid = (l+r)/;
g_ans(l,mid,rt<<,L,R);
g_ans(mid+,r, rt<<|, L,R);
}
int main(){
int t; scanf("%d", &t);
while(t--){ scanf("%d%d", &n, &m);
scanf("%u%u%u", &X, &Y, &Z);
build(,n,);
// debug(X);
for(int i=; i<=m; i++){
ll s = RNG61();
ll t = RNG61();
int tmp1 = s%n+;
int tmp2 = t%n+; int le = min(tmp1 , tmp2);
int ri = max(tmp1, tmp2);
ll v = RNG61() % MOD;
update(,n,,le,ri,v);
}
ans = 0ll;
g_ans(,n,,,n);
printf("%lld\n", ans);
} return ;
}

线段树

由于询问只有一次,就是最后的输出,所以可以用ST表,这道题算是一个逆向的构造ST表,推出dp【0】【i】的结果;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <iostream>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
// #define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; // template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
unsigned X,Y,Z,W;
unsigned RNG61(){
X = X ^ (X<<);
X = X ^ (X>>);
X = X ^ (X<<);
X = X ^ (X>>);
W = X ^ (Y ^ Z);
X = Y;
Y = Z;
Z = W;
return Z;
} const ll MOD = (<<);
const int maxn = 1e5+;
int n,m; ll dp[][maxn];
int Log[maxn];
void update(int le,int ri,ll val){
int k = Log[ri-le+];
dp[k][le] = max(dp[k][le], val);
dp[k][ri-(<<k)+] = max(val,dp[k][ri-(<<k)+]);
}
void solve(){
cin>>n>>m>>X>>Y>>Z;
for(int j=; j<;j++){
for(int i=; i<=n; i++){
dp[j][i] = ;
}
} for(int i=; i<=m; i++){
int t1 = RNG61()%n + ;
int t2 = RNG61()%n + ;
int le = min(t1, t2);
int ri = max(t1, t2);
ll val = RNG61() % MOD; update(le,ri,val);
// cout<<val<<endl;
} for(int j=; j; j--){
for(int i=; i+(<<(j-)) <=n; i++){
dp[j-][i] = max(dp[j][i] , dp[j-][i]);
dp[j-][i+(<<(j-))] = max(dp[j-][i+(<<(j-))] ,dp[j][i]);
}
} ll ans = ;
for(int i=; i<=n; i++){
ans = ans ^ (i * dp[][i]);
// cout<<dp[0][i]<<" ";
}
// cout<<endl;
cout<<ans<<endl;
} int main(){
OKC;
Log[] = ;
for(int i=; i<maxn; i++){
Log[i] = Log[i>>] + ;
}
int t; cin>>t; while(t--){
solve();
}
return ;
}

ST表

05-11 22:55