sicily 1686 Happy Children‘s Day

线段树,第一次用。。。参考代码写

#include<iostream>

#include<cstdio>
#include<cstring>
using namespace std;
int max(int a,int b){
return a<b?b:a;
}
struct node{
int left;
int right;
int most;
int rest;
int index;
}seg[200010];
int w[100010];
void build(int p,int x,int y){
seg[p].left = x;
seg[p].right = y;
seg[p].rest = 0;
seg[p].most = 0;
seg[p].index = x;
if(y-x>=1){
int mid = (x+y)/2;
build(2*p,x,mid);
build(2*p+1,mid+1,y);
}
}
void insert(int p,int x,int y,int v){
if(x<=seg[p].left && y>=seg[p].right){
seg[p].rest+=v;
seg[p].most+=v;
return ;
}
int rest = seg[p].rest;
seg[p].rest = 0;
seg[2*p].rest+=rest;
seg[2*p].most+=rest;
seg[2*p+1].rest+=rest;
seg[2*p+1].most+=rest;
int mid = (seg[p].left+seg[p].right)/2;
if(x<=mid)insert(2*p,x,y,v);
if(y>mid)insert(2*p+1,x,y,v);
if(seg[2*p].most>=seg[2*p+1].most){
seg[p].most = seg[2*p].most;
seg[p].index = seg[2*p].index;
}else {
seg[p].most = seg[2*p+1].most;
seg[p].index = seg[2*p+1].index;
}
}
int find(int p,int x,int y,int &index){
if(x<=seg[p].left && y>=seg[p].right){
index = seg[p].index;
return seg[p].most;
}
int mid = (seg[p].left+seg[p].right)/2;
int rest = seg[p].rest;
seg[p].rest = 0;
seg[2*p].rest+=rest;
seg[2*p].most+=rest;
seg[2*p+1].rest+=rest;
seg[2*p+1].most+=rest;
if(x<=mid){
if(y>=mid+1){
int index1,index2;
int ans1 = find(2*p,x,y,index1);
int ans2 = find(2*p+1,x,y,index2);
if(ans1>=ans2){
index = index1;
}else {
ans1 = ans2;
index = index2;
}
return ans1;
}else {
int index1;
int ans;
ans = find(2*p,x,y,index1);
index = index1;
return ans;
}
}else{
int index1;
int ans = find(2*p+1,x,y,index1);
index = index1;
return ans;
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m),n!=0 && m!=0){
for(int i = 1;i<=n;i++)scanf("%d",&w[i]);
build(1,1,n);
int x,y,z;
char str[10];
for(int i = 1;i<=m;i++){
scanf("%s",str);
if(str[0]=='I'){
scanf("%d%d%d",&x,&y,&z);
insert(1,x,y,z);
}else {
scanf("%d%d",&x,&y);
int ans = find(1,x,y,z);
printf("%d\n",ans);
insert(1,z,z,-ans);
}
}
}
return 0;
}

你可能感兴趣的:(struct,REST,insert,Build)