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

One to watch: Macquarie...

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group,...

THE FREEPER CANTEEN –...

The Freeper Canteen: A Look That Up, It Must...

215% PENGU Rally Incoming?...

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency...

Junk is the new...

Junk is the New Punk: Why We're Falling Back...

Are weighted vests good...

Unlocking the Power of AI-Generated Content for SEO In today's...

Are weighted vests good...

Unlocking the Power of AI-Generated Content: A Boon or...

One to watch: Macquarie tips double-digit returns for this ASX healthcare stock

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group, a prominent Australian financial services company, has issued a bullish prediction for a specific ASX-listed...

THE FREEPER CANTEEN – The Look That Up, It Must Be On My Bucket List Quiz – Wednesday, August 27, 2025

The Freeper Canteen: A Look That Up, It Must Be On My Bucket List The Freeper Canteen, a unique online community, has recently launched a...

215% PENGU Rally Incoming? Analyst Says Token ‘Inches’ From Next Leg Up

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency market is constantly fluctuating, and predicting the next big move is a challenge. However, one...

Junk is the new punk: Why we’re falling back in love with retro tech

Junk is the New Punk: Why We're Falling Back in Love with Retro Tech In a world dominated by sleek, minimalist designs and cutting-edge technology,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content for SEO In today's digital landscape, search engine optimization (SEO) is paramount for online success. But creating high-quality,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content: A Boon or Bane for SEO? The world of search engine optimization (SEO) is constantly evolving, and the recent...

TSMC cuts Chinese tools from cutting-edge chip production to avoid US ire – Nikkei Asia

TSMC Ditches Chinese Tools for Cutting-Edge Chip Production Taiwan Semiconductor Manufacturing Company (TSMC), the world's leading chipmaker, is reportedly phasing out Chinese-made equipment from its...

Google Messages beta rolling out QR code key verification – 9to5Google

Google Messages Adds QR Code Key Verification for Enhanced Security Google is bolstering the security of its Messages app with a new feature rolling out...

A Secretive US Space Plane Will Soon Test Quantum Navigation Technology

## US Space Plane to Test Revolutionary Quantum Navigation Technology The United States is on the cusp of a significant leap forward in space navigation....