[UOJ # 218.]] [UNR # 1 train sustainable management of tree line

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ALPS233/article/details/51956364

Topic:
uoj there's a train station, used to manage the train belongs to uoj.

Train a total of the n-
the n-
pieces numbered 1, ..., the n-
1, ..., the n-
only used to hold one end of the train rail, train due to the special structure, each railway cars can be parked countless small train. Each railway are independent of each other.

Railway is a stack structure, after the parked train can be first out.

The tonnage per train has a t
t
.

Since NOI2016 coming, you want to train with a small front fighting people up, the train station administrator nine poor need to deal with a lot of events per day.

About three kinds of events can be summarized as:
• 1 LR someone wants to railway number in [L, r]
[L, r]
small positive each railway first opened a can out of the train fight, you need to train these statistics the tonnage and no railway trains are not included in the answer.
• 2 l No. L
L
railway drove off a train if the railway is not no train out.
• 3 lrx railway number in [L, r]
[L, r]
of each railway are a new park tonnage of the X-
the X-
trains.

Now administrators nine poor to go to the front line of the South China Sea, for him you need to manage the train station.

As the busy railway station, so you need real-time feedback, we will require you to use some means to force online, see the specific input format.

Input Format

Input of the first row three non-negative integer n-, m, TY
n-, m, TY
denotes the number of operations and the number of the railway line and whether mandatory.

Subsequently m
m
rows of three numbers or four numbers or two numbers represents a single operation.

For real-time feedback information, you need to decrypt L, R & lt
L, R & lt
, provided that the read L1, R1
L1, R1
, the actual values are as follows:

==== L2r2lr (L1 + Lastans⋅ty) Modn + 1 (R1 + Lastans⋅ty) Modn + seen 1min (L2, R2) by max (L2, R2)
L2 = (L1 + Lastans⋅ty) Modn + 1r2 = ( r1 + lastans⋅ty) modn + 1l = min (l2, r2) r = max (l2, r2)

lastans
lastans
the answer to the query once represented, if not before asking or 0
0
.

When you perform the second type of operation, so only = L (L1 + lastans⋅ty) modN. 1 +
L = (L1 + lastans⋅ty) modN +. 1
to.

Input data to ensure 1≤l1, r1≤n
1≤l1, r1≤n
.

Note that we do not encrypt the tonnage and type of operation

Output Format

For each output line of inquiry, a non-negative integer answers.

A sample

input
10 10 0
3 1 5 3
1 1 6
3 1 7 1
1 1 9
1 1 6
3 1 5 2
1 3 6
1 3 9
3 1 7 6
2 1

output
15
7
6
7
8

http://uoj.ac/problem/218
Solution:

We can build a sustainable segment of the tree against time, maintain the stack top of the stack for each time period of each rail tonnage and top of the stack trains.
Let us maintain a segment tree for statistical answer.
So operating on only three: the
range of inquiry: direct questions to answer in the segment tree.
Interval pressure: be persistent in the tree line a section for cover, and then change it in the answer segment tree.
Number of single-point bomb: Since we recorded the stack time, inbound inquiries to time t, we can query the tree before time t, find out information on top of the stack before the current point stack, and then the tree line and the answer persistence can modify the tree line.

Code :

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 500005
#define lson id*2
#define rson id*2+1
using namespace std;
int n,m,sum[N*4],lazy[N*4];

void pd(int id,int l,int r,int mid)
{
    if(lazy[id]==-1) return ;
    sum[lson]=(mid-l+1)*lazy[id];
    sum[rson]=(r-mid)*lazy[id];
    lazy[lson]=lazy[rson]=lazy[id];
    lazy[id]=-1;
}
void add(int id,int L,int R,int l,int r,int v)
{
    //cout<<L<<"  "<<R<<"  "<<l<<" "<<r<<endl;
    if(L>r||R<l) return ;
    if(L>=l&&R<=r)
    {
        lazy[id]=v;
        sum[id]=(R-L+1)*v;
        return ;
    }
    int mid=(L+R)>>1;
    pd(id,L,R,mid);
    add(lson,L,mid,l,r,v);
    add(rson,mid+1,R,l,r,v);
    sum[id]=sum[lson]+sum[rson];
}
int getans(int id,int L,int R,int l,int r)
{
    if(L>r||R<l) return 0;
    if(L>=l&&R<=r) return sum[id];
    int mid=(L+R)>>1;
    pd(id,L,R,mid);
    int ret=getans(lson,L,mid,l,r)+getans(rson,mid+1,R,l,r);
    return ret;
}

int tim[N*75],val[N*75],tot,ls[N*75],rs[N*75];
int rt[N];

void down(int id)
{
    if(tim[id]==-1) return;
    ++tot;tim[tot]=tim[id];ls[tot]=ls[ls[id]];rs[tot]=rs[ls[id]];ls[id]=tot;val[tot]=val[id];
    ++tot;tim[tot]=tim[id];ls[tot]=ls[rs[id]];rs[tot]=rs[rs[id]];rs[id]=tot;val[tot]=val[id];
    tim[id]=-1;
}
void build(int pre,int &now,int L,int R,int l,int r,int t,int v)
{
    now=++tot;

    if(L>=l&&R<=r)
    {
        tim[now]=t;
        val[now]=v;
        return ;
    }   //cout<<L<<"   "<<R<<"           "<<l<<" "<<r<<endl;
    if(pre) down(pre);
    ls[now]=ls[pre],rs[now]=rs[pre];
    int mid=(L+R)>>1;
    if(mid>=l) build(ls[pre],ls[now],L,mid,l,r,t,v);
    if(mid+1<=r) build(rs[pre],rs[now],mid+1,R,l,r,t,v);
}
int get(int id,int l,int r,int pos)
{
    if(!id||tim[id]!=-1) return id;
    int mid=(l+r)>>1;
    if(pos<=mid) return get(ls[id],l,mid,pos);
    return get(rs[id],mid+1,r,pos); 

}
void del(int x,int y)
{
    int id=get(rt[y-1],1,n,x);
    if(!id)
    {
        rt[y]=rt[y-1];
        return ;
    }   
//  if(tim[id]==0) while(1);
    id=get(rt[tim[id]-1],1,n,x);
    build(rt[y-1],rt[y],1,n,x,x,tim[id],val[id]);
    add(1,1,n,x,x,val[id]);
}
void change(int l,int r,int v,int i)
{
    build(rt[i-1],rt[i],1,n,l,r,i,v);

    add(1,1,n,l,r,v);
} 
int ty;
int ans;
int main()
{
    scanf("%d%d%d",&n,&m,&ty);
    memset(lazy,-1,sizeof(lazy));
    memset(tim,-1,sizeof(tim));
    for(int i=1,aa,bb,cc,dd;i<=m;i++)
    {

        scanf("%d",&aa);

        if(aa==1)
        {
            scanf("%d%d",&bb,&cc);
            bb=(bb+ans*ty)%n+1;
            cc=(cc+ans*ty)%n+1;
            if(bb>cc) swap(bb,cc);
            ans=getans(1,1,n,bb,cc);
            printf("%d\n",ans); 
            rt[i]=rt[i-1];
        }
        else if(aa==2)
        {
            scanf("%d",&bb);
            bb=(bb+ans*ty)%n+1;
            del(bb,i);
        }
        else if(aa==3)
        {
            scanf("%d%d%d",&bb,&cc,&dd);
            bb=(bb+ans*ty)%n+1;
            cc=(cc+ans*ty)%n+1;
            if(bb>cc) swap(bb,cc);
            change(bb,cc,dd,i);
        }
    }

} 

Guess you like

Origin blog.csdn.net/ALPS233/article/details/51956364