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

S-Works Tarmac SL8 Shimano...

Experience the Ultimate Speed: S-Works Tarmac SL8 Now on...

NIPOST boss urges harmony...

NIPOST Boss Tola Odeyemi Advocates for Industrial Harmony to...

Why I dumped athletics...

From Shot Put Circles to the WWE Ring: The...

Quote of the Day...

The Wisdom of Albert Einstein: Rediscovering the Balance Between...

April Singer Brand Reputation...

April Singer Brand Reputation Rankings AnnouncedThe Korean Business Research...

7 Ways To Build...

How to Build an Inflation-Resistant Business In the ever-changing landscape...

S-Works Tarmac SL8 Shimano Dura-Ace Di2 $17,999.99 (down from $20,900.00)

Experience the Ultimate Speed: S-Works Tarmac SL8 Now on Massive Sale In the world of high-performance cycling, few names carry as much weight—or as little...

NIPOST boss urges harmony for effective service delivery

NIPOST Boss Tola Odeyemi Advocates for Industrial Harmony to Boost Service DeliveryIn a strategic move aimed at revitalizing Nigeria’s postal services, the Postmaster-General and...

Why I dumped athletics for WWE – Oba Femi

From Shot Put Circles to the WWE Ring: The Evolution of Oba FemiIn the world of professional wrestling, few stars have risen as quickly...

Quote of the Day by Albert Einstein: “The intuitive mind is a sacred gift and the rational mind is a…..” What powerful life lessons...

The Wisdom of Albert Einstein: Rediscovering the Balance Between Intuition and Logic Albert Einstein is widely regarded as one of the greatest minds in human...

April Singer Brand Reputation Rankings Announced

April Singer Brand Reputation Rankings AnnouncedThe Korean Business Research Institute has officially released the brand reputation rankings for singers for the month of April....

7 Ways To Build Your Business To Withstand Inflation

How to Build an Inflation-Resistant Business In the ever-changing landscape of the global economy, inflation remains a persistent challenge that business owners must navigate with...

Shohei Ohtani might be held out of the lineup during more pitching starts, Dodgers manager says

Dodgers Strategy: Managing Shohei Ohtani’s Health During Pitching Starts The Los Angeles Dodgers are prioritizing the long-term health and performance of their superstar, Shohei Ohtani,...

LG G6 vs. LG G5: I compared the latest OLED TV models, and it’s a surprisingly tough choice

LG G6 vs. LG G5 OLED TV: Which Gallery Series Model Is Right for You? LG has long been a leader in the OLED television...

Mystery Science Theater 3000: The Movie Proved That “Mainstream” Isn’t Always Better

Mystery Science Theater 3000: The Movie Proved That "Mainstream" Isn't Always BetterReleased thirty years ago, Mystery Science Theater 3000: The Movie serves as a...