Test data structure 2 on 2019.9.25

It is the data structure of the day, but direct explosion today, 3 1 title and title are written hang 200-> 0.

T1 abnormal

 

 I began to think that a tree line, but he felt no maintenance to decisively abandon segment tree. Then they thought of Mo offline team practice, I thought This question is stable, and the results finally beat explode. Meaning face questions are wrong, really is a sucker.

This question is for the team in terms of Mo Mo is the easiest team, with an array cnt to maintain the number of occurrences of each Charisma can, but at the time pay attention to the number of occurrences statistics, or to increase attention, because the topics defined abnormal gang is only appear twice, so less than two and have accumulated more than twice their contributions. It requires little special sentence.

code show as below:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
struct node{
    int l,r,id;
    long long ans;
}mo[maxn*4];
int belong[maxn];
int block;
bool cmp(node a,node b){
    return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
} 
int cnt[maxn];
long long ans;
int a[maxn];
int vis[maxn];
inline void add(int x){
    cnt[a[x]]++;
    if(cnt[a[x]]>3) ans+=a[x];
    else if(cnt[a[x]]==3) ans+=a[x]*cnt[a[x]];
    else if(cnt[a[x]]==2) ans-=a[x];
    else ans+=a[x];
}
inline void del(int x){
    cnt[a[x]]--;
    if(cnt[a[x]]>2) ans-=a[x];
    else if(cnt[a[x]]==2) ans-=3*a[x];
    else if(cnt[a[x]]==1) ans+=a[x];
    else ans-=a[x];
}
int n,m;
long long all[maxn];
int main(){
    scanf("%d%d",&n,&m);
    block=sqrt(n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        belong[i]=(i-1)/block+1;
        mo[i].id=i;
    }
    int l=1,r=0;
    for(int i=1;i<=m;i++) scanf("%d%d",&mo[i].l,&mo[i].r);
    sort(mo+1,mo+1+m,cmp);
    //for(int i=1;i<=m;i++) printf("%d %d\n",mo[i].l,mo[i].r); 
    for(int i=1;i<=m;i++){
        while(l<mo[i].l) del(l++);
        while(r>mo[i].r) del(r--);
        while(l>mo[i].l) add(--l);
        while(r<mo[i].r) add(++r);
        all[mo[i].id]=ans;
    }
    for(int i=1;i<=m;i++) printf("%lld\n",all[i]);
    return 0;
}
View Code

T2 irregular

Dsu on tree to be modified

T3 unnormal

 

 median? Direct vigorously balanced tree. But in the end the whole wa? Why, longlong not take the time to take 1ll see fathers.

But the only thing I'll balance the tree is splay, splay constant and large, in terms of this question last card splay, how cards are cards however.

90 minutes following code (number plus or had not optimized):

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define Ri register int  
#define sswap(x,y) x^=y^=x^=y;
template <class T> T mmin(T x,T y){return(x)<(y)?(x):(y);}
template <class T> T mmax(T x,T y){return(x)<(y)?(y):(x);}
template <class T> T lowbit(T x){return ((x)&(-(x)));}
typedef long long ll;
using namespace std;
namespace io{
    const int MT=5e7;
    char buf[MT];ll c,sz;
    void begin(){c=0;sz=fread(buf,1,MT,stdin);}
    template<class T>
    inline bool read(T &t) 
    {
        while(c<sz&&buf[c]!='-'&&(buf[c]<'0'||buf[c]>'9'))c++;
        if(c>=sz)return false;
        bool flag=0;if(buf[c]=='-')flag=1,c++;
        for(t=0;c<sz&&'0'<=buf[c]&&buf[c]<='9';c++)t=t*10+buf[c]-'0';
        if(flag==1)t=-t;return true;
    }
}
template <typename _TpInt>
inline void write(_TpInt x)
{
    if (x<0){
        putchar('-');
        write<_TpInt>(~x+1);
    }
    else {
        if (x>9)write<_TpInt>(x/10);   
        putchar(x%10+'0');
    }
}
const int maxn=1e6+7;
const int inf=0x7fffffff;
const int mod=1e9+7;
int ch[maxn][2],fa[maxn],siz[maxn],cnt[maxn];
long long key[maxn];
int rt,sz;
long long fi;
long long sum;
int a,b,c,n;
long long min(long long a,long long b){
    return a<b?a:b;
}
bool check(int x){
    return ch[fa[x]][1]==x;
}
void pushup(int x){
    siz[x]=siz[ch[x][1]]+siz[ch[x][0]]+cnt[x];
}
void rotate(int x){
    int y=fa[x],z=fa[y],who=check(x);
    ch[y][who]=ch[x][who^1];
    fa[ch[y][who]]=y;
    ch[x][who^1]=y;
    fa[y]=x,fa[x]=z;
    if(z) ch[z][ch[z][1]==y]=x;
    pushup(y);pushup(x);
}
void splay(int x){
    for(int f;(f=fa[x]);rotate(x)){
        if(fa[f]) rotate((check(x)==check(f))?f:x);  
    }
    rt=x;
} 
void insert(int x){
    if(!rt){
        rt=++sz;
        key[sz]=x;
        siz[sz]=cnt[sz]=1;
        return;
    }
    int now=rt,f=0;
    while(1){
        if(x==key[now]){
            cnt[now]++;
            pushup(f);
            pushup(now);
            splay(now);
            return;
        }
        f=now,now=ch[now][x>key[now]];
        if(!now){
            sz++;
            fa[sz]=f;
            siz[sz]=cnt[sz]=1;
            ch[f][x>key[f]]=sz;
            key[sz]=x;
            pushup(f);
            splay(sz);
            return;
        }
    }
}
long long rnk(int x){
    int now=rt;
    while(1){
        if(ch[now][0]&&x<=siz[ch[now][0]]) now=ch[now][0];
        else{
            int tmp=siz[ch[now][0]]+cnt[now];
            if(tmp>=x) return key[now];
            x-=tmp;
            now=ch[now][1];
        }
    }
} 
int main(){
    scanf("%d%d%d%d",&a,&b,&c,&n);
    insert(1);
    sum=1;
    for(int i=2;i<=n;i++){
        int wz=(i/2);
        long long mi=rnk(wz);
        fi=(long long)(1ll*a*mi+1ll*b*i+c)%mod;
        sum+=fi;
        insert(fi);
    }
    printf("%lld\n",sum);
    return 0;
} 
/*
64582 34650 2040 100000
41492359064511
*/
View Code

 

Guess you like

Origin www.cnblogs.com/LJB666/p/11614644.html