C++ 复制 #include <iostream>
#define ls (now *2)
#define rs (now *2+1)
#define len (x ) (T[x].r- T[x].l+ 1 )
#define int long long
using namespace std ;
const int N = 1 e 5 + 5 ;
int n,q,m,a[N];
struct eerT {
int l,r,tag,tagx= 1 ,sum;
}T[N* 4 ];
void Build (int now ,int l ,int r ){
T[now].l = l,T[now].r = r;
if (l== r){
T[now].sum = a[l]% m;
return ;
}
int mid = (l+ r)/ 2 ;
Build (ls,l,mid);
Build (rs,mid+ 1 ,r);
T[now].sum = (T[ls].sum + T[rs].sum)% m;
}
void pushdown (int now ){
#define k (x ) T[x].tag
#define kx (x ) T[x].tagx
T[ls].sum = (T[ls].sum* kx (now)% m+ k (now)* len (ls)% m)% m;
T[rs].sum = (T[rs].sum* kx (now)% m+ k (now)* len (rs)% m)% m;
kx (ls) = kx (ls)* kx (now)% m;
kx (rs) = kx (rs)* kx (now)% m;
k (ls) = (k (ls)* kx (now)% m+ k (now)% m)% m;
k (rs) = (k (rs)* kx (now)% m+ k (now)% m)% m;
k (now) = 0 ,kx (now) = 1 ;
#undef k
#undef kx
}
void Plus (int now ,int l ,int r ,int k ){
if (T[now].r< l|| T[now].l> r) return ;
if (l<= T[now].l&& T[now].r<= r){
T[now].tag = (T[now].tag+ k)% m;
T[now].sum = (T[now].sum+ k* len (now)% m)% m;
return ;
}
pushdown (now);
Plus (ls,l,r,k);
Plus (rs,l,r,k);
T[now].sum = (T[ls].sum + T[rs].sum)% m;
}
void Times (int now ,int l ,int r ,int k ){
if (r< T[now].l|| l> T[now].r) return ;
if (l<= T[now].l&& T[now].r<= r){
T[now].tagx = T[now].tagx* k% m;
T[now].sum = T[now].sum* k% m;
T[now].tag = T[now].tag* k% m;
return ;
}
pushdown (now);
Times (ls,l,r,k);
Times (rs,l,r,k);
T[now].sum = (T[ls].sum + T[rs].sum)% m;
}
int Query (int now ,int l ,int r ){
if (T[now].r< l|| T[now].l> r) return 0 ;
if (l<= T[now].l&& T[now].r<= r) return T[now].sum% m;
pushdown (now);
return (Query (ls,l,r)+ Query (rs,l,r))% m;
}
signed main (){
cin >> n >> q >> m;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i];
}
Build (1 ,1 ,n);
while (q-- ){
int opt,x,y,k= 0 ;
cin >> opt >> x >> y;
if (opt== 1 ){
cin >> k;
Times (1 ,x,y,k% m);
}
else if (opt== 2 ){
cin >> k;
Plus (1 ,x,y,k% m);
}
else {
cout << Query (1 ,x,y)% m << endl;
}
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <cmath>
using namespace std ;
const double INF= 1 e- 7 ;
const int N= 15 ;
int n;
double x,y,k[N];
double calc (double x ){
double arr = 0 ;
for (int i= 0 ;i<= n;i++ ){
arr += k[i]* pow (x,i);
}
return arr;
}
int main ()
{
cin >> n >> x >> y;
for (int i= n;i>= 0 ;i-- ){
cin >> k[i];
}
double l = x,r = y, lmid = 0 , rmid = 0 ;
while (l+ INF<= r){
lmid = l + (r- l)/ 3 ;
rmid = r - (r- l)/ 3 ;
if (calc (lmid)> calc (rmid)) r = rmid;
else l = lmid;
}
printf (" %.5f\n " ,(rmid+ lmid)/ 2 );
return 0 ;
}
C++ 复制 #include <iostream>
using namespace std ;
const int N = 3 e 6 + 5 ;
int kT,n,q,to[N][100 ],cnt,sum[N][2 ];
string k;
int td (char k ){
if (k<= '9' && k>= '0' ) return k- '0' ;
if (k<= 'z' && k>= 'a' ) return k- 'a' + 10 ;
if (k<= 'Z' && k>= 'A' ) return k- 'A' + 36 ;
}
void add (string k ){
int now = 0 ;
for (auto x:k){
if (! to[now][td (x)]) to[now][td (x)] = ++ cnt;
now = to[now][td (x)];
if (sum[now][1 ]!= kT){
sum[now][0 ] = 0 ;
sum[now][1 ] = kT;
}
sum[now][0 ]++ ;
}
}
int query (string k ){
int now = 0 ;
for (auto x:k){
now = to[now][td (x)];
if (! now|| sum[now][1 ]!= kT) return 0 ;
}
return sum[now][0 ];
}
int main (){
cin >> kT;
while (kT-- ){
cin >> n >> q;
for (int i= 1 ;i<= n;i++ ){
cin >> k;
add (k);
}
while (q-- ){
cin >> k;
cout << query (k) << endl;
}
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
using namespace std ;
const int N = 5 e 5 + 5 ;
int n,m,s,dep[N],fa[N][32 ],lg2[N];
vector<int> T[N];
void dfs (int now ,int from ){
fa[now][0 ] = from, dep[now] = dep[from]+ 1 ;
for (int i= 1 ;(1 << i)<= dep[now];i++ ){
fa[now][i] = fa[fa[now][i- 1 ]][i- 1 ];
}
for (auto x:T[now]){
if (x== from) continue ;
dfs (x,now);
}
}
int lca (int x ,int y ){
if (dep[x]< dep[y]) swap (x,y);
while (dep[x]> dep[y]){
x = fa[x][lg2[dep[x]- dep[y]]];
}
if (x== y) return x;
for (int i= lg2[dep[x]];i>= 0 ;i-- ){
if (fa[x][i]== fa[y][i]) continue ;
x = fa[x][i];
y = fa[y][i];
}
return fa[x][0 ];
}
int main (){
cin >> n >> m >> s;
for (int i= 2 ;i<= n;i++ ){
lg2[i] = lg2[i/ 2 ]+ 1 ;
}
for (int i= 1 ;i< n;i++ ){
int x,y;
cin >> x >> y;
T[x].push_back (y);
T[y].push_back (x);
}
dfs (s,0 );
while (m-- ){
int a,b;
cin >> a >> b;
cout << lca (a,b) << endl;
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
using namespace std ;
const int N = 5 e 5 + 5 ;
int n,m,s,f[N],arr[N];
bool vis[N];
vector<int> T[N];
vector< pair<int ,int>> query[N];
int iFind (int x ){
return (f[x]== x? x: (f[x]= iFind (f[x])));
}
void tarjan (int now ){
vis[now] = 1 ;
for (auto x:T[now]){
if (vis[x]) continue ;
tarjan (x);
f[x] = now;
}
for (auto u:query[now]){
int v = u.first, i = u.second;
if (vis[v]) arr[i] = iFind (v);
}
}
int main (){
cin >> n >> m >> s;
for (int i= 1 ;i<= n;i++ ) f[i] = i;
for (int i= 1 ;i< n;i++ ){
int u,v;
cin >> u >> v;
T[u].push_back (v);
T[v].push_back (u);
}
for (int i= 1 ;i<= m;i++ ){
int a,b;
cin >> a >> b;
query[a].push_back ({b,i});
query[b].push_back ({a,i});
}
tarjan (s);
for (int i= 1 ;i<= m;i++ ){
cout << arr[i] << endl;
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
using namespace std ;
const int N = 5 e 5 + 5 ;
int n,m,s,cnt;
int fa[N],dep[N],siz[N],son[N],top[N],dfn[N],seg[N];
vector<int> to[N];
void dfs1 (int now ,int from ){
fa[now] = from;
dep[now] = dep[from]+ 1 ;
siz[now] = 1 ;
for (auto x:to[now]){
if (x== from) continue ;
dfs1 (x,now);
siz[now] += siz[x];
if (siz[x]> siz[son[now]]) son[now] = x;
}
}
void dfs2 (int now ,int head ){
top[now] = head;
dfn[now] = ++ cnt;
seg[cnt] = now;
if (son[now]) dfs2 (son[now],head);
for (auto x:to[now]){
if (x== fa[now]|| x== son[now]) continue ;
dfs2 (x,x);
}
}
int opt1 (int x ,int y ){
while (top[x]!= top[y]){
if (dep[top[x]]< dep[top[y]]) swap (x,y);
x = fa[top[x]];
}
return (dep[x]< dep[y]? x: y);
}
signed main (){
cin >> n >> m >> s;
for (int i= 1 ;i< n;i++ ){
int u,v;
cin >> u >> v;
to[u].push_back (v);
to[v].push_back (u);
}
dfs1 (s,0 );
dfs2 (s,s);
while (m-- ){
int x,y;
cin >> x >> y;
cout << opt1 (x,y) << endl;
}
return 0 ;
}
C++ 复制 #include <iostream>
using namespace std ;
const int N = 1 e 6 + 5 ;
string s,t;
int n,m,fail[N];
int main (){
cin >> s >> t;
n = s.size (),m = t.size ();
s = '$' + s, t = '$' + t;
for (int i= 2 ,j= 0 ;i<= m;i++ ){
while (j&& t[i]!= t[j+ 1 ]) j = fail[j];
if (t[i]== t[j+ 1 ]) j++ ;
fail[i] = j;
}
for (int i= 1 ,j= 0 ;i<= n;i++ ){
while (j&& s[i]!= t[j+ 1 ]) j = fail[j];
if (s[i]== t[j+ 1 ]) j++ ;
if (j== m){
cout << i- j+ 1 << endl;
j = fail[j];
}
}
for (int i= 1 ;i<= m;i++ ) cout << fail[i] << ' ' ;
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std ;
const int N = 1 e 5 + 5 ;
int n,m,v[N],cnt,dfn[N],low[N],inn[N],outt[N],scc[N],tot;
bitset< N> instack;
stack<int> f;
vector<int> T[N];
vector<int> nT[N];
int a[N],dp[N],arr;
void tarjan (int u ){
dfn[u] = low[u] = ++ tot;
instack[u] = 1 ;
f.push (u);
for (auto v:T[u]){
if (! dfn[v]){
tarjan (v);
low[u] = min (low[u],low[v]);
}
else if (instack[v]){
low[u] = min (low[u],dfn[v]);
}
}
if (dfn[u]== low[u]){
int y = 0 ;
cnt++ ;
do {
y = f.top ();
f.pop ();
instack[y] = 0 ;
scc[y] = cnt;
a[cnt] += v[y];
}while (y!= u);
// cout << endl;
}
}
int main (){
cin >> n >> m;
for (int i= 1 ;i<= n;i++ ){
cin >> v[i];
}
for (int i= 1 ;i<= m;i++ ){
int u,v;
cin >> u >> v;
T[u].push_back (v);
}
for (int i= 1 ;i<= n;i++ ){
if (dfn[i]) continue ;
tarjan (i);
}
for (int u= 1 ;u<= n;u++ ){
for (auto v:T[u]){
if (scc[u]== scc[v]) continue ;
nT[scc[u]].push_back (scc[v]);
}
}
for (int u= 1 ;u<= n;u++ ){
dp[u] = a[u];
for (auto v:nT[u]){
dp[u] = max (dp[u],dp[v]+ a[u]);
}
arr = max (arr,dp[u]);
}
cout << arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#define int unsigned long long
#define gc () (p1== p2&& (p2= (p1= buf)+ fread (buf,1 ,1 << 21 ,stdin),p1== p2)? EOF:* p1++ )
using namespace std ;
const int N = 1 e 6 ;
struct Node {
vector<int> k;
vector<int> id;
}mp[N];
int n,cnt,arr;
char buf[1 << 23 ],* p1= buf,* p2= buf;
inline unsigned long long rd () {
unsigned long long x= 0 ;
char ch= gc ();
while (! isdigit (ch))ch= gc ();
while (isdigit (ch)) x= x* 10 + (ch^ 48 ),ch= gc ();
return x;
}
int hsh (int k ){
int res1 = 0 ,base1 = 277 ,mod1 = 1 e 5 - 9 ;
while (k){
int r = k% 10 ;
res1 = (res1* base1+ r)% mod1;
k/= 10 ;
}
return res1;
}
signed main (){
n = rd ();
for (int i= 1 ;i<= n;i++ ){
int x= rd (),y= rd (),ok= 1 ;
int h = hsh (x);
for (int j= 0 ;j< mp[h].id.size ();j++ ){
if (mp[h].id[j]!= x) continue ;
arr += i* mp[h].k[j];
mp[h].k[j] = y;
ok= 0 ;
break ;
}
if (ok){
mp[h].id.push_back (x);
mp[h].k.push_back (y);
}
}
cout << arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#include <cmath>
using namespace std ;
const int N = 25 ;
int n,a[N],x,y,arr;
int igcd (int a ,int b ){
if (! b) return a;
return igcd (b,a% b);
}
int main (){
cin >> n;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i];
}
arr = igcd (a[1 ],a[2 ]);
for (int i= 2 ;i< n;i++ ){
arr = igcd (arr,igcd (a[i],a[i+ 1 ]));
}
cout << abs (arr) << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#define int long long
#define lowbit (x ) (x&- x)
using namespace std ;
const int N = 1 e 6 + 5 ;
const int M = 998244353 ;
int n,q,a[N],c[N],jc[N],arr;
void add (int x ,int k ){
for (;x<= n;x+= lowbit (x)) c[x]+= k;
}
int query (int x ){
int arr = 0 ;
for (;x>= 1 ;x-= lowbit (x)) arr+= c[x];
return arr;
}
signed main (){
cin >> n;
jc[0 ] = 1 ;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i];
add (i,1 );
jc[i] = jc[i- 1 ]* i% M;
}
for (int i= 1 ;i<= n;i++ ){
arr += query (a[i]- 1 )* jc[n- i];
add (a[i],- 1 );
arr %= M;
}
cout << (arr+ 1 )% M << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std ;
const int N = 2 e 5 + 5 ;
struct Node {
int x,y,v;
}T[N];
int n,m,fa[N],arr;
bool cmp (Node T1 ,Node T2 ){
return T1.v < T2.v;
}
int iFind (int x ){
return ((fa[x]== x)? x: (fa[x]= iFind (fa[x])));
}
int main (){
cin >> n >> m;
for (int i= 1 ;i<= n;i++ ){
fa[i] = i;
}
for (int i= 1 ;i<= m;i++ ){
int x,y,z;
cin >> x >> y >> z;
T[i] = (Node){x,y,z};
}
sort (T+ 1 ,T+ 1 + m,cmp);
for (int i= 1 ;i<= m;i++ ){
int x = T[i].x,y= T[i].y;
int nx = iFind (fa[x]),ny= iFind (fa[y]);
if (nx== ny){
continue ;
}
fa[nx] = ny;
arr+= T[i].v;
}
bool tot = 0 ;
for (int i= 1 ;i<= n;i++ ){
if (fa[i]== i){
if (! tot){
tot= 1 ;
continue ;
}
cout << "orz" << endl;
return 0 ;
}
}
cout << arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#define lowbit (x ) (x&- x)
using namespace std ;
const int N = 1 e 6 + 5 ;
int n,q,a[N],c[N];
void add (int x ,int k ){
for (;x<= n;x+= lowbit (x)) c[x]+= k;
}
int query (int x ){
int arr = 0 ;
for (;x;x-= lowbit (x)) arr+= c[x];
return arr;
}
int main (){
cin >> n >> q;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i];
add (i,a[i]);
}
while (q-- ){
int l,r,opt;
cin >> opt >> l >> r;
if (opt== 1 ){
add (l,r);
}
else {
cout << query (r)- query (l- 1 ) << endl;
}
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <cstdio>
using namespace std ;
const int N = 1 e 8 + 5 ;
int n,q,cnt,b[N];
bool a[N];
int main (){
scanf (" %d%d " ,& n,& q);
a[1 ] = 1 ;
for (int i= 2 ;i<= n;i++ ){
if (! a[i]){
b[++ cnt] = i;
}
for (int j= 1 ;j<= cnt&& i* b[j]<= n;j++ ){
a[i* b[j]] = 1 ;
if (i% b[j]== 0 ){
break ;
}
}
}
// \
由1-n遍历 b一定升序 \
则小于等于i的质数一定在b中 \
即此合数的最小质因子一定在b中 \
(否则i的最小质因子将成为该合数的最小质因子) \
即1-n任意整数*n下任意质数 \
则一定能筛1-n所有合数
while (q-- ){
int k;
scanf (" %d " ,& k);
printf (" %d\n " ,b[k]);
}
return 0 ;
}
/*
k = i*b[j]
b[j] -> gcd(k)
i -> k/gcd(k)
if i/m(m<b[j]) -> gcd(k) < b[j]
gcd(i) >= gcd(k)
no break until >=k
kv-
*/
C++ 复制 #include <iostream>
#include <cstring>
#define int long long
using namespace std ;
const int N = 1 e 9 + 7 ;
int m,n,k;
struct Node {
int a[105 ][105 ];
int m,n;
Node (){
memset (a,0 ,sizeof (a));
m = n = 0 ;
}
Node operator * (const Node & B ) const {
Node res;
res.m = m;
res.n = B.n;
for (int i= 1 ;i<= res.m;i++ ){
for (int j= 1 ;j<= res.n;j++ ){
for (int k= 1 ;k<= n;k++ ){
res.a[i][j] += (a[i][k]% N) * (B.a[k][j]% N);
res.a[i][j] %= N;
}
}
}
return res;
}
}kA,kB;
Node pow (Node kC ,int k ){
Node kF;
kF.m = kF.n = kC.m;
for (int i= 1 ;i<= kC.m;i++ ){
kF.a[i][i] = 1 ;
}
while (k){
//cout << k << endl;
if (k& 1 ){
kF = kF * kC;
}
kC = kC * kC;
k>>= 1 ;
}
return kF;
}
signed main (){
cin >> m >> k;
kA.m = m,kA.n = m;
for (int i= 1 ;i<= m;i++ ){
for (int j= 1 ;j<= m;j++ ){
cin >> kA.a[i][j];
}
}
kB = pow (kA,k);
for (int i= 1 ;i<= kB.m;i++ ){
for (int j= 1 ;j<= kB.n;j++ ){
cout << kB.a[i][j] << ' ' ;
}
cout << endl;
}
return 0 ;
}
C++ 复制 #include <iostream>
using namespace std ;
int x,y,a,b,n;
int exgcd (int a ,int b ,int & x ,int & y ){
if (! b){
x = 1 ,y = 0 ;
return a;
}
int d = exgcd (b,a% b,y,x);
y -= a/ b* x;
return d;
}
int main (){
cin >> n >> b;
for (int a= 1 ;a<= n;a++ ){
x = 0 ,y = 0 ;
exgcd (a,b,x,y);
cout << (x% b+ b)% b << endl;
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#define int unsigned int
using namespace std ;
const int N = 5 e 5 + 5 ;
int n,m,s,dis[N];
vector< pair<int ,int>> to[N];
bitset< N> vis;
queue<int> f;
signed main (){
cin >> n >> m >> s;
for (int i= 1 ;i<= n;i++ ) dis[i]=0x 7fffffff ;
for (int i= 1 ;i<= m;i++ ){
int u,v,w;
cin >> u >> v >> w;
to[u].push_back ({v,w});
}
vis[s] = 1 ,dis[s] = 0 ;
f.push (s);
while (! f.empty ()){
int u = f.front ();
vis[u] = 0 ;
f.pop ();
for (auto [v,w]:to[u]){
if (dis[v]<= dis[u]+ w) continue ;
dis[v] = dis[u] + w;
if (vis[v]) continue ;
vis[v] = 1 ;
f.push (v);
}
}
for (int i= 1 ;i<= n;i++ ) cout << dis[i] << ' ' ;
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std ;
const int N = 1 e 5 + 5 ;
const int INF = 0x 7fffffff ;
int n,m,s,dis[N];
bitset< N> vis;
vector< pair<int ,int>> T[N];
priority_queue< pair<int ,int> ,vector< pair<int ,int>> ,greater< pair<int ,int>>> f;
int main (){
cin >> n >> m >> s;
for (int i= 1 ;i<= n;i++ ) dis[i] = INF;
for (int i= 1 ;i<= m;i++ ){
int u,v,w;
cin >> u >> v >> w;
T[u].push_back ({v,w});
}
dis[s] = 0 ;
f.push ({0 ,s});
while (! f.empty ()){
int u = f.top ().second;
f.pop ();
if (vis[u]) continue ;
vis[u] = 1 ;
for (auto [v,w]:T[u]){
if (dis[v]<= dis[u]+ w|| vis[v]) continue ;
dis[v] = dis[u]+ w;
f.push ({dis[v],v});
}
}
for (int i= 1 ;i<= n;i++ ) cout << dis[i] << ' ' ;
cout << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#include <bitset>
#include <queue>
#include <vector>
#define int long long
using namespace std ;
const int N = 3 e 3 + 5 ;
const int INF = 1 e 9 ;
int n,m,dis[N],_dis[N],cnt[N];
vector< pair<int ,int>> to[N];
bitset< N> vis;
bool spfa (int x ){
fill (dis+ 1 ,dis+ 1 + n+ 1 ,INF);
vis.reset ();
queue<int> f;
f.push (x),vis[x]= 1 ;
dis[x] = 0 ;
cnt[x]++ ;
while (! f.empty ()){
int u = f.front ();
f.pop ();
vis[u] = 0 ;
for (auto [v,w]:to[u]){
if (dis[v]<= dis[u]+ w) continue ;
dis[v] = dis[u]+ w;
if (vis[v]) continue ;
if ((++ cnt[v])> n) return 0 ;
vis[v] = 1 ;
f.push (v);
}
}
return 1 ;
}
void dijkstra (int x ){
fill (_dis+ 1 ,_dis+ 1 + n+ 1 ,INF);
vis.reset ();
priority_queue< pair<int ,int>> f;
_dis[x] = 0 ;
f.push ({0 ,x});
while (! f.empty ()){
int u = f.top ().second;
f.pop ();
if (vis[u]) continue ;
vis[u] = 1 ;
for (auto [v,w]:to[u]){
if (_dis[v]<= _dis[u]+ w) continue ;
_dis[v] = _dis[u]+ w;
f.push ({- _dis[v],v});
}
}
}
signed main (){
cin >> n >> m;
for (int i= 1 ;i<= m;i++ ){
int u,v,w;
cin >> u >> v >> w;
to[u].push_back ({v,w});
}
for (int i= 1 ;i<= n;i++ ){
to[n+ 1 ].push_back ({i,0 });
}
if (! spfa (n+ 1 )){
cout << - 1 << endl;
return 0 ;
}
for (int i= 1 ;i<= n;i++ ){
for (auto& [v,w]:to[i]){
w += dis[i] - dis[v];
}
}
for (int i= 1 ;i<= n;i++ ){
int inarr = 0 ;
dijkstra (i);
// for(int j=1;j<=n;j++){
// cout << _dis[j]+dis[j]-dis[i] << ' ';
// }
// cout << endl;
for (int j = 1 ; j <= n; j++ ) {
if (_dis[j] == INF)
inarr += j * INF;
else
inarr += j * (_dis[j] + dis[j] - dis[i]);
}
cout << inarr << endl;
}
return 0 ;
}
C++ 复制 //P10814 【模板】离线二维数点
#include <iostream>
#include <vector>
#define lowbit (x ) (x& (- x))
using namespace std ;
const int N = 2 e 6 + 5 ;
struct Node {
int x,id,tag;
};
int n,m,a[N],f[N],arr[N];
vector< Node> k[N];
void Edit (int x ){
for (;x<= n;x+= lowbit (x)) f[x]++ ;
}
int Query (int x ){
int inarr = 0 ;
for (;x;x-= lowbit (x)) inarr += f[x];
return inarr;
}
int main (){
scanf (" %d%d " ,& n,& m);
for (int i= 1 ;i<= n;i++ ) scanf (" %d " ,& a[i]);
for (int i= 1 ;i<= m;i++ ){
int l,r,x;
scanf (" %d%d%d " ,& l,& r,& x);
k[l- 1 ].push_back ({x,i,- 1 });
k[r].push_back ({x,i,1 });
}
for (int i= 1 ;i<= n;i++ ){
Edit (a[i]);
for (auto x:k[i]) arr[x.id] += x.tag* Query (x.x);
}
for (int i= 1 ;i<= m;i++ ) printf (" %d\n " ,arr[i]);
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
using namespace std ;
const int N = 505 ;
int n,m,e,cnt,vis[N],r[N],arr;
vector<int> T[N];
bool dfs (int now ,int k ){
if (vis[now]== k) return 0 ;
vis[now] = k;
for (auto x:T[now]){
if (r[x]&&! dfs (r[x],k)) continue ;
r[x] = now;
return 1 ;
}
return 0 ;
}
int main (){
cin >> n >> m >> e;
for (int i= 1 ;i<= e;i++ ){
int u,v;
cin >> u >> v;
T[u].push_back (v);
}
for (int i= 1 ;i<= n;i++ ){
arr+= dfs (i,i);
}
cout << arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#include <queue>
using namespace std ;
const int N = 1 e 2 + 5 ;
int n,cnt;
int inl[N],arr[N];
vector<int> son[N];
queue<int> f;
int main (){
cin >> n;
for (int i= 1 ;i<= n;i++ ){
int a;
while (cin>> a){
if (! a){
break ;
}
son[i].push_back (a);
inl[a]++ ;
}
}
for (int i= 1 ;i<= n;i++ ){
if (inl[i]){
continue ;
}
f.push (i);
}
while (! f.empty ()){
int v = f.front ();
f.pop ();
cout << v << ' ' ;
for (int i= 0 ;i< son[v].size ();i++ ){
inl[son[v][i]]-- ;
if (inl[son[v][i]]){
continue ;
}
f.push (son[v][i]);
}
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <cstring>
using namespace std ;
const int N = 1 e 6 + 5 ;
int n,a[N],k;
struct Node {
int head= 1 ,tail,c[N];
void clear (){
head = 1 ,tail = 0 ;
memset (c,0 ,sizeof (c));
}
void push (int i ,int j ){
while (head<= tail&& a[c[tail]]* j< a[i]* j)
tail-- ;
c[++ tail] = i;
}
int get (int i ){
while (head<= tail&& i- c[head]>= k)
head++ ;
return a[c[head]];
}
}f;
int main (){
cin >> n >> k;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i];
}
for (int i= 1 ;i<= n;i++ ){
f.push (i,- 1 );
if (i< k) continue ;
cout << f.get (i) << ' ' ;
}
f.clear ();
cout << endl;
for (int i= 1 ;i<= n;i++ ){
f.push (i,1 );
if (i< k) continue ;
cout << f.get (i) << ' ' ;
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <vector>
#define int long long
#define ls (now <<1)
#define rs (now <<1|1)
#define len (x ) (T[x].r- T[x].l+ 1 )
using namespace std ;
const int N = 1 e 5 + 5 ;
int n,m,r,p,cnt;
int fa[N],dep[N],siz[N],son[N],top[N],dfn[N],seg[N];
int val[N];
vector<int> to[N];
struct eerT {
int l,r,sum,tag;
}T[N* 4 ];
void Build (int now ,int l ,int r ){
T[now].l = l,T[now].r = r;
if (l== r){
T[now].sum = val[seg[l]];
return ;
}
int mid = (l+ r)/ 2 ;
Build (ls,l,mid);
Build (rs,mid+ 1 ,r);
T[now].sum = T[ls].sum+ T[rs].sum;
}
void pushdown (int now ){
T[ls].sum += len (ls)* T[now].tag;
T[rs].sum += len (rs)* T[now].tag;
T[ls].tag += T[now].tag;
T[rs].tag += T[now].tag;
T[now].tag = 0 ;
}
void Edit (int now ,int l ,int r ,int k ){
if (T[now].l> r|| T[now].r< l) return ;
if (l<= T[now].l&& T[now].r<= r){
T[now].sum += len (now)* k;
T[now].tag += k;
return ;
}
if (T[now].tag) pushdown (now);
Edit (ls,l,r,k);
Edit (rs,l,r,k);
T[now].sum = T[ls].sum+ T[rs].sum;
}
int Query (int now ,int l ,int r ){
if (T[now].l> r|| T[now].r< l) return 0 ;
if (l<= T[now].l&& T[now].r<= r) return T[now].sum;
if (T[now].tag) pushdown (now);
return Query (ls,l,r)+ Query (rs,l,r);
}
void dfs1 (int now ,int from ){
fa[now] = from;
dep[now] = dep[from]+ 1 ;
siz[now] = 1 ;
for (auto x:to[now]){
if (x== from) continue ;
dfs1 (x,now);
siz[now] += siz[x];
if (siz[x]> siz[son[now]]) son[now] = x;
}
}
void dfs2 (int now ,int head ){
top[now] = head;
dfn[now] = ++ cnt;
seg[cnt] = now;
if (son[now]) dfs2 (son[now],head);
for (auto x:to[now]){
if (x== fa[now]|| x== son[now]) continue ;
dfs2 (x,x);
}
}
void opt1 (int x ,int y ,int v ){
while (top[x]!= top[y]){
if (dep[top[x]]< dep[top[y]]) swap (x,y);
Edit (1 ,dfn[top[x]],dfn[x],v);
x = fa[top[x]];
}
Edit (1 ,min (dfn[x],dfn[y]),max (dfn[x],dfn[y]),v);
}
int opt2 (int x ,int y ){
int arr = 0 ;
while (top[x]!= top[y]){
if (dep[top[x]]< dep[top[y]]) swap (x,y);
(arr += Query (1 ,dfn[top[x]],dfn[x])) %= p;
x = fa[top[x]];
}
(arr += Query (1 ,min (dfn[x],dfn[y]),max (dfn[x],dfn[y]))) %= p;
return arr;
}
signed main (){
cin >> n >> m >> r >> p;
for (int i= 1 ;i<= n;i++ ){
cin >> val[i];
val[i]%= p;
}
for (int i= 1 ;i< n;i++ ){
int u,v;
cin >> u >> v;
to[u].push_back (v);
to[v].push_back (u);
}
dfs1 (r,0 );
dfs2 (r,r);
Build (1 ,1 ,n);
while (m-- ){
// cout << "*****************\n";
int opt= 0 ,x= 0 ,y= 0 ,z= 0 ;
cin >> opt >> x;
if (opt== 1 ){
cin >> y >> z;
opt1 (x,y,z);
}
else if (opt== 2 ){
cin >> y;
cout << opt2 (x,y)% p << endl;
}
else if (opt== 3 ){
cin >> z;
Edit (1 ,dfn[x],dfn[x]+ siz[x]- 1 ,z);
}
else {
// cout << dfn[x] << ' ' << siz[x] << endl;
cout << Query (1 ,dfn[x],dfn[x]+ siz[x]- 1 )% p << endl;
}
}
return 0 ;
}
C++ 复制 #include <iostream>
#include <algorithm>
#define int long long
#define ls (now *2)
#define rs (now *2+1)
using namespace std ;
const int N = 1 e 6 + 5 ;
struct Node {
int l,r,h,tag;
}l[N* 2 ];
struct eerT {
int l,r,len,sum;
}T[N* 4 ];
int n,x[N* 2 ],tot,arr;
void Build (int now ,int l ,int r ){
T[now].l = l,T[now].r = r;
if (l== r) return ;
int mid = (l+ r)/ 2 ;
Build (ls,l,mid);
Build (rs,mid+ 1 ,r);
}
void pushup (int now ){
if (T[now].sum) T[now].len = x[T[now].r+ 1 ]- x[T[now].l];
else T[now].len = T[ls].len + T[rs].len;
}
void Edit (int now ,int l ,int r ,int k ){
if (x[T[now].r+ 1 ]<= l|| x[T[now].l]>= r) return ;
if (l<= x[T[now].l]&& x[T[now].r+ 1 ]<= r){
T[now].sum += k;
pushup (now);
return ;
}
Edit (ls,l,r,k);
Edit (rs,l,r,k);
pushup (now);
}
signed main (){
cin >> n;
for (int i= 1 ;i<= n;i++ ){
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
x[i* 2 - 1 ] = x1,x[i* 2 ] = x2;
l[i* 2 - 1 ] = {x1,x2,y1,1 };
l[i* 2 ] = {x1,x2,y2,- 1 };
}
n*= 2 ;
sort (x+ 1 ,x+ 1 + n);
sort (l+ 1 ,l+ 1 + n,[](const Node & T1 ,const Node & T2 ){return T1.h < T2.h;});
tot = unique (x+ 1 ,x+ 1 + n)- x- 1 ;
Build (1 ,1 ,tot- 1 );
for (int i= 1 ;i< n;i++ ){
Edit (1 ,l[i].l,l[i].r,l[i].tag);
arr += T[1 ].len* (l[i+ 1 ].h- l[i].h);
}
cout << arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#define int __int128
using namespace std ;
const int N = 15 ;
signed n,a[N],b[N];
int lcm= 1 ,arr;
int x,y;
int exgcd (int a ,int b ,int & x ,int & y ){
if (! b){
x = 1 ,y = 0 ;
return a;
}
int d = exgcd (b,a% b,y,x);
y -= a/ b* x;
return d;
}
signed main (){
cin >> n;
for (int i= 1 ;i<= n;i++ ){
cin >> a[i] >> b[i];
lcm *= a[i];
}
for (int i= 1 ;i<= n;i++ ){
int k = lcm/ a[i];
exgcd (k,a[i],x,y);
arr += (b[i]* k* x)% lcm;
arr = (arr% lcm+ lcm)% lcm;
}
cout << (long long )arr << endl;
return 0 ;
}
C++ 复制 #include <iostream>
#define int __int128
using namespace std ;
const int N = 1 e 5 + 5 ;
long long n,a,b;
int A= 1 ,B;
int x,y;
int exgcd (int a ,int b ,int & x ,int & y ){
if (! b){
x= 1 ,y= 0 ;
return a;
}
int d = exgcd (b,a% b,y,x);
y -= a/ b* x;
return d;
}
signed main (){
cin >> n;
for (int i= 1 ;i<= n;i++ ){
cin >> a >> b;
int d = exgcd (A,a,x,y);
x = (B- b)/ d* x;
B = B- A* x;
A = a/ d* A; //lcm(A,a)
B = (B% A+ A)% A;
}
cout << (long long )((B% A+ A)% A) << endl;
return 0 ;
}