#include
using namespace std;
#define ll long long
const int N = 2e5 + 10;
ll a[N];
int main()
{
int t;
cin>>t;
while (t--)
{
int n,k,x;
cin>>n>>k;
fill(a,a+n+1,0);
for (int i=0; i>x;
a[i%k]+=x;//把数分为k个集合,每次操作1就等效对每个集合操作
}
if (k==1)
{
cout<<0<
显然用bfs去跑即可 ,为了使一个点不要重复经过,我们可以记录每个点在方向f时的最短路是多少,只更新更短的
#include
using namespace std;
#define ll long long
#define int ll
typedef pair pii;
const int N = 110;
struct node
{
int d,x,y,p,f;
bool operator<(const node&k)const
{
return d>k.d;
}
};
int dp[N][N][60][5];
bool vis[N][N][60][5];
int ways[5][3]= {{-1,0},{1,0},{0,-1},{0,1}};
pii mp[N][N];
char ch[N][N];
int32_t main()
{
int n,m,p,k;
cin>>n>>m>>k>>p;
int x1,y1,x2,y2;
for (int i=1; i<=k; ++i)
{
cin>>x1>>y1>>x2>>y2;
mp[x1][y1]= {x2,y2};
mp[x2][y2]= {x1,y1};
}
memset(dp,0x3f,sizeof(dp));
for (int i=1; i<=n; ++i)for (int j=1; j<=m; ++j)
{
cin>>ch[i][j];
if (ch[i][j]=='U')ch[i][j]='0';
else if (ch[i][j]=='D')ch[i][j]='1';
else if (ch[i][j]=='L')ch[i][j]='2';
else if (ch[i][j]=='R')ch[i][j]='3';
}
priority_queueq;
dp[1][1][0][3]=0;
q.push({0,1,1,0,3});
while (!q.empty())
{
node u=q.top();
q.pop();
if (vis[u.x][u.y][u.p][u.f])continue;
vis[u.x][u.y][u.p][u.f]=1;
if (u.x==n&&u.y==m)
{
cout<<"YES"<dp[u.x][u.y][u.p][u.f]+1)
{
dp[v.x][v.y][v.p][v.f]=v.d=dp[u.x][u.y][u.p][u.f]+1;
q.push(v);
}
}
}
}
else if (('0'<=ch[u.x][u.y]&&ch[u.x][u.y]<='3')||ch[u.x][u.y]=='@')
{
node v=u;
if (ch[u.x][u.y]!='@')v.f=ch[u.x][u.y]-'0';
v.x+=ways[v.f][0],v.y+=ways[v.f][1];
if (1<=v.x&&v.x<=n&&1<=v.y&&v.y<=m&&ch[v.x][v.y]!='#')
{
if (mp[v.x][v.y].first!=0)
{
pii tmp=mp[v.x][v.y];
v.x=tmp.first,v.y=tmp.second;
}
if (v.p<=p&&dp[v.x][v.y][v.p][v.f]>dp[u.x][u.y][u.p][u.f]+1)
{
dp[v.x][v.y][v.p][v.f]=v.d=dp[u.x][u.y][u.p][u.f]+1;
q.push(v);
}
}
}
}
cout<<"NO"<
裸拓扑,反向建边即可
#include
using namespace std;
#define ll long long
#define int ll
const int N = 2e5 + 10;
const int mod=1e9+7;
ll a[N],in[N];
struct node
{
int next,to,w;
} edge[N<<1];
int head[N],num;
void add(int u,int v,ll w)
{
edge[++num].next=head[u];
edge[num].to=v,edge[num].w=w;
head[u]=num;
}
int32_t main()
{
int n,m,k;
cin>>n>>m>>k;
int x,y,c;
for (int i=1; i<=k; ++i)
{
cin>>x>>y;
a[x]+=y;
}
for (int i=1; i<=m; ++i)cin>>x>>y>>c,add(y,x,c),in[x]++;
queueq;
ll ans=0;
for (int i=1; i<=n; ++i)if (!in[i])q.push(i);
while (!q.empty())
{
int u=q.front();
q.pop();
ans=(ans+a[u])%mod;
for (int i=head[u]; i; i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
a[v]=(a[v]+a[u]*w)%mod;
if (--in[v]==0)q.push(v);
}
}
cout<
#include
using namespace std;
#define ll long long
#define endl "\n"
const int N = 5e3 + 10;
const int mod=1e9+7;
ll c[N][N];
void init()
{
c[0][0]=1;
for (int i=1; i<=5e3; ++i)
{
c[i][0]=1;
for (int j=1; j<=i; ++j)c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
int main()
{
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t,n,x;
cin>>t;
init();
while (t--)
{
cin>>n>>x;
if (n+1>x)
{
cout<<-1<
#include
using namespace std;
#define ll long long
#define endl "\n"
typedef pair pii;
const int N = 2e5 + 10;
int t[N],a[N],b[N];
unordered_mapmp;
void add(int x,int w)
{
for (int i=x; i; i-=i&-i)t[i]=max(t[i],w);
}
int ask(int x)
{
int ans=0;
for (int i=x; i<=N; i+=i&-i)ans=max(ans,t[i]);
return ans;
}
int main()
{
int n,m;
cin>>n;
for (int i=1; i<=n; ++i)cin>>a[i];
cin>>m;
for (int i=1; i<=m; ++i)
{
cin>>b[i];
if (!mp[b[i]].first)mp[b[i]]= {i,i};//更新每个在b中出现的数的出现区间
else mp[b[i]].second=i;
}
int ans=0,sum=0;
for (int i=1; i<=n; ++i)
{
if (!mp[a[i]].first)sum++;//b中没出现直接加
else
{
int tmp=ask(mp[a[i]].second)+1;//找符合条件的最长子串,寻找在b数组顺序在a[i]之后出现的数(这些数在a数组中是在a之前出现,先出现才能被加到t数组里面)
ans=max(ans,tmp);
add(mp[a[i]].first,tmp);//更新t数组中出现的数字的区间
}
}
cout<
注意到[l,r]胜利与否结果就是r是1还是0,所以 我们在区间操作中维护每个数被修改次数即可,奇数次转变奇偶性,偶数次等价没修改。所以其实是树状数组裸题
#include
using namespace std;
#define ll long long
#define endl "\n"
const int N = 1e6 + 10;
int t[N];
int n,q,x,op,l,r;
void add(int x,int w)
{
for (int i=x; i<=n; i+=i&-i)t[i]+=w;
}
int ask(int x)
{
int ans=0;
for (int i=x; i; i-=i&-i)ans+=t[i];
return ans;
}
int main()
{
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>q>>x>>x>>x>>x;
char c;
for (int i=1; i<=n; ++i)
{
cin>>c;
if (c=='1')add(i,1),add(i+1,-1);
}
while (q--)
{
cin>>op>>l>>r;
if (op==1)
{
add(l,1),add(r+1,-1);//区间修改
}
else
{
if (ask(r)&1)//单点查询
{
cout<<"Magical Splash Flare"<