1. 程式人生 > >HNOI2017影魔

HNOI2017影魔

init 位置 truct pan void fin node 左右 按順序

影魔
  • 這麽簡單的方法盡然想不到,我是真的菜
  • 對每個點,用單調棧的方式處理出他左右第一個比他大的數的位置,你可以把\(0\)\(n+1\)設成\(inf\)
  • 顯然對於每對\(lef[i]\)\(rig[i]\)都會做出\(p1\)的貢獻
  • 每個\(lef[i]\)會對\(i+1\)\(rig[i]-1\)做出\(p2\)貢獻
  • 同理,每個\(rig[i]\)都會給\(lef[i]+1\)\(i-1\)做出\(p2\)貢獻
  • 用結構體存下來,按順序用線段樹將貢獻加入即可
  • 統計貢獻,對於每個詢問\(l-r\)在掃到\(l-1\)時將這段區間減去,在掃到\(r\)時再將這段區間貢獻加上即可

    // luogu-judger-enable-o2
    #include<bits/stdc++.h> using namespace std; typedef int sign; typedef long long ll; #define For(i,a,b) for(register sign i=(sign)a;i<=(sign)b;++i) #define Fordown(i,a,b) for(register sign i=(sign)a;i>=(sign)b;--i) const int N=2e5+5; bool cmax(sign &a,sign b){return (a<b)?a=b,1:0;} bool
    cmin(sign &a,sign b){return (a>b)?a=b,1:0;} template<typename T>inline T read() { T f=1,ans=0; char ch=getchar(); while(!isdigit(ch)&&ch!=‘-‘)ch=getchar(); if(ch==‘-‘)f=-1,ch=getchar(); while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch-‘0‘),ch=getchar(); return ans*f; } template
    <typename T>inline void write(T x,char y) { if(x==0) { putchar(‘0‘);putchar(y); return; } if(x<0) { putchar(‘-‘); x=-x; } static char wr[20]; int top=0; for(;x;x/=10)wr[++top]=x%10+‘0‘; while(top)putchar(wr[top--]); putchar(y); } void file() { #ifndef ONLINE_JUDGE freopen("3722.in","r",stdin); freopen("3722.out","w",stdout); #endif } int n,m,p1,p2; int a[N]; struct Q { int l,r,x,tag,id; bool operator < (const Q &s)const {return x<s.x;} }opt[N<<2]; int cnt; ll ans[N]; void input() { int l,r; n=read<int>();m=read<int>();p1=read<int>();p2=read<int>(); For(i,1,n)a[i]=read<int>(); For(i,1,m) { l=read<int>();r=read<int>(); ans[i]+=1ll*(r-l)*1ll*p1; opt[++cnt]=(Q){l,r,l-1,-1,i}; opt[++cnt]=(Q){l,r,r,1,i}; } } const int inf=0x3f3f3f3f; namespace Tree { #define mid ((l+r)>>1) #define lson h<<1,l,mid #define rson h<<1|1,mid+1,r ll sum[N<<2],lazy[N<<2]; void push_up(int h) { sum[h]=sum[h<<1]+sum[h<<1|1]; } void push_down(int h,int l,int r) { if(!lazy[h])return; int ls=h<<1,rs=ls|1; lazy[ls]+=lazy[h];lazy[rs]+=lazy[h]; sum[ls]+=lazy[h]*1ll*(mid-l+1); sum[rs]+=lazy[h]*1ll*(r-mid); lazy[h]=0; } void update(int h,int l,int r,int s,int t,ll v) { if(s<=l&&r<=t) { lazy[h]+=v; sum[h]+=1ll*v*1ll*(r-l+1); } else { push_down(h,l,r); if(s<=mid)update(lson,s,t,v); if(mid<t)update(rson,s,t,v); push_up(h); } } ll query(int h,int l,int r,int s,int t) { if(s<=l&&r<=t)return sum[h]; push_down(h,l,r); ll res=0; if(s<=mid)res=query(lson,s,t); if(mid<t)res+=query(rson,s,t); push_up(h); return res; } } #define rg register int lef[N],rig[N]; struct node { int l,r,x; ll v; bool operator < (const node &s)const {return x<s.x;} }e[N<<2]; int sz; void init() { int j; sort(opt+1,opt+cnt+1); a[0]=a[n+1]=inf; For(i,1,n) { for(j=i-1;j>=0;j=lef[j])if(a[j]>a[i])break; lef[i]=j; } Fordown(i,n,1) { for(j=i+1;j<=n+1;j=rig[j])if(a[j]>a[i])break; rig[i]=j; } //For(i,1,n)cout<<lef[i]<<‘ ‘<<rig[i]<<endl; For(i,1,n) { if(lef[i]+1<i&&rig[i]<=n)e[++sz]=(node){lef[i]+1,i-1,rig[i],p2}; if(1<=lef[i]&&rig[i]>i+1)e[++sz]=(node){i+1,rig[i]-1,lef[i],p2}; if(1<=lef[i]&&rig[i]<=n)e[++sz]=(node){lef[i],lef[i],rig[i],p1}; } sort(e+1,e+sz+1); } void work() { int pos1=1,pos2=1; while(pos2<=cnt&&!opt[pos2].x)pos2++; //cerr<<pos2<<‘ ‘<<cnt<<endl; For(i,1,n) { for(;pos1<=sz&&e[pos1].x==i;++pos1) Tree::update(1,0,n,e[pos1].l,e[pos1].r,e[pos1].v); for(;pos2<=cnt&&opt[pos2].x==i;++pos2) ans[opt[pos2].id]+=Tree::query(1,0,n,opt[pos2].l,opt[pos2].r)*opt[pos2].tag; } For(i,1,m)write(ans[i],\n); } int main() { file(); input(); init(); work(); return 0; }

HNOI2017影魔