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;
}

China’s three telecom giants...

China’s telecommunications landscape is undergoing a monumental paradigm shift....

Fangzhou Ranked among 2025...

In a significant milestone for the digital healthcare sector,...

Why Everyone is Obsessed with the Wireless Karaoke Microphone

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 LED Lamp

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

China’s three telecom giants race into the AI token economy

China’s telecommunications landscape is undergoing a monumental paradigm shift. The nation's three state-owned telecom giants—China Mobile, China Telecom, and China Unicom—are aggressively moving beyond...

Why Everyone is Obsessed with the USB Rechargeable Lighter

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

Why Everyone is Obsessed with the AeroPress Coffee Maker

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

Fangzhou Ranked among 2025 Guangzhou AI Innovation “Most Promising Enterprises” for AI‑Powered Chronic Disease Services

In a significant milestone for the digital healthcare sector, Fangzhou has been officially ranked among the 2025 Guangzhou AI Innovation 'Most Promising Enterprises' list....

Why Everyone is Obsessed with the Universal Car Cleaning Gel

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

Why Everyone is Obsessed with the Portable Door Lock for Travel

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

Infected with TDSS and Google Keeps redirecting

Dealing with TDSS Rootkit Infections and Google Search Redirects Computer security is a continuous battle, and some threats are significantly more stubborn than others. A...