2022 牛客 数据结构 训练题2
课程小内容
判断2的整数次幂
bool is_pow(int x)
{
return x && x == lowbit(x);
}
D 数据结构
区间和加和乘的pushdown的更新:`
区间平方和的pushdown
代码
#include<iostream>
using namespace std;
const int N = 1e4+10;
struct node{
int l,r;
long long res,sq;
long long tag1 , tag2;
}tr[N<<2];
int a[N];
int n,m;
void pushup(int u){
tr[u].res = tr[u<<1].res + tr[u<<1|1].res ;
tr[u].sq = tr[u<<1].sq + tr[u<<1|1].sq;
}
void pushdown(int u){
int mul = tr[u<<1].tag2, b = tr[u].tag1;
long long templ = tr[u<<1].res ,tempr = tr[u<<1|1].res;
tr[u<<1].res = templ * mul + b * (tr[u<<1].r - tr[u<<1].l + 1);
tr[u<<1|1].res = tempr * mul + b * (tr[u<<1|1].r - tr[u<<1|1].l + 1);
tr[u<<1].sq = mul*mul*tr[u<<1].sq + 2 * templ * mul * b + b * b * (tr[u<<1].r - tr[u<<1].l + 1);
tr[u<<1|1].sq = mul*mul*tr[u<<1|1].sq + 2 * tempr * mul * b + b * b * (tr[u<<1|1].r - tr[u<<1|1].l + 1);
tr[u<<1].tag1 = tr[u<<1].tag1 * mul + b;
tr[u<<1|1].tag1 = tr[u<<1|1].tag1 * mul + b;
tr[u<<1].tag2 = tr[u<<1].tag2 * mul;
tr[u<<1|1].tag2 = tr[u<<1|1].tag2 *mul;
tr[u].tag1 = 0;
tr[u].tag2 = 1;
}
void build(int u,int l,int r){
tr[u].l = l;tr[u].r = r;tr[u].tag1 = 0,tr[u].tag2 = 1,tr[u].res = 0;
if(l==r){
tr[u].res = a[l];
tr[u].sq = a[l] * a[l];
return ;
}
int mid = l + r >> 1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
long long query_sum(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r){
return tr[u].res;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
long long res = 0;
if(l <= mid)res += query_sum(u<<1,l,r);
if(r > mid )res += query_sum(u<<1|1,l,r);
return res;
}
long long query_sq(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r){
return tr[u].sq;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
long long res = 0;
if(l <= mid)res += query_sq(u<<1,l,r);
if(r > mid )res += query_sq(u<<1|1,l,r);
return res;
}
void modify1(int u,int l,int r,int k){
if(tr[u].l >= l && tr[u].r <= r){
// up = a1
tr[u].sq = tr[u].sq * k * k;
tr[u].tag2 *= k;
tr[u].tag1 *= k;
tr[u].res *= k;
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid)modify1(u<<1,l,r,k);
if(r > mid)modify1(u<<1|1,l,r,k);
pushup(u);
}
void modify2(int u,int l,int r,int k){
if(tr[u].l >= l && tr[u].r <= r){
//up = b1
tr[u].sq += 2 * k * tr[u].res + k * k * (tr[u].r - tr[u].l + 1);
tr[u].tag1 += k;
tr[u].res += k * (tr[u].r - tr[u].l + 1);
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid)modify2(u<<1,l,r,k);
if(r > mid)modify2(u<<1|1,l,r,k);
pushup(u);
}
int main(){
cin >> n >> m;
for(int i = 1;i<=n;i++){
cin >> a[i];
}
build(1,1,n);
for(int i = 1;i<=m;i++){
int opt,l,r,k;
cin >> opt >> l >> r;
if(opt == 1){
cout << query_sum(1,l,r) << endl;
}
else if(opt == 2){
cout << query_sq(1,l,r) << endl;
}
else if(opt == 3){
cin >> k;
modify1(1,l,r,k);
}
else if(opt == 4){
cin >> k;
modify2(1,l,r,k);
}
}
return 0;
}
E 线段树
pushdown
代码
注意query,错了好久。。。
#include<iostream>
using namespace std;
const int N = 2e5+10;
struct node{
int l,r;
long long sum,res;
long long mul,add;
}tr[N<<2];
int t,n,m,p;
int a[N];
void pushup(int u){
tr[u].sum = (tr[u<<1].sum + tr[u<<1|1].sum) % p;
tr[u].res = ( (tr[u<<1].res + tr[u<<1|1].res) % p + tr[u<<1].sum * tr[u<<1|1].sum % p )% p;
}
void pushdown(int u,long long b,long long k){
b%=p,k%=p;
long long len = tr[u].r - tr[u].l + 1;
tr[u].res = (k * k % p * tr[u].res % p + len * (len - 1) / 2 % p * b % p * b % p +
(len-1) * k % p * b % p * tr[u].sum % p) % p;
tr[u].sum = tr[u].sum * k % p + b * len % p;
tr[u].add = (tr[u].add * k % p + b)% p;
tr[u].mul = (tr[u].mul * k) % p;
tr[u].add %= p;
tr[u].mul %= p;
tr[u].res %= p;
tr[u].sum %= p;
}
void pushdown(int u){
pushdown(u<<1,tr[u].add,tr[u].mul);
pushdown(u<<1|1,tr[u].add,tr[u].mul);
tr[u].mul = 1;
tr[u].add = 0;
}
node query(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r)return tr[u];
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
node res1 = {0,0,0,0,0,0},res2 = {0,0,0,0,0,0};
long long ans = 0;
if(l <= mid)res1 = query(u<<1,l,r);
if(r > mid )res2 = query(u<<1|1,l,r);
res1.res = (res1.res + res2.res + res1.sum*res2.sum % p)%p;
res1.sum = ( res1.sum+res2.sum ) % p;
return res1;
}
void build(int u,int l,int r){
tr[u].l = l , tr[u].r = r,tr[u].sum = 0,tr[u].res = 0, tr[u].add = 0, tr[u].mul = 1;
if(l == r){
tr[u].sum = a[l];
return ;
}
int mid = l + r >> 1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
void modify_add(int u,int l,int r,int k){
if(tr[u].l >= l && tr[u].r <= r){
pushdown(u,k,1);
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid)modify_add(u<<1,l,r,k);
if(r > mid)modify_add(u<<1|1,l,r,k);
pushup(u);
}
void modify_mul(int u,int l,int r,int k){
if(tr[u].l >= l && tr[u].r <= r){
pushdown(u,0,k);
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid)modify_mul(u<<1,l,r,k);
if(r > mid)modify_mul(u<<1|1,l,r,k);
pushup(u);
}
int main(){
cin >> t;
while(t--){
cin >> n >> m >> p;
for(int i = 1;i<=n;i++)
cin >> a[i];
build(1,1,n);
int opt,l,r,k;
for(int i = 1;i<=m;i++){
cin >> opt >> l >> r;
if(opt == 1){
cin >> k;
modify_add(1,l,r,k);
}
else if(opt == 2){
cin >> k;
modify_mul(1,l,r,k);
}
else{
cout << query(1,l,r).res%p << endl;
}
}
}
return 0;
}
A 借教室
(noip题目)
题目链接
代码
#include<iostream>
using namespace std;
const int N = 1e6+10;
struct node{
int l,r;
int mn;
int tag;
}tr[N<<2];
int n,m;
void pushup(int u ){
tr[u].mn = min(tr[u<<1].mn,tr[u<<1|1].mn);
}
void pushdown(int u){
if(tr[u].tag){
tr[u<<1].mn -= tr[u].tag;
tr[u<<1|1].mn -= tr[u].tag;
tr[u<<1].tag += tr[u].tag;
tr[u<<1|1].tag += tr[u].tag;
tr[u].tag = 0;
}
}
void build(int u,int l,int r){
tr[u].l = l;tr[u].r = r;
if(l == r){
scanf("%d",&tr[u].mn);
return ;
}
int mid = tr[u].l + tr[u].r >> 1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
bool query(int u,int l,int r,int d){
if(tr[u].l >= l && tr[u].r <= r){
if(tr[u].mn >= d)return 1;
else return 0;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
bool res = 1;
if(l <= mid)res = min(res,query(u<<1,l,r,d));
if(r > mid)res = min(res,query(u<<1|1,l,r,d));
return res;
}
void modify(int u ,int l,int r,int d){
if(tr[u].l >= l && tr[u].r <= r){
tr[u].mn -= d;
tr[u].tag += d;
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid)modify(u<<1,l,r,d);
if(r > mid )modify(u<<1|1,l,r,d);
pushup(u);
}
int main(){
cin >> n >> m;
build(1,1,n);
for(int i = 1;i<=m;i++)
{
int d,l,r;
cin >> d >> l >> r;
if(!query(1,l,r,d)){
cout << -1 << endl << i << endl;
return 0;
}
modify(1,l,r,d);
}
cout << 0 << endl;
return 0;
}
B HH的项链
noip题目
代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e6+10;
struct Q{
int l,r;
int key;
bool operator<(Q a)const{
if(l!=a.l)return l < a.l;
return r < a.r;
}
}q[N];
int ans[N],a[N];
int st[N];
int n,m;
int main(){
cin >> n;
for(int i = 1;i<=n;i++)scanf("%d",&a[i]);
cin >> m;
for(int i = 1;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].key = i;
}
sort(q+1,q+1+m);
int l = q[1].l,r = q[1].l-1,temp = 0;
for(int i = 1;i<=m;i++){
while(r > q[i].r){
if(--st[a[r--]]==0)temp--;
}
while(r < q[i].r){
if(++st[a[++r]]==1)temp++;
}
while(l < q[i].l){
if(--st[a[l++]]==0)temp--;
}
ans[q[i].key] = temp;
}
for(int i = 1;i<=m;i++)printf("%d\n",ans[i]);
return 0;
}