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

What color do eagles...

The Incredible Vision of Eagles: Beyond the Human SpectrumEagles...

Northeast Texas lithium production...

The Surge of Lithium Extraction in Northeast Texas The landscape...

AgelessRx 2026: Longevity Telehealth...

AgelessRx 2026: Revolutionizing Longevity and Anti-Aging Telehealth As we move...

Can you release pet...

Why Releasing Pet Fish Into the Wild Is an...

Law & Order: SVU...

The Evolution of Captain Curry in Law & Order:...

Did humans cause coral...

Did Humans Cause Coral Bleaching? Exploring the Scientific TruthCoral...

What color do eagles see?

The Incredible Vision of Eagles: Beyond the Human SpectrumEagles have long been celebrated as the masters of the sky, but their prowess isn't just...

Northeast Texas lithium production picking up, could play major role in energy independence

The Surge of Lithium Extraction in Northeast Texas The landscape of American energy is shifting, and surprisingly, the spotlight has turned toward Northeast Texas. Historically...

AgelessRx 2026: Longevity Telehealth Platform Overview — Treatments, Pricing, Compounded Medications, and What Prospective Patients Should Know

AgelessRx 2026: Revolutionizing Longevity and Anti-Aging Telehealth As we move into 2026, the quest for a longer, healthier life has transitioned from a niche pursuit...

Can you release pet fish into the wild?

Why Releasing Pet Fish Into the Wild Is an Environmental DisasterReleasing a pet fish into a local pond, lake, or river might seem like...

Law & Order: SVU Star Talks Learning From The ‘Best In The Business,’ But How Much Does Curry Have Benson’s Back?

The Evolution of Captain Curry in Law & Order: SVU"Law & Order: Special Victims Unit" has long been celebrated for its ability to balance...

Did humans cause coral bleaching?

Did Humans Cause Coral Bleaching? Exploring the Scientific TruthCoral reefs are often called the “rainforests of the sea,” providing essential habitats for roughly a...

The true meaning of AUKUS, the ABC’s new Q+A is a turn off, and news.com’s TikTok mystery

Decoding the Shift in Australian Media and Global Strategy Recent developments across the Australian media and political landscape have sparked significant debate, ranging from the...

Beijing warns of more chip supply worries after Nexperia China claims it was cut off from SAP

Beijing Warns of New Volatility in Global Semiconductor Markets The global semiconductor landscape is facing a fresh wave of uncertainty as China’s Ministry of Commerce...

Should I fear raccoons?

Understanding Raccoon Behavior: Safety and Awareness Raccoons are some of the most recognizable urban wildlife in North America. With their distinctive black masks and ringed...