【UOJ 510】某idol的人氣調查
阿新 • • 發佈:2020-11-24
某idol的人氣調查
暴力也有分!!!
題目背景
最近蔡徐坤非常煩惱 因為總是有人喜歡黑他
於是蔡徐坤化名菜虛鯤 暗中來到 x湖一中進行人氣調查
這個學校的學生對於蔡徐坤有這樣的特點
一開始每個人的惡意值都為0
如果一個人黑他 這個學生周圍的一片人 就會累加 x點惡意值
蔡徐坤會隨機時間調查一群人的惡意值最大的那個人
當蔡徐坤看不下去了就會給一部分人發律師函警告 這些學生的惡意值就會清零
由於雞你實在是太美
請你這隻雞模擬這一過程
題目描述 & 格式
第一行 n表示學生數 m表示事件個數
接下來m行
每行3或4個數 op,L,R (v)
若op=1 請輸出區間[L,R] 的惡意最大值
若op=2 表示將區間[L,R] 惡意值清零
若op=3 表示區間[L,R] 內的學生惡意值都加v
提示
初始化時修改標記要置為-1(因為會有0出現) !!切記
這題的pushdown需要修改
如果一個點上有區間修改標記
那麼我們只需要考慮修改標記,
下傳時把左右子節點的加法標記清零
如果一個點上只有加法標記
正常下傳加法標記
但是需要考慮它的左子節點和右子節點上有修改標記的情況
如果這個節點上有修改標記,我們只需要在修改標記上+add[o]即可,不用考慮加法標記
否則照常讓加法標記 +add[o]
這題的modify也要修改
考慮遞迴到目標節點時
如果這個節點上有修改標記,我們只需要在修改標記上+v即可,不用考慮加法標記
否則照常讓加法標記 +v
如何寫區間修改操作呢
考慮遞迴到目標節點時
將該點的最值,加法標記,修改標記全部置0即可
對拍程式
#include<cstdio>
#include<iostream>
using namespace std;
inline int read(){
int x;scanf("%d",&x);return x;
}
int a[100000+5];
int main(){
int n =read(),m =read();
// for(int i=1;i<=n;++i) a[i] =read();
while(m--){
int op =read(),l =read(),r =read();
if(op==1){
int ans =-999;
for(int i=l;i<=r;++i) ans =max(ans,a[i]);
cout<<ans <<endl;
}
else if(op==2){
for(int i=l;i<=r;++i) a[i] =0;
}
else if(op==3){
int x =read();
for(int i=l;i<=r;++i) a[i] +=x;
}
}
}
隨機資料生成器
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<ctime>
using namespace std;
int main(){
freopen("cxk.in","w",stdout);
int n,m;
n = m =1e3; //此處填寫資料範圍
srand(time(0));
cout<<n<<' '<<m<<endl;
for(int i=1;i<=m;++i){
int op =rand()%3+1;
int l =rand()%n+1,r =rand()%n+1;
if(l>r) swap(l,r);
cout<<op<<' '<<l<<' '<<r<<' ';
if(op==3){
cout<<rand()%10<<endl;
}
else puts("");
}
}
樣例
5 4
3 1 4 4
1 2 3
2 2 2
1 2 3
-> 4 4 4 4 0
-> 4 0 4 4 0
4
4
資料範圍
25%的點沒有2操作
另有25%的點 n,m≤104
n,m≤106 所有值在int以內
題解:線段樹嗯,清零用乘0就可以啦
#include<cstdio> #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<bits/stdc++.h> typedef long long ll; using namespace std; const int N=100003; int yc,n,T,x,y; ll z,mod,tim[N*4],mx[N*4]; ll a[N*4],sum[N*4],add[N*4]; void pushup(int rt) { sum[rt]=(sum[rt*2]+sum[rt*2+1]); mx[rt]=max(mx[rt*2],mx[rt*2+1]); } void build(int l,int r,int rt){ tim[rt]=1; add[rt]=0; if(l==r){ mx[rt]=a[l]; return ; } int m=(l+r)/2; build(l,m,rt*2); build(m+1,r,rt*2+1); pushup(rt); } void pushdown(int rt,int ln,int rn){ if(add[rt]==0 && tim[rt]==1) return; if(ln+rn==1) return ; mx[rt*2]=(mx[rt*2]*tim[rt]+add[rt]); mx[rt*2+1]=(mx[rt*2+1]*tim[rt]+add[rt]); add[rt*2]=(add[rt*2]*tim[rt]+add[rt]); add[rt*2+1]=(add[rt*2+1]*tim[rt]+add[rt]); add[rt]=0; tim[rt*2]=(tim[rt*2]*tim[rt]); tim[rt*2+1]=(tim[rt*2+1]*tim[rt]); tim[rt]=1; } void update1(int L,int R,ll c,int l,int r,int rt){//清零 int m=(l+r)/2; pushdown(rt,m-l+1,r-m); if(L<=l && r<=R) { mx[rt]=(mx[rt]*c); tim[rt]*=c; mx[rt]*=c; return ; } if(L<=m) update1(L,R,c,l,m,rt*2); if(R>m) update1(L,R,c,m+1,r,rt*2+1); pushup(rt); } void update2(int L,int R,ll c,int l,int r,int rt){//加值 int m=(l+r)/2; pushdown(rt,m-l+1,r-m); if(L<=l && r<=R) { add[rt]+=c; mx[rt]+=c; return ; } if(L<=m) update2(L,R,c,l,m,rt*2); if(R>m) update2(L,R,c,m+1,r,rt*2+1); pushup(rt); } ll query(int L,int R,int l,int r,int rt){ if(L<=l && r<=R) return mx[rt]; int m=(l+r)/2; pushdown(rt,m-l+1,r-m); ll ans=-1; if(L<=m) ans=max(ans,query(L,R,l,m,rt*2)); if(R>m) ans=max(ans,query(L,R,m+1,r,rt*2+1)); return ans; } int main(){ scanf("%d %d",&n,&T); for(int i=1;i<=4*n;i++) tim[i]=1; for(int i=1;i<=n;i++) a[i]=0; build(1,n,1); while(T--){ scanf("%d",&yc); if(yc==2){ scanf("%d %d ",&x,&y); update1(x,y,0,1,n,1); } if(yc==3){ scanf("%d %d %lld",&x,&y,&z); update2(x,y,z,1,n,1); } if(yc==1){ scanf("%d %d",&x,&y); cout<<query(x,y,1,n,1)<<endl; } } return 0; }