HDU-6356 Glad You Came 线段树 ST表
题意:
有m次操作,每次操作通过给定的随机函数生成 l , r , v,使得在 l 到 r 区间内,所有的$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; //2147483647
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
template<typename T>
inline T read(T&x){
x=0;int f=0;char ch=getchar();
while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',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<<11);
X = X ^ (X>>4);
X = X ^ (X<<5);
X = X ^ (X>>14);
W = X ^ (Y ^ Z);
X = Y;
Y = Z;
Z = W;
return Z;
}
const ll MOD = (1<<30);
const int maxn = 1e5+9;
int n,m;
ll sum[maxn*4],lazy[maxn*4];
void build(int l,int r,int rt){
sum[rt] = 0;
lazy[rt] = 0;
if(l==r){
return;
}
int mid = (l + r)/2;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
}
void pushup(int rt){
sum[rt] = min(sum[rt<<1] , sum[rt<<1|1]);
}
void pushdown(int rt){
if(lazy[rt]){
lazy[rt<<1] = max(lazy[rt],lazy[rt<<1]);
lazy[rt<<1|1] = max(lazy[rt],lazy[rt<<1|1]);
sum[rt<<1] = max(sum[rt<<1] , lazy[rt]);
sum[rt<<1|1] = max(sum[rt<<1|1] , lazy[rt]);
lazy[rt] = 0;
}
}
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)/2;
if(mid >= L)update(l,mid,rt<<1,L,R,val);
if(mid < R)update(mid+1,r, rt<<1|1, 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)/2;
g_ans(l,mid,rt<<1,L,R);
g_ans(mid+1,r, rt<<1|1, L,R);
}
int main(){
int t; scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
scanf("%u%u%u", &X, &Y, &Z);
build(1,n,1);
// debug(X);
for(int i=1; i<=m; i++){
ll s = RNG61();
ll t = RNG61();
int tmp1 = s%n+1;
int tmp2 = t%n+1;
int le = min(tmp1 , tmp2);
int ri = max(tmp1, tmp2);
ll v = RNG61() % MOD;
update(1,n,1,le,ri,v);
}
ans = 0ll;
g_ans(1,n,1,1,n);
printf("%lld\n", ans);
}
return 0;
}
由于询问只有一次,就是最后的输出,所以可以用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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',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<<11); X = X ^ (X>>4); X = X ^ (X<<5); X = X ^ (X>>14); W = X ^ (Y ^ Z); X = Y; Y = Z; Z = W; return Z; } const ll MOD = (1<<30); const int maxn = 1e5+9; int n,m; ll dp[20][maxn]; int Log[maxn]; void update(int le,int ri,ll val){ int k = Log[ri-le+1]; dp[k][le] = max(dp[k][le], val); dp[k][ri-(1<<k)+1] = max(val,dp[k][ri-(1<<k)+1]); } void solve(){ cin>>n>>m>>X>>Y>>Z; for(int j=0; j<20;j++){ for(int i=0; i<=n; i++){ dp[j][i] = 0; } } for(int i=1; i<=m; i++){ int t1 = RNG61()%n + 1; int t2 = RNG61()%n + 1; int le = min(t1, t2); int ri = max(t1, t2); ll val = RNG61() % MOD; update(le,ri,val); // cout<<val<<endl; } for(int j=19; j; j--){ for(int i=1; i+(1<<(j-1)) <=n; i++){ dp[j-1][i] = max(dp[j][i] , dp[j-1][i]); dp[j-1][i+(1<<(j-1))] = max(dp[j-1][i+(1<<(j-1))] ,dp[j][i]); } } ll ans = 0; for(int i=1; i<=n; i++){ ans = ans ^ (i * dp[0][i]); // cout<<dp[0][i]<<" "; } // cout<<endl; cout<<ans<<endl; } int main(){ OKC; Log[2] = 1; for(int i=3; i<maxn; i++){ Log[i] = Log[i>>1] + 1; } int t; cin>>t; while(t--){ solve(); } return 0; }
skr