题解在下已经写过一次了,所以就不再写了,下面只有代码
题解下载(1):http://pan.baidu.com/s/1hsAUjMs
题解下载(2):http://pan.baidu.com/s/1mhC7EYk
A 卿学姐与公主
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXN 100000
#define ls (o<<1)
#define rs (o<<1|1)
#define mid ((tr[o].l+tr[o].r)>>1)
using namespace std;
struct qq {
int l, r;
long long val;
}tr[MAXN<<2];
int l,r;
void build(int o,int l,int r) {
tr[o].l = l;
tr[o].r = r;
if (tr[o].l == tr[o].r)
return;
build(ls,l,mid);
build(rs,mid+1,r);
}
void change(int o) {
if (tr[o].l == tr[o].r) {
tr[o].val += r;
return;
}
if (l <= mid) {
change(ls);
} else {
change(rs);
}
tr[o].val = max(tr[ls].val, tr[rs].val);
}
long long Q(int o) {
if (l <= tr[o].l && tr[o].r <= r) {
return tr[o].val;
}
long long ans = 0;
if (l <= mid)ans = max(ans, Q(ls));
if (r > mid)ans = max(ans, Q(rs));
return ans;
}
int main() {
int n,q;
scanf("%d%d",&n,&q);
build(1,1,n);
while(q--) {
int type;
scanf("%d%d%d", &type, &l, &r);
if (type == 1) {
change(1);
} else {
printf("%lld\n", Q(1));
}
}
return 0;
}
B 卿学姐与基本法
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5+7;
vector<int> v;
struct node
{
int x,y,z;
};
map<int,int>H1,H2;
typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy;
void update(int x)
{
sum=0;
lazy=1;
}
};
treenode tree[maxn*4];
inline void push_down(int o)
{
SgTreeDataType lazyval = tree[o].lazy;
if(lazyval==0)return;
tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);
tree[o].lazy = 0;
}
inline void push_up(int o)
{
tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum;
}
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = 0,tree[o].lazy = 0;
if( L == R)
{
tree[o].sum = H2[L+1]-H2[L];
return;
}
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
push_up(o);
}
}
inline void update(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
}
inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = 0;
if (QL <= mid) res += query(QL,QR,2*o);
if (QR > mid) res += query(QL,QR,2*o+1);
push_up(o);
return res;
}
}
node tmp[maxn];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d%d",&tmp[i].x,&tmp[i].y,&tmp[i].z);
v.push_back(tmp[i].y),v.push_back(tmp[i].z);
if(tmp[i].y!=1)v.push_back(tmp[i].y-1);
v.push_back(tmp[i].z+1);
}
v.push_back(n+1);
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(int i=0;i<v.size();i++)
H1[v[i]]=i+1,H2[i+1]=v[i];
build_tree(1,v.size(),1);
for(int i=1;i<=q;i++)
{
if(tmp[i].x==1)
update(H1[tmp[i].y],H1[tmp[i].z],1,1);
else
printf("%d\n",query(H1[tmp[i].y],H1[tmp[i].z],1));
}
}
C 卿学姐与诡异村庄
代码
#include <cstdio>
#define MAX_N 100005
int father[MAX_N * 2], N;
void init(){
for(int i = 1; i <= 2 * N; i++)
father[i] = i;
}
int Find(int x){
if(x == father[x]) return x;
return father[x] = Find(father[x]);
}
void unionSet(int x, int y){
int u = Find(x), v = Find(y);
if(u == v) return;
father[u] = v;
}
bool Same(int x, int y){
return Find(x) == Find(y);
}
int Good(int x){
return x;
}
int Bad(int x){
return x + N;
}
int main(){
scanf("%d", &N);
init();
for(int i = 1; i <= N; i++){
int a, t;
scanf("%d%d", &a, &t);
if(t == 1){
unionSet(Good(a), Good(i));
unionSet(Bad(a), Bad(i));
}
else{
unionSet(Good(a), Bad(i));
unionSet(Bad(a), Good(i));
}
}
for(int i = 1; i <= N; i++){
if(Same(Good(i), Bad(i))){
printf("One face meng bi\n");
return 0;
}
}
printf("Time to show my power\n");
return 0;
}
D 卿学姐与魔法
代码
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#define MAXN 100010
int Q[MAXN],W[MAXN],A[MAXN],B[MAXN];
using namespace std;
struct qq{
int a,b;
bool operator <(const qq &X)const{
return Q[a]+W[b]>Q[X.a]+W[X.b];
}
};
priority_queue<qq> q;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &Q[i]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &W[i]);
}
sort(Q, Q + n);
sort(W, W + n);
for (int i = 0; i < n; ++i) {
q.push((qq) {i, 0});
}
for (int i = 0; i < n; ++i) {
qq tem = q.top();
q.pop();
printf("%d\n", Q[tem.a] + W[tem.b]);
tem.b++;
if (tem.b == n)continue;
q.push(tem);
}
return 0;
}
E 卿学姐与城堡的墙
代码
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#define MAX 200100
using namespace std;
typedef long long LL;
LL n;
struct Point{
LL x,y;
bool operator <(const Point &X)const{
if(x!=X.x)return x<X.x;
return y>X.y;
}
};
LL tr[MAX<<1];
LL Q(LL o){
LL res=0;
while(o){
res+=tr[o];
o-=o&(-o);
}
return res;
}
vector<Point>a;
vector<LL>tot;
void change(LL o){
while(o<=tot.size()){
tr[o]++;
o+=o&(-o);
}
}
int main(int argc, const char * argv[]) {
LL u,v;
scanf("%lld%lld%lld",&n,&u,&v);
for(int i=0;i<n;++i){
LL k,b;
scanf("%lld%lld",&k,&b);
a.push_back((Point){k*u+b,k*v+b});
tot.push_back(a[i].x);
tot.push_back(a[i].y);
}
sort(tot.begin(),tot.end());
tot.resize(distance(tot.begin(),unique(tot.begin(),tot.end())));
for(Point &i:a){
i.y=(LL)(lower_bound(tot.begin(),tot.end(),i.y)-tot.begin())+1;
i.x=(LL)(lower_bound(tot.begin(),tot.end(),i.x)-tot.begin())+1;
}
sort(a.begin(),a.end());
LL ans=0;
for(Point &i:a){
ans+=Q((int)tot.size())-Q(i.y-1);
change(i.y);
}
cout<<ans<<endl;
return 0;
}
F 郭大侠与“有何贵干?”
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 15;
vector < int > HaY ;
int C , K , N;
struct Line{
int x , y1 , y2 , flag ;
friend bool operator < (const Line & a , const Line & b){
return a.x < b.x;
}
Line( int x = 0 , int y1 = 0 , int y2 = 0 , int flag = 0 ) : x(x) , y1(y1) , y2(y2) , flag(flag){}
};
int getrank(int x){ return lower_bound( HaY.begin() , HaY.begin() + C , x ) - HaY.begin();}
struct Sgtree{
struct node{
int l , r ;
int cover;
int len[12];
}tree[maxn * 8];
void maintain( int o ){
int l = tree[o].l , r = tree[o].r;
if( r - l == 1 ){
memset( tree[o].len , 0 , sizeof( tree[o].len ) );
tree[o].len[min( K + 1 , tree[o].cover )] = HaY[r] - HaY[l];
}else{
memset( tree[o].len , 0 , sizeof( tree[o].len ) );
int extra = tree[o].cover;
for(int i = 0 ; i <= K + 1 ; ++ i) tree[o].len[ min( i + extra , K + 1 ) ] = tree[o << 1].len[i] + tree[o << 1 | 1].len[i];
}
}
void build(int l , int r , int o){
tree[o].l = l , tree[o].r = r , tree[o].cover = 0;
for(int i = 1 ; i < 12 ; ++ i) tree[o].len[i] = 0;
tree[o].len[0] = HaY[r] - HaY[l];
if( r - l > 1 ){
int mid = l + r >> 1;
build( l , mid , o << 1 );
build( mid , r , o << 1 | 1 );
}
}
void update(int ql , int qr , int v , int o){
if( ql == qr ) return ;
int l = tree[o].l , r = tree[o].r;
if( l >= ql && r <= qr ) tree[o].cover += v ;
else{
int mid = l + r >> 1;
if( ql < mid ) update( ql , qr , v , o << 1 );
if( qr > mid ) update( ql , qr , v , o << 1 | 1 );
}
maintain( o );
}
}Sgtree;
vector < Line > Tl[2];
long long solve( const vector < Line > & ts ){
long long res = 0;
if( ts.size() == 0 ) return 0LL;
Sgtree.build( 0 , C - 1 , 1 );
int pre = ts[0].x;
for(int i = 0 ; i < ts.size() ; ++ i){
int len = ts[i].x - pre;
res += 1LL * len * Sgtree.tree[1].len[K];
Sgtree.update( getrank( ts[i].y1 ) , getrank( ts[i].y2 ) , ts[i].flag , 1 );
pre = ts[i].x;
}
return res;
}
int main(int argc,char *argv[]){
scanf("%d%d",&N,&K);
for(int i = 1 ; i <= N ; ++ i){
int x1 , y1 , z1 , x2 , y2 , z2 ;
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
if( z2 - z1 >= 1 ){
HaY.push_back( y1 );
HaY.push_back( y2 );
for(int j = z1 ; j < z2 ; ++ j){
Tl[j - 1].push_back( Line( x1 , y1 , y2 , 1 ));
Tl[j - 1].push_back( Line( x2 , y1 , y2 , -1 ));
}
}
}
for(int i = 0 ; i < 2 ; ++ i) sort( Tl[i].begin() , Tl[i].end() );
sort(HaY.begin(),HaY.end());
C = unique( HaY.begin() , HaY.end() ) - HaY.begin();
long long ans = 0;
for(int i = 0 ; i < 2 ; ++ i) ans += solve( Tl[i] );
printf("%lld\n" , ans );
return 0;
}
G - 郭大侠与阴阳家
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <bitset>
using namespace std;
#define MAXN 2010
#define mo 1000000007
typedef long long LL;
int n,m;
struct node
{
int x,y; LL a,b;
bool operator<(const node &z) const{
if(x!=z.x) return x<z.x;
if(y!=z.y) return y<z.y;
if(a!=z.a) return a<z.a;
return b<z.b;
}
}vec[MAXN*MAXN],p[MAXN];
LL GCD(LL a,LL b)
{
for(LL t;b;t=a%b,a=b,b=t);
return a;
}
int main()
{
int i,j,l,r; LL t,ans=0;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+n);
for(i=1,j=2;j<=n;++j){
if(p[i].x==p[j].x&&p[i].y==p[j].y) continue;
p[++i]=p[j];
}
n=i;
for(i=1;i<=n;++i)
for(j=1;j<=n;++j){
if(i==j) continue;
++m;
vec[m].x=p[i].x-p[j].x;
vec[m].y=p[i].y-p[j].y;
if(vec[m].x==0) vec[m].a=mo,vec[m].b=p[i].x;
else if(1LL*vec[m].x*p[i].y-1LL*vec[m].y*p[i].x==0) vec[m].a=vec[m].b=0;
else{
t=GCD(vec[m].x,1LL*vec[m].x*p[i].y-1LL*vec[m].y*p[i].x);
vec[m].a=(1LL*vec[m].x*p[i].y-1LL*vec[m].y*p[i].x)/t;
vec[m].b=vec[m].x/t;
}
}
sort(vec+1,vec+1+m);
for(i=1,j=2;j<=m+1;++j)
if(j>m||vec[j].x!=vec[i].x||vec[j].y!=vec[i].y){
for(l=i,r=l+1;r<=j;++r)
if(r==j||vec[r].a!=vec[l].a||vec[r].b!=vec[l].b)
ans+=1LL*(r-l)*(l-i),l=r;
i=j;
}
printf("%lld\n",ans/4);
return 0;
}
H - 郭大侠与英雄学院
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
pair<int,pair<int,int> >A[maxn];
map<int,int>X;
map<int,int>Y;
int Hx[maxn],Hy[maxn];
int ans[maxn];
int fa[maxn];
int fi(int u){
return u != fa[u] ? fa[u] = fi( fa[u] ) : u;
}
void uni(int u ,int v){
int p1 = fi( u ) , p2 = fi( v );
if( p1 != p2 ) fa[p1] = p2;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n*m;i++)
{
fa[i]=i;
scanf("%d",&A[i].first);
A[i].second.first=i/m;
A[i].second.second=i%m;
}
int j=-1;
sort(A,A+n*m);
for(int i=0;i<n*m;i++)
{
if(i!=n*m-1&&A[i].first==A[i+1].first)
continue;
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
Hx[x]=p;
Hy[y]=p;
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
uni(Hx[x],p);
uni(Hy[y],p);
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
int pa = fi(p);
ans[pa]=max(ans[pa],max(X[x],Y[y])+1);
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
X[x]=max(X[x],ans[fi(p)]);
Y[y]=max(Y[y],ans[fi(p)]);
}
j=i;
}
for(int i=0;i<n*m;i++)
{
printf("%d ",ans[fi(i)]);
if(i%m==m-1)printf("\n");
}
}
I - 郭大侠与线上游戏
代码
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
const int maxn = 1e6 + 15;
int N , bit[maxn] , vi[maxn] , C , TC , op[maxn] , v[maxn];
void update( int x , int y ){
while( x <= C ){
bit[x] += y ;
x += lowbit( x );
}
}
int prefix( int x ){
int res = 0;
while( x ){
res += bit[x];
x -= lowbit( x );
}
return res;
}
queue < int > Q;
int getrk( int x ){ return lower_bound( vi , vi + TC , x ) - vi + 1; }
int main(int argc,char *argv[]){
scanf("%d",&N);
for(int i = 1 ; i <= N ; ++ i){
scanf("%d",op + i);
if( op[i] == 1 ){
scanf("%d", v + i);
vi[C++]=v[i];
}
}
sort(vi , vi + C);
TC = unique( vi , vi + C ) - vi ;
for(int i = 1 ; i <= N ; ++ i){
if( op[i] == 1 ){
int add = v[i];
add = getrk( add );
update( add , 1 );
Q.push( add );
}else if(op[i] == 2){
int x = Q.front() ; Q.pop();
update( x , -1 );
}else{
int l = 1 , r = C;
int f = Q.size() / 2 + 1;
while( l < r ){
int mid = l + r >> 1;
if( prefix( mid ) < f ) l = mid + 1;
else r = mid ;
}
printf("%d\n" , vi[ l - 1 ] );
}
}
return 0;
}
J - 郭大侠与Rabi-Ribi
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 15;
int N ;
pair < int , int > Rabi[maxn];
priority_queue< int , vector < int > , greater < int > >Q;
int main(int argc,char *argv[]){
scanf("%d",&N);
for(int i = 1 ; i <= N ; ++ i) scanf("%d" , &Rabi[i].first);
for(int i = 1 ; i <= N ; ++ i) scanf("%d" , &Rabi[i].second);
sort( Rabi + 1 , Rabi + N + 1 );
int ans = 0 , j = 1;
for(int i = 1 ; i <= N ; ++ i){
if(Q.size() < Rabi[i].first) Q.push(Rabi[i].second);
else if(Q.top() < Rabi[i].second){
Q.pop();
Q.push( Rabi[i].second );
}
}
while(!Q.empty()){
ans += Q.top();
Q.pop();
}
printf("%d\n" , ans );
return 0;
}
K - 郭大侠与甲铁城
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
int N , Q , cnt[maxn] , unit , a[maxn] , out[maxn];
struct query{
int l , r , idx;
friend bool operator < (const query & a , const query & b){
int idx1 = a.l / unit , idx2 = b.l / unit;
if( idx1 != idx2 ) return idx1 < idx2;
return a.r < b.r;
}
}op[maxn];
int main(int argc,char *argv[]){
scanf("%d%d",&N,&Q);
unit = sqrt( N );
for(int i = 1 ; i <= N ; ++ i) scanf("%d" , a + i);
for(int i = 1 ; i <= Q ; ++ i){
scanf("%d%d",&op[i].l,&op[i].r);
op[i].idx=i;
}
sort( op + 1 , op + Q + 1 );
int l = 0 , r = -1 , ans = 0;
for(int i = 1 ; i <= Q ; ++ i){
while( r < op[i].r ){
++ r;
if( cnt[a[r]] == 0 ) ans ++ ;
cnt[a[r]] ++ ;
}
while( r > op[i].r ){
if( cnt[a[r]] == 1 ) ans -- ;
cnt[a[r]] -- ;
-- r;
}
while( l < op[i].l ){
if( cnt[a[l]] == 1 ) ans -- ;
cnt[a[l]] --;
l ++ ;
}
while( l > op[i].l ){
-- l;
if( cnt[a[l]] == 0 ) ans ++ ;
cnt[a[l]] ++ ;
}
out[op[i].idx] = ans;
}
for(int i = 1 ; i <= Q ; ++ i) printf("%d\n" , out[i] );
return 0;
}
L - 郭大侠与苦恼
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 500;
struct edge{
int v , nxt;
}e[maxn << 1];
int n , head[maxn] , tot , fa[maxn];
long long ans;
map < int , int > S[maxn] , *ptr[maxn];
void link(int u ,int v){
e[tot].v=v,e[tot].nxt=head[u],head[u]=tot++;
}
void union_map(map < int , int > * & a , map < int , int > * & b){
if(a->size()>b->size()) swap( a , b );
map < int , int > :: iterator it = a->begin();
while(a->size()){
it=a->begin();
(*b)[it->first] += it->second;
a->erase(it);
}
}
void dfs(int u , int val){
(*ptr[u])[val]++;
for(int i = head[u] ; ~i ; i = e[i].nxt){
int v = e[i].v;
if( v == fa[u] ) continue;
fa[v] = u ;
dfs( v , val ^ v );
map < int , int > :: iterator it = ptr[v]->begin();
while( it != ptr[v]->end() ){
int S = ((it->first)^u);
if(ptr[u]->count(S)) ans += 1LL * (*ptr[u])[S] * it->second;
it++;
}
union_map( ptr[v] , ptr[u] );
}
}
int main(int argc,char *argv[]){
memset( head , -1 ,sizeof( head ) );
scanf("%d",&n);
for(int i = 1 ; i < n ; ++ i){
int u , v ;
scanf("%d%d",&u,&v);
link( u , v );
link( v , u );
}
for(int i = 1 ; i <= n ; ++ i) ptr[i] = S + i;
dfs( 1 , 1 );
printf("%lld\n" , ans);
return 0;
}
M - 卿学姐失恋了Ⅱ
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define FOR(i, l, r) for(int i = l; i <= r; i++)
#define ROF(i, r, l) for(int i = r; i >= l; i--)
#define all(a) a.begin(), a.end()
int M, a[25], two[25];
int work(int t, int w){
if(t <= 0) return 0;
while(a[t] == w && t) t--;
if(!t) return 0;
ll res = work(t - 1, 6 - w - a[t]);
return res + two[t - 1];
}
int main(){
cin >> M;
for(int i = 20; i >= 1; i--) scanf("%d", a + i);
two[0] = 1; for(int i = 1; i < 20; i++) two[i] = two[i - 1] * 2;
if(M >= work(20, 1)) printf("YES\n");
else if(M >= work(20, 2)) printf("YES\n");
else if(M >= work(20, 3)) printf("YES\n");
else printf("NO\n");
return 0;
}
N - 秋实大哥搞算数
代码
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 50;
ll s[maxn];
char temp[maxn];
int main(int argc,char *argv[])
{
int Case,top;
scanf("%d%*c",&Case);
while(Case--)
{
top = 0;
char ch;
ll read = 0;
int ope = -1;
ll sign = 1;
scanf("%s",temp);
int len = strlen(temp);
int pos = 0;
while(pos < len)
{
ch = temp[pos++];
if (ch == '#' || ch == ' ')
break;
//printf("%c\n",ch);
if (ch <= '9' && ch >= '0')
{
read *= 10;
read += (ch-'0');
}
else
{
s[top++] = read*sign;
sign = 1;
// cout << read << endl;
read = 0;
if (ope == 2)
{
s[top-2] = s[top-1]*s[top-2];
top--;
}
else if(ope == 3)
{
s[top-2] = s[top-2] / s[top-1];
top--;
}
ope = -1;
if(ch == '-')
sign = -1;
else if(ch == '*')
ope = 2;
else if(ch == '/')
ope = 3;
}
}
s[top++] = sign*read;
if (ope == 2)
{
s[top-2] = s[top-1]*s[top-2];
top--;
}
else if(ope == 3)
{
s[top-2] = s[top-2] / s[top-1];
top--;
}
ll ans = 0;
for(int i = 0 ; i < top ; ++ i)
ans += s[i];
printf("%lld\n",ans);
}
return 0;
}
O - 卿学姐种美丽的花
代码
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-(x)))
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long int ll;
const int maxn = 1e6+10 ,mod = 772002+233;
ll bit[maxn],cbit[maxn];
void upd(ll b[],int x,ll v){
while(x < maxn){
b[x] += v;
x += lowbit(x);
}
}
void upd(ll b[],int l,int r,ll v){
upd(b,l,v);
upd(b,r+1,-v);
}
ll query(ll b[],int x){
ll ans = 0;
while(x > 0){
ans += b[x];
x -= lowbit(x);
}
return ans;
}
ll a[maxn]; int N,Q;
int main(){
scanf("%d%d",&N,&Q);
rep(i,1,N)scanf("%lld",&a[i]);
rep(i,1,Q){
int c,x,y;
scanf("%d%d",&c,&x);
if(c == 1){
scanf("%d",&y);
upd(bit,x,min(x+y,N),(ll)y+x);
upd(cbit,x,min(x+y,N),1);
}else{
ll ans = query(bit,x) + a[x];
ans -= 1LL * x * query(cbit,x);
printf("%d\n",ans % mod);
}
}
return 0;
}
P - 浑身难受
代码
#include <bits/stdc++.h>
#define y1 fjne9w8err
#define pf(x) ( (x) * (x) )
using namespace std;
typedef long long LL;
const int N = 2000 + 10;
int a[5],f[N];
int n;
int tr[2][N];
LL ans = 0,cnt1,cnt2;
void updata(int h0,int k,int num)
{
while(k <= n)
{
tr[h0][k] += num ;
k += k&-k;
}
}
int query(int h0 , int k)///1~kµÄÇø¼äºÍ
{
int sum = 0;
while(k)
{
sum += tr[h0][k];
k -= k&-k;
}
return sum;
}
bool cmp(int a,int b){return a > b;}
void myreverse()
{
swap(a[1],a[4]),swap(a[2],a[3]);
for(int i = 1;i * 2 <= n;i++)
swap(f[i] , f[n - i + 1]);
}
void init()
{
scanf("%d",&n);
for(int i = 1;i <= 4;i++)
scanf("%d",&a[i]);
for(int i = 1;i <= n;i++)
scanf("%d",&f[i]);
}
bool check(int ai,int aj)
{
if(ai > aj) swap(ai,aj);
if(ai == 2 && aj == 3) return true;
if(ai + aj == 6 || ai + aj == 4) return true;
return false;
}
int getcnt(int ai,int h0,int fi,int fj)
{
if(ai == 1) return query(h0,min(fi,fj) - 1);
if(ai == 2 || ai == 3) return query(h0,max(fi,fj) - 1) - query(h0,min(fi,fj));
if(ai == 4) return query(h0,n) - query(h0,max(fi,fj));
}
void solve1()
{
for(int i = 1;i < n - 2;i++)
{
memset(tr,0,sizeof(tr));
for(int j = i + 2;j <= n;j++) updata(1,f[j],1);
for(int j = i + 2;j < n;j++)
{
updata(0,f[j - 1],1);
updata(1,f[j],-1);
if(cmp(f[i] , f[j]) == cmp(a[1] , a[3]))
{
cnt1 = getcnt(a[2],0,f[i],f[j]);
cnt2 = getcnt(a[4],1,f[i],f[j]);
ans += cnt1 * cnt2;
}
}
}
}
void solve2()
{
for(int i = 2;i < n - 1;i++)
{
for(int j = 1;j <= n;j++) tr[1][j] = 0;
updata(0,f[i - 1],1);
for(int j = n - 1;j > i;j--)
{
updata(1,f[j + 1],1);
if(cmp(f[i] , f[j]) == cmp(a[2] , a[3]))
{
cnt1 = getcnt(a[1],0,f[i],f[j]);
cnt2 = getcnt(a[4],1,f[i],f[j]);
ans += cnt1 * cnt2;
}
}
}
}
void solve3()
{
for(int i = 1;i < n - 2;i++)
{
memset(tr,0,sizeof(tr));
for(int j = i + 2;j <= n;j++) updata(1,f[j],1);
for(int j = i + 2;j < n;j++)
{
updata(0,f[j - 1],1);
updata(1,f[j],-1);
if(f[i] > f[j])
{
cnt1 = getcnt(4,0,f[i],f[j]);
cnt2 = getcnt(4,1,f[i],f[j]);
ans += cnt1 * cnt2;
}
}
}
}
void work()
{
if( check(a[1],a[3]) ) solve1();
else if( check(a[2],a[3]) ) solve2();
else if( check(a[2],a[4]) ) myreverse(),solve1();
else
{
if(a[1] == 3) myreverse();
swap(a[2],a[4]); solve2(); ans *= -1LL;
solve3();
}
printf("%lld\n",ans);
}
int main()
{
init();
work();
return 0;
}
Q - 昊昊爱运动 II
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 15;
// _builtin_popcount()
struct Sgtree{
struct node{
int l , r ;
unsigned long long A , B;
int lazy;
void update(int x){
lazy = x;
A = B = 0;
if( x <= 61 ) A |= (1LL << x);
else B |= (1LL << (x - 62));
}
}tree[maxn * 4];
void push_up( int o ){
int lson = o << 1 , rson = o << 1 | 1;
tree[o].A = tree[lson].A | tree[rson].A;
tree[o].B = tree[lson].B | tree[rson].B;
}
void push_down( int o ){
if( ~tree[o].lazy ){
tree[o << 1].update( tree[o].lazy );
tree[o << 1 | 1].update( tree[o].lazy );
tree[o].lazy = -1;
}
}
void build(int l , int r , int o){
tree[o].l = l , tree[o].r = r , tree[o].A = tree[o].B = 0;
tree[o].lazy = -1;
if( r > l ){
int mid = l + r >> 1;
build( l , mid , o << 1 );
build( mid + 1 , r , o << 1 | 1 );
}
}
void update(int ql , int qr , int v , int o){
int l = tree[o].l , r = tree[o].r;
if( l >= ql && r <= qr ) tree[o].update( v );
else{
int mid = l + r >> 1;
push_down( o );
if( ql <= mid ) update( ql , qr , v , o << 1 );
if( qr > mid ) update( ql , qr , v , o << 1 | 1 );
push_up( o );
}
}
void Process ( pair < unsigned long long , unsigned long long > & x , pair < unsigned long long , unsigned long long > y ){
x.first |= y.first;
x.second |= y.second;
}
pair < unsigned long long , unsigned long long > query(int ql , int qr , int o){
int l = tree[o].l , r = tree[o].r;
if( l >= ql && r <= qr ) return make_pair( tree[o].A , tree[o].B );
else{
int mid = l + r >> 1;
pair < unsigned long long , unsigned long long > res ;
res.first = res.second = 0;
push_down( o );
if( ql <= mid ) Process( res , query( ql , qr , o << 1 ) );
if( qr > mid ) Process( res , query( ql , qr , o << 1 | 1 ) );
push_up( o );
return res;
}
}
}Sgtree;
int N , M , Q;
char buffer[20];
int main(int argc,char *argv[]){
scanf("%d%d",&N,&M);
Sgtree.build( 1 , N , 1 );
for(int i = 1 ; i <= N ; ++ i){
int x ;
scanf("%d",&x);
Sgtree.update( i , i , x - 1 , 1 );
}
scanf("%d" , &Q);
while( Q -- ){
int x , y , z;
scanf("%s%d%d",buffer,&x,&y);
if(buffer[0]=='Q'){
pair < unsigned long long , unsigned long long > ans = Sgtree.query( x , y , 1 );
printf("%d\n" , __builtin_popcount(ans.first) + __builtin_popcount(ans.first >> 32) + __builtin_popcount(ans.second) + __builtin_popcount(ans.second >> 32) );
}
else{
scanf("%d",&z);
Sgtree.update( x , y , z - 1 , 1 );
}
}
return 0;
}
R - Japan
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define LD long double
#define sqr(x) (x)*(x)
const int MAXN = 200100;
const int MAXM = 300010;
const int INF = 0x3f3f3f3f;
const LL MOD = 1e9+7;
const double eps = 1e-5;
struct rec{int x, y;}mp[1000010];
int a[1000010];
int n, m, k;
bool cmp(rec a, rec b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
void insert(int x) {
while (x <= m) {
a[x]++;
x += x & (-x);
}
}
int find(int x) {
int ans = 0;
while (x >= 1) {
ans += a[x];
x -= x & (-x);
}
return ans;
}
int main() {
int T;
int cas = 0;
scanf("%d", &T);
while (T--) {
memset(a, 0, sizeof a);
scanf("%d%d%d", &n, &m, &k);
for (int i=1; i<=k; ++i) {
scanf("%d%d", &mp[i].x, &mp[i].y);
}
sort(mp+1, mp+1+k, cmp);
LL ans = 0;
for (int i=1; i<=k; ++i) {
insert(mp[i].y);
LL t = i - find(mp[i].y);
//printf("%d\n", t);
ans += t;
}
printf("Test case %d: %lld\n", ++cas, ans);
}
}