結果
問題 | No.619 CardShuffle |
ユーザー |
![]() |
提出日時 | 2017-12-19 01:10:02 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,099 ms / 3,000 ms |
コード長 | 4,160 bytes |
コンパイル時間 | 3,029 ms |
コンパイル使用メモリ | 78,224 KB |
実行使用メモリ | 95,476 KB |
最終ジャッジ日時 | 2024-12-22 21:37:33 |
合計ジャッジ時間 | 23,754 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 35 |
ソースコード
import java.io.*; import java.util.*; class Main { static final long MOD=1000000007; static int[]c,o; static SegmentTree seg; static void update(int i){ if(o[i]==1) seg.update(i,new long[]{-1,0,c[i]}); else seg.update(i,new long[]{c[i],0,1}); } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n=sc.nextInt(); c=new int[n/2+1]; o=new int[n/2+1]; for(int i=0;i<n;++i) if(i%2==0) c[i/2]=sc.nextInt(); else o[i/2]=sc.next().equals("*")?1:0; o[n/2]=0; int q=sc.nextInt(); seg=new SegmentTree(); for(int i=0;i<n/2+1;++i)update(i); for(int i=0;i<q;++i){ String t=sc.next(); int x=sc.nextInt(); int y=sc.nextInt(); if(t.equals("!")){ x--; y--; if(x%2==1){ int temp=o[x/2]; o[x/2]=o[y/2]; o[y/2]=temp; }else{ int taplis=c[x/2]; c[x/2]=c[y/2]; c[y/2]=taplis; } update(x/2); update(y/2); }else{ long[]res=seg.query(x/2,y/2+1); long ans=0; if(res[0]>=0) for(int j=0;j<2;++j)ans=(ans+res[j])%MOD; if(o[y/2]==1) ans=(ans+res[2])%MOD; out.println(ans); } } out.close(); } // http://codeforces.com/blog/entry/7018 //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class SegmentTree { static final int SIZE = 1 << 18; static final long MOD=1000000007; long[][] seg; SegmentTree() { this.seg = new long[2 * SIZE][3]; for(int i=0;i<2*SIZE;++i){ seg[i][0]=-1; seg[i][2]=1; } } static long[]comb(long[]a,long[]b){ if(a[0]==-1&&b[0]==-1) return new long[]{-1,0,a[2]*b[2]%MOD}; if(a[0]==-1&&b[0]>=0) return new long[]{a[2]*b[0]%MOD,b[1],b[2]}; if(a[0]>=0&&b[0]==-1) return new long[]{a[0],a[1],a[2]*b[2]%MOD}; return new long[]{a[0],(a[1]+b[1]+a[2]*b[0])%MOD,b[2]}; } void update(int x, long[]value) { x += SIZE - 1; for(int i=0;i<3;++i) this.seg[x][i] = value[i]; while (x > 0) { x = (x - 1) / 2; this.seg[x] = comb(this.seg[2 * x + 1], this.seg[2 * x + 2]); } } long[]query(int a,int b,int l,int r,int k){ if(a<=l&&r<=b)return seg[k]; if(b<=l||r<=a) return new long[]{-1,0,1}; long[]ans1=query(a,b,l,(l+r)/2,2*k+1); long[]ans2=query(a,b,(l+r)/2,r,2*k+2); return comb(ans1,ans2); } long[]query(int l, int r) { return query(l,r,0,SIZE,0); } }