Sponsors

Codeforces Round #416 (Div. 2), problem: (E) Vladik and Entertaining Flags Solution In C/C++

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define Rep(i,x,y) for(int i=x;i<y;++i)
#define For(i,x,y) for(int i=x;i<=y;++i)
#define Forn(i,x,y) for(int i=x;i>=y;–i)
const int N = 1e5+5;
typedef vector<int> Vi;
typedef pair<int,int> pii;
int n,m,q;
long long nowans;
struct dat{
int l[11];
int r[11];
int num;
};
int a[11][N];
int f[50],id[50];
int fa(int x){
return f[x]==x?x:f[x]=fa(f[x]);
}
dat b[N];
dat merge(const dat&x,const dat&y,int m){
For(i,1,n*4) f[i]=i;
//int s=x.num+y.num;
dat res;
res.num=x.num+y.num;
For(i,1,n){
if(a[i][m]==a[i][m+1]&&fa(x.r[i])!=fa(y.l[i]+n+n)){
–res.num;
f[fa(x.r[i])]=fa(y.l[i]+n+n);
}
}
For(i,1,(n*4)) id[i]=0;
int cnt=0;
For(i,1,n){
int t=fa(x.l[i]);
if(!id[t]) id[t]=++cnt;
res.l[i]=id[t];
}
For(i,1,n){
int t=fa(y.r[i]+n+n);
if(!id[t]) id[t]=++cnt;
res.r[i]=id[t];
}
return res;
}
struct Segment_Tree{
dat w[N<<2];
void build(int p,int L,int R){
if(L==R){
int cnt=0;
For(i,1,n){
if(i==1||a[i][L]!=a[i-1][L]) ++cnt;
w[p].l[i]=w[p].r[i]=cnt;
}
w[p].num=cnt;
return;
}
int M=L+R>>1;
build(p<<1,L,M);
build(p<<1|1,M+1,R);
w[p]=merge(w[p<<1],w[p<<1|1],M);
}
dat query(int p,int L,int R,int x,int y){
if(x<=L&&R<=y) return w[p];
int M=L+R>>1;
if(y<=M) return query(p<<1,L,M,x,y);
if(x> M) return query(p<<1|1,M+1,R,x,y);
return merge(query(p<<1,L,M,x,y),query(p<<1|1,M+1,R,x,y),M);
}
}T;
int main(){
scanf(“%d%d%d”,&n,&m,&q);
For(i,1,n) For(j,1,m) scanf(“%d”,&a[i][j]);
T.build(1,1,m);
while(q–){
int l,r;
scanf(“%d%d”,&l,&r);
nowans=0;
printf(“%d\n”,T.query(1,1,m,l,r).num);
}
return 0;
}

Ronda Rousey vs. Gina...

The Dream Match: Ronda Rousey vs. Gina Carano at...

Why Everyone is Obsessed with the Portable Handheld Misting Fan

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Ronda Rousey vs. Gina Carano MVP MMA live blog

The Dream Match: Ronda Rousey vs. Gina Carano at MVP MMA The world of mixed martial arts is currently witnessing a moment many thought would...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Sunset Projection Lamp LED

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Cordless

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

The Ultimate Guide to the Keyboard and Car Universal Cleaning Gel

I discovered the Keyboard and Car Universal Cleaning Gel and it is a total game-changer for my daily routine. Here is why you need...