比赛 |
cdcqの省选膜你赛 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
新史「新幻想史 -现代史-」 |
最终得分 |
100 |
用户昵称 |
liaoy |
运行时间 |
3.337 s |
代码语言 |
C++ |
内存使用 |
10.02 MiB |
提交时间 |
2017-04-11 21:27:02 |
显示代码纯文本
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- char ch; bool flr;
- inline void read(int &a){
- flr=1;
- for(ch=getchar();ch>'9'||ch<'0';ch=getchar())if(ch=='-')flr=0;
- for(a=0;ch<='9'&&ch>='0';ch=getchar())a=((a+(a<<2))<<1)+(ch^'0');
- if(!flr)a=-a;
- }
- int n,m,qtop,top;
- int val[50010];
- struct opt{
- int x,l,r,num;
- int opt;//opt==0表示查询,opt不为0表示修改。特别的。表示修改w
- }O[100010],tmp[100010];
- long long Ans[50010];
- struct node{
- int l,r,m;
- long long sum,tag;
- }Seg[50010<<2];
- void Segbuild(int p,int l,int r){
- Seg[p].l=l; Seg[p].r=r; Seg[p].m=(l+r)>>1; if(l==r)return;
- Segbuild(p<<1,l,Seg[p].m); Segbuild(p<<1|1,Seg[p].m+1,r);
- }
- void unite(int p){
- Seg[p].sum=Seg[p<<1].sum+Seg[p<<1|1].sum+Seg[p].tag*(Seg[p].r-Seg[p].l+1);
- }
- void Change(int p,int l,int r,int data){
- if(Seg[p].l==l&&Seg[p].r==r){
- Seg[p].sum+=(long long)data*(r-l+1); Seg[p].tag+=data; return;
- }
- if(r<=Seg[p].m)Change(p<<1,l,r,data);
- else
- if(Seg[p].m+1<=l)Change(p<<1|1,l,r,data);
- else
- Change(p<<1,l,Seg[p].m,data),Change(p<<1|1,Seg[p].m+1,r,data);
- unite(p);
- }
- long long Getsum(int p,int l,int r){
- if(Seg[p].l==l&&Seg[p].r==r)return Seg[p].sum;
- long long sum=Seg[p].tag*(r-l+1);
- if(r<=Seg[p].m)sum+=Getsum(p<<1,l,r);
- else
- if(Seg[p].m+1<=l)sum+=Getsum(p<<1|1,l,r);
- else
- sum+=Getsum(p<<1,l,Seg[p].m)+Getsum(p<<1|1,Seg[p].m+1,r);
- return sum;
- }
- void CDQ(int l,int r){
- if(l==r)return;
- int m=(l+r)>>1,tl,tr,tt;
- CDQ(l,m); CDQ(m+1,r);
- for(tl=l,tr=m+1;tr<=r;tr++){
- if(O[tr].opt)continue;
- while(O[tl].x<=O[tr].x&&tl<=m){
- if(O[tl].opt)Change(1,O[tl].l,O[tl].r,O[tl].opt);
- tl++;
- }
- Ans[O[tr].num]+=Getsum(1,O[tr].l,O[tr].r);
- }
- for(tl--;tl>=l;tl--)if(O[tl].opt)Change(1,O[tl].l,O[tl].r,-O[tl].opt);
- for(tt=l,tl=l,tr=m+1;tt<=r;tt++)if((tr>r)||(tl<=m&&O[tl].x<=O[tr].x))tmp[tt]=O[tl++];else tmp[tt]=O[tr++];
- for(tt=l;tt<=r;tt++)O[tt]=tmp[tt];
- }
- int main(){
- int i,opt,x;
- freopen("cdcq_a.in","r",stdin); freopen("cdcq_a.out","w",stdout);
- read(n); read(m);
- for(i=1;i<=n;i++)read(val[i]);
- for(i=1;i<=m;i++){
- for(ch=getchar();ch!='Q'&&ch!='M';ch=getchar()); opt=(ch!='Q');
- if(opt){
- top++;
- read(O[top].l); read(O[top].r); read(O[top].opt); O[top].opt=-O[top].opt;
- top++;
- read(O[top].l); read(O[top].r); read(O[top].opt);
- read(x);
- O[top].x=O[top-1].x=x;
- }else{
- qtop++; top++;
- read(O[top].l); read(O[top].r); read(O[top].x); O[top].num=qtop;
- }
- }
- Segbuild(1,1,n);
- CDQ(1,top);
- for(i=1;i<=n;i++)Change(1,i,i,val[i]);
- for(i=1;i<=top;i++)if(!O[i].opt)Ans[O[i].num]+=Getsum(1,O[i].l,O[i].r);
- for(i=1;i<=qtop;i++)printf("%lld\n",Ans[i]);
- }