Summary & p1903 [national training team] data color/maintenance queue

Source: Internet
Author: User
Time Complexity

Complexity of multi-dimensional teamsSimilar\ (O (N ^ {\ frac {2k-1} {k }})\)
From zhihu daxie

Parity classification Optimization
return a.l == b.l ? (a.l & 1) ? a.r<b.r: a.r>b.r  :  a.l < b.l;

This is faster because the pointer does not need to jump back to the left after it is moved to the right, and the next block needs to jump back to the right after it is jumped back to the left. This reduces the number of operations by half and doubles theoretically.


After a long time on the subject, we only need to click here.
This topic is also used for non-Conference events with modifications.
I made another step today, so I have a new understanding.

In fact, a timestamp is added to the non-modified pair.
It's okay to move the modification time with the time.
Although the complexity will be higher (so it is very slow to wait for modification)

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define FOR(i,a,b) for(int i=a;i<=b;++i)using namespace std;const int maxn=5e4+7;const int maxm=1e6+7;char s;int n,m,A,B,a[maxn],belong[maxn],vis[maxm],ans;struct node {    int x,y,id,tim,ans;}Q[maxn];struct modify {    int id,v;}C[maxn];bool cmp1(const node &a,const node &b) {    return belong[a.x]==belong[b.x] ? (belong[a.y]==belong[b.y] ? a.tim<b.tim : a.y<b.y) : a.x<b.x;}bool cmp2(const node &a,const node &b) {    return<;}int read() {    int x=0,f=1;char s=getchar();    for(;s<'0'||s>'9';s=getchar()) if(s=='-') f=-1;    for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';    return x*f;}void add(int x) {    if(!vis[a[x]]) ++ans;    ++vis[a[x]];}void delet(int x) {    --vis[a[x]];    if(!vis[a[x]]) --ans;}void work(int x,int i){    if(Q[i].x<=C[x].id && C[x].id<=Q[i].y) {        --vis[a[C[x].id]];        ++vis[C[x].v];        if(!vis[a[C[x].id]]) --ans;        if(vis[C[x].v]==1) ++ans;    }    swap(C[x].v,a[C[x].id]);}int main() {    n=read(),m=read();    int k=sqrt(n)*7;    FOR(i,1,n) a[i]=read();    FOR(i,1,n) belong[i]=(i-1)/k+1;    FOR(i,1,m) {        scanf("%s",&s);        if(s=='Q') {            Q[++A].id=A;            Q[A].tim=B;            Q[A].x=read();            Q[A].y=read();        } else {            C[++B].id=read();            C[B].v=read();        }    }    sort(Q+1,Q+1+A,cmp1);    int l=1,r=0,ttt=0;    FOR(i,1,A) {        while(Q[i].x<l) add(--l);        while(Q[i].x>l) delet(l++);        while(Q[i].y>r) add(++r);        while(Q[i].y<r) delet(r--);        while(Q[i].tim>ttt) work(++ttt,i);        while(Q[i].tim<ttt) work(ttt--,i);        Q[i].ans=ans;    }    sort(Q+1,Q+1+A,cmp2);    FOR(i,1,A) cout<<Q[i].ans<<"\n";    return 0;}

Summary & p1903 [national training team] data color/maintenance queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.